Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ deletes the quests that follow it.
- [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values
- [Iterators](/quest/m2/ts-iter.md) - lazy `rt.ts` adaptors with a `DONE` protocol
- [Collections](/quest/m2/ts-collections.md) - maps, sets, and queues keep Rust's key semantics
- [Methods on generic types](/quest/m2/ts-generic-methods.md) - generic classes' methods type-check, derived `Debug` included
- [Source order](/quest/m2/ts-source-order.md) - each module's items come out in Rust source order
- [Translate moq-net's model to TypeScript](/quest/m2/ts-model.md) - the driver grows from moq-net's codec to its model, for moq to compare against WASM
- [Namespaced external types](/quest/m2/uniffi-namespaces.md) - two crates linked into one library can each export a same-named type in every generator
Expand Down
16 changes: 0 additions & 16 deletions quest/m2/ts-generic-methods.md

This file was deleted.

1 change: 0 additions & 1 deletion quest/m2/ts-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ Public API: the translator's output for moq-net's model.
- [Shared ownership](/quest/m2/ts-shared.md) - kio's waiters are `Arc`/`Weak` with observable counts
- [Iterators](/quest/m2/ts-iter.md) - the model's adaptor chains
- [Collections](/quest/m2/ts-collections.md) - the model's maps and queues
- [Methods on generic types](/quest/m2/ts-generic-methods.md) - the model's generic types
13 changes: 10 additions & 3 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ would: `rt.fmt.toString((f) => DecodeError.fmt$Display(err, f))` and

Functions and members are camelCase. Functions are monomorphized: each instance is its
own function, and instances that share a name are suffixed by type, as in
`roundtripLite$Group`. Types are translated once, generically. A `Copy` struct, tuple, or
`roundtripLite$Group` or `get$Wrap_Pair`. Types are translated once, generically, so a
generic class holds a method per instance, each declaring the instance it takes as
`this`: `fmt$Pair(this: Wrap<Pair>, f: rt.Formatter)`. A `Copy` struct, tuple, or
array, or an `Option` or `Result` holding one, is copied where Rust copies it, since JS
would share the object.

Expand All @@ -92,6 +94,10 @@ Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` bu

- 64-bit integers are native `bigint`: lossless and readable. A pair of `u32` halves is
faster but waits until `just ts bench` measures `bigint` as the cost.
- A generic type is one class or union, the type TS callers name, and a method on it is
one instance's, typed by its `this`. A class per instance (`Wrap$Pair`) would rename
the type for callers, and TS generics with trait dictionaries would undo
monomorphization.
- `Result` is a plain `{ ok, value } | { ok, error }` object rather than an exception, so
`?` and `match` keep Rust's control flow exactly.
- Bodies lower from THIR, which keeps loops, `match`, and `?` shaped as written. It does
Expand Down Expand Up @@ -130,8 +136,9 @@ built natively for wasm32 and run beside its translation on the same inputs by
- `codec` vendors moq-net's sync codec, which a harness outside the crate could not reach,
and writes its errors' `Display` and `Debug`.
- `lang` covers every integer width's arithmetic and casts, control flow, patterns,
`Copy` aggregates, and formatting. Its differential test runs through `caller.ts`, a
hand-written caller that also prints a `printable` error nothing translated prints.
`Copy` aggregates, formatting, and methods on generic types. Its differential test runs
through `caller.ts`, a hand-written caller that also prints a `printable` error nothing
translated prints.
- `reject` holds constructs outside the subset; its expected errors are `expected.txt`,
and its expected census is `census.md`.

Expand Down
312 changes: 312 additions & 0 deletions ts/fixtures/lang/expected/generics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { Pair, put } from "./fmt.ts";
import { Point, Meters, Shape } from "./shapes.ts";

/** A generic struct, with methods on every instance, on some, and on one. */
export class Wrap<T> {
value: T;
count: number;

constructor(fields: { value: T; count: number }) {
this.value = fields.value;
this.count = fields.count;
}

static new$Point(value: Point): Wrap<Point> {
return new Wrap({ value: rt.copy(value), count: 0 });
}

bump$Point(this: Wrap<Point>): number {
this.count = rt.u8.wrappingAdd(this.count, 1);
return this.count;
}

static new$u8(value: number): Wrap<number> {
return new Wrap({ value, count: 0 });
}

bump$u8(this: Wrap<number>): number {
this.count = rt.u8.wrappingAdd(this.count, 1);
return this.count;
}

static new$Wrap_Pair(value: Wrap<Pair>): Wrap<Wrap<Pair>> {
return new Wrap({ value: rt.copy(value), count: 0 });
}

static new$Pair(value: Pair): Wrap<Pair> {
return new Wrap({ value: rt.copy(value), count: 0 });
}

static new$Shape(value: Shape): Wrap<Shape> {
return new Wrap({ value, count: 0 });
}

intoInner(this: Wrap<Point>): Point {
return rt.copy(this.value);
}

fmt$Point(this: Wrap<Point>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => this.value.fmt(f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

fmt$u8(this: Wrap<number>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.u8.display(this.value, f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

fmt$Wrap_Pair(this: Wrap<Wrap<Pair>>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => this.value.fmt$Pair(f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

fmt$Shape(this: Wrap<Shape>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => Shape.fmt(this.value, f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

double(this: Wrap<number>): number {
return rt.u16.mul(this.value, 2);
}

get$u8(this: Wrap<number>): number {
return this.value;
}

get$Pair(this: Wrap<Pair>): Pair {
return this.value;
}

get$Wrap_Pair(this: Wrap<Wrap<Pair>>): Wrap<Pair> {
return this.value;
}

scaled$Meters(this: Wrap<Meters>): bigint {
return rt.u64.saturatingAdd(this.value.area(), BigInt(this.count));
}

scaled$Shape(this: Wrap<Shape>): bigint {
return rt.u64.saturatingAdd(Shape.area(this.value), BigInt(this.count));
}

/** A default method, monomorphized for each type. */
describe$Meters(this: Wrap<Meters>, out: rt.ByteVec): void {
out.push(0);
out.extendFromSlice(rt.u64.toBeBytes(this.area$Meters()));
}

/** A default method, monomorphized for each type. */
describe$Shape(this: Wrap<Shape>, out: rt.ByteVec): void {
out.push(4);
out.extendFromSlice(rt.u64.toBeBytes(this.area$Shape()));
}

/** A method with its own type parameter. */
map(this: Wrap<number>, f: (a0: number) => Pair): Wrap<Pair> {
return new Wrap({ value: f(this.value), count: this.count });
}

eq(this: Wrap<Pair>, other: Wrap<Pair>): boolean {
return this.count === other.count && this.value.eq(other.value);
}

fmt$Pair(this: Wrap<Pair>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => this.value.fmt(f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

bump$Vec_u16(this: Wrap<number[]>): number {
this.count = rt.u8.wrappingAdd(this.count, 1);
return this.count;
}

static new$Option_u8(value: number | undefined): Wrap<number | undefined> {
return new Wrap({ value, count: 0 });
}

get$Option_u8(this: Wrap<number | undefined>): number | undefined {
return this.value;
}

fmt$Vec_u16(this: Wrap<number[]>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.fmt.list(this.value, f, rt.u16.display)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

fmt$Option_u8(this: Wrap<number | undefined>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.fmt.option(this.value, f, rt.u8.display)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

fmt$Result_u8_Shape(this: Wrap<rt.Result<number, Shape>>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.fmt.result(this.value, f, rt.u8.display, (value: Shape, f: rt.Formatter) => Shape.fmt(value, f))).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

static new$i16(value: number): Wrap<number> {
return new Wrap({ value, count: 0 });
}

fmt$i16(this: Wrap<number>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.i16.display(this.value, f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish();
}

area$Meters(this: Wrap<Meters>): bigint {
return this.value.area();
}

area$Shape(this: Wrap<Shape>): bigint {
return Shape.area(this.value);
}
}

/** A generic enum, with a companion function per instance. */
export type Either<L, R> = { tag: "Left"; _0: L } | { tag: "Right"; value: R } | { tag: "Neither" };

export const Either = {
flip(self: Either<number, Point>): Either<Point, number> {
if (self.tag === "Left") {
const l = self._0;
return { tag: "Right", value: l };
} else if (self.tag === "Right") {
const value = rt.copy(self.value);
return { tag: "Left", _0: rt.copy(value) };
} else {
return { tag: "Neither" };
}
},

left$u8_Point(self: Either<number, Point>): number | undefined {
if (self.tag === "Left") {
const l = self._0;
return l;
} else {
return undefined;
}
},

left$Point_u8(self: Either<Point, number>): Point | undefined {
if (self.tag === "Left") {
const l = rt.copy(self._0);
return rt.copy(l);
} else {
return undefined;
}
},

fmt$u8_Point(self: Either<number, Point>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
if (self.tag === "Left") {
const __self0 = self._0;
return f.debugTuple("Left").field((f: rt.Formatter) => rt.u8.display(__self0, f)).finish();
} else if (self.tag === "Right") {
const __self02 = self.value;
return f.debugStruct("Right").field("value", (f: rt.Formatter) => __self02.fmt(f)).finish();
} else {
return f.writeStr("Neither");
}
},

fmt$Point_u8(self: Either<Point, number>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
if (self.tag === "Left") {
const __self0 = self._0;
return f.debugTuple("Left").field((f: rt.Formatter) => __self0.fmt(f)).finish();
} else if (self.tag === "Right") {
const __self02 = self.value;
return f.debugStruct("Right").field("value", (f: rt.Formatter) => rt.u8.display(__self02, f)).finish();
} else {
return f.writeStr("Neither");
}
},

left$u8_u16(self: Either<number, number>): number | undefined {
if (self.tag === "Left") {
const l = self._0;
return l;
} else {
return undefined;
}
},

fmt$u8_u16(self: Either<number, number>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
if (self.tag === "Left") {
const __self0 = self._0;
return f.debugTuple("Left").field((f: rt.Formatter) => rt.u8.display(__self0, f)).finish();
} else if (self.tag === "Right") {
const __self02 = self.value;
return f.debugStruct("Right").field("value", (f: rt.Formatter) => rt.u16.display(__self02, f)).finish();
} else {
return f.writeStr("Neither");
}
},
};

/** Two parameters, one nested in another generic type. */
export class Tagged<K, V> {
key: K;
inner: Wrap<V>;
all: Wrap<V>[];

constructor(fields: { key: K; inner: Wrap<V>; all: Wrap<V>[] }) {
this.key = fields.key;
this.inner = fields.inner;
this.all = fields.all;
}

first(this: Tagged<Either<number, number>, number>): Wrap<number> | undefined {
return rt.copy(rt.slice.first(this.all));
}

fmt(this: Tagged<Either<number, number>, number>, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugStruct("Tagged").field("key", (f: rt.Formatter) => Either.fmt$u8_u16(this.key, f)).field("inner", (f: rt.Formatter) => this.inner.fmt$i16(f)).field("all", (f: rt.Formatter) => rt.fmt.list(this.all, f, (value: Wrap<number>, f: rt.Formatter) => value.fmt$i16(f))).finish();
}
}

/** Each method once per instance: `Wrap` of a struct, an enum, a number, and itself. */
export function methods(data: Uint8Array, out: rt.ByteVec): void {
const at = (i: number): number => {
return rt.option.unwrapOr(rt.slice.get(data, i), 0);
};
let point: Wrap<Point> = Wrap.new$Point(new Point({ x: at(0), y: -2 }));
point.bump$Point();
let byte: Wrap<number> = Wrap.new$u8(at(1));
byte.bump$u8();
byte.bump$u8();
const nested = Wrap.new$Wrap_Pair(Wrap.new$Pair(new Pair(at(2), -3n)));
const meters = new Wrap({ value: new Meters(BigInt(at(3))), count: at(4) });
const shape = Wrap.new$Shape({ tag: "Rect", corner: rt.copy(point).intoInner(), width: at(5), height: 3 });
put(out, rt.fmt.format([(f: rt.Formatter) => point.fmt$Point(f), " ", (f: rt.Formatter) => byte.fmt$u8(f), " ", (f: rt.Formatter) => nested.fmt$Wrap_Pair(f), " ", (f: rt.Formatter) => shape.fmt$Shape(f)]));
put(out, rt.fmt.format([rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => nested.fmt$Wrap_Pair(f))]));
out.extendFromSlice(rt.u16.toBeBytes(byte.double()));
out.push(byte.get$u8());
out.push(nested.get$Wrap_Pair().get$Pair()._0);
out.extendFromSlice(rt.u64.toBeBytes(meters.scaled$Meters()));
out.extendFromSlice(rt.u64.toBeBytes(shape.scaled$Shape()));
meters.describe$Meters(out);
shape.describe$Shape(out);
const mapped = rt.copy(byte).map((b: number): Pair => {
return new Pair(b, BigInt(at(6)));
});
const arg2 = out;
const arg = mapped.eq(Wrap.new$Pair(new Pair(at(1), 0n)));
put(arg2, rt.fmt.format([(f: rt.Formatter) => mapped.fmt$Pair(f), " ", (f: rt.Formatter) => rt.fmt.bool(arg, f)]));
const eithers = [{ tag: "Left", _0: at(7) } as Either<number, Point>, { tag: "Right", value: Point.new(1, -1) } as Either<number, Point>, { tag: "Neither" } as Either<number, Point>];
for (const either of rt.copy(eithers)) {
const flipped = Either.flip(either);
const arg5 = out;
const arg3 = Either.left$u8_Point(either);
const arg4 = Either.left$Point_u8(flipped);
put(arg5, rt.fmt.format([(f: rt.Formatter) => Either.fmt$u8_Point(either, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => Either.fmt$Point_u8(flipped, f)), " ", (f: rt.Formatter) => rt.fmt.option(arg3, f, rt.u8.display), " ", (f: rt.Formatter) => rt.fmt.option(arg4, f, (value: Point, f: rt.Formatter) => value.fmt(f))]));
}
let list: Wrap<number[]> = new Wrap({ value: [], count: 1 });
list.value.push(at(10));
list.bump$Vec_u16();
const none = new Wrap({ value: undefined, count: 0 });
const some = Wrap.new$Option_u8(rt.slice.get(data, 11));
const err = new Wrap({ value: rt.Err({ tag: "Dot" } as Shape), count: 2 });
const arg7 = out;
const arg6 = rt.option.unwrapOr(none.get$Option_u8(), some.count);
put(arg7, rt.fmt.format([(f: rt.Formatter) => list.fmt$Vec_u16(f), " ", (f: rt.Formatter) => none.fmt$Option_u8(f), " ", (f: rt.Formatter) => some.fmt$Option_u8(f), " ", (f: rt.Formatter) => err.fmt$Result_u8_Shape(f), " ", (f: rt.Formatter) => rt.u8.display(arg6, f)]));
const tagged = new Tagged({ key: { tag: "Right", value: 500 } as Either<number, number>, inner: Wrap.new$i16(rt.i16.sub(at(8), 100)), all: rt.slice.toVec([Wrap.new$i16(-1), Wrap.new$i16(at(9))]) });
const arg9 = out;
const arg8 = tagged.first();
put(arg9, rt.fmt.format([(f: rt.Formatter) => tagged.fmt(f), " ", (f: rt.Formatter) => rt.fmt.option(arg8, f, (value: Wrap<number>, f: rt.Formatter) => value.fmt$i16(f))]));
out.extendFromSlice(rt.i16.toBeBytes(tagged.inner.value));
out.push(rt.option.unwrapOr(Either.left$u8_u16(tagged.key), 0));
}

//# sourceMappingURL=generics.ts.map
Loading