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/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ generic std gap is planned here even when moq never calls it.
- [A reversed zip's per-pair cost](/quest/m3/ts-zip-rev-overhead.md) - a reversed by-index zip costs per pair what a forward one does, or the gap is explained
- [Whole writes into a lazy cell or thread-local key](/quest/m3/ts-lazy-writes.md) - a whole write through a `&mut` to a `LazyCell`, `LazyLock`, or `LocalKey` keeps its state
- [Type errors at the Rust that made them](/quest/m3/ts-span-precision.md) - a `tsc` error on output names the tail expression or call argument that made it
- [An Option of numbers in the result slot](/quest/m3/ts-option-slot.md) - `varint_opt`'s `Option` comes back through the slot with no object per call
- [Fresh writes through a zip, a copy, and a repeat](/quest/m3/ts-fresh-shapes.md) - zipping a `Vec<[T; N]>`, reading a `T` out of `Zip::next`, and `copied` or `vec![x; n]` over a `Copy` type translate
- [Python's wake on the loop's own thread](/quest/m3/ffi-python-wake.md) - a wake on the event loop's thread resumes the future with no socket pair
- [Python's call cost](/quest/m3/ffi-python-calls.md) - a call on an object costs what BoltFFI's does, or the gap is explained
Expand Down
17 changes: 0 additions & 17 deletions quest/m3/ts-option-slot.md

This file was deleted.

5 changes: 3 additions & 2 deletions ts/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
whose object its body never makes as its `data`, `start`, and `length`:
`read(buf, form)` is `read(buf.data, buf.start, buf.length, form)`. A caller takes
which from the output. One that returns a `Result` of byte slices
and scalars, or of numbers alone, returns its error or `undefined`, and leaves the `Ok`'s parts in a
slot `lib.ts` exports, named for the payload type, such as `out$ref_slice_u8`. If the
and scalars, or of numbers alone, an `Option` of one counting as a number, such as
`Result<Option<u64>, E>`, returns its error or `undefined`, and leaves the `Ok`'s parts in a
slot `lib.ts` exports, named for the payload type, such as `out$ref_slice_u8` or `out$Option_u64`. If the
`Ok`'s first part is a number, it returns that, or `undefined` for an error, and leaves
the rest, or the error as `err`, in the slot, such as `out$tuple2_u64_ref_slice_u8`. A root still takes an `rt.ByteSlice` and returns
the `Result`.
Expand Down
7 changes: 4 additions & 3 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,8 @@ they need.
the geometric mean, and frame decode at 0.61, 0.56, 0.70, and 0.61, with the A/A route
within 0.04 and encode no slower. A tuple or `Result` of numbers alone is held so only
where it meets a slot below, and its object, such as an `rt.Ok`, counts as made at each
read that makes it. A return is still a `ByteSlice` but for the `Result`s below.
read that makes it. An `Option` of a number is one part, that or `undefined`, as its JS
value is. A return is still a `ByteSlice` but for the `Result`s below.
- A function only translated code calls takes each `&[u8]` or `&[u8; N]` parameter as
three, its `data`, `start`, and `length`, which its body holds as that local's parts:
not a root, which the outside calls, a closure, or an `async fn`. One whose object the
Expand All @@ -819,8 +820,8 @@ they need.
1.01-1.05 over two runs, with the A/A route within 0.05, Firefox and Bun within their
noise, and Node's varint decode at 1.11-1.15.
- A function of the crate's that only translated code calls and that returns a `Result`
of byte slices and scalars, or of numbers alone, such as `Result<(u64, &[u8]), E>` or
`Result<u64, E>`, whose error is one JS
of byte slices and scalars, or of numbers alone, such as `Result<(u64, &[u8]), E>`,
`Result<u64, E>`, or `Result<Option<u64>, E>`, whose error is one JS
value that is never `undefined`, writes the `Ok`'s parts into a module-level slot and returns the error or
`undefined`. The slot is shared by every function returning that payload type, in
`lib.ts` and named for the type as the shims are, as `out$ref_slice_u8` or `out$u64`, so
Expand Down
9 changes: 5 additions & 4 deletions ts/fixtures/codec/expected/coding/decode.Decoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "../rt.ts";
import { out$bool, out$ref_slice_u8, out$tuple2_u64_ref_slice_u8, out$u16, out$u64, out$u8 } from "../lib.ts";
import { out$Option_u64, out$bool, out$ref_slice_u8, out$tuple2_u64_ref_slice_u8, out$u16, out$u64, out$u8 } from "../lib.ts";
import * as DecodeError from "../coding/decode.DecodeError.ts";
import { read } from "../coding/varint.ts";
import type * as Form from "../coding/varint.Form.ts";
Expand Down Expand Up @@ -190,13 +190,14 @@ export function varint(self: Decoder): rt.u64 | undefined {
}

/** Read an optional varint: 0 is `None`, and `n + 1` is `Some(n)`. */
export function varintOpt(self: Decoder): rt.Result<rt.u64 | undefined, DecodeError.DecodeError> {
export function varintOpt(self: Decoder): DecodeError.DecodeError | undefined {
const part: rt.u64 | undefined = varint(self);
const err: DecodeError.DecodeError | undefined = part === undefined ? out$u64.err as DecodeError.DecodeError : undefined;
if (err === undefined) {
return rt.Ok(rt.u64CheckedSub(part as rt.u64, 1));
out$Option_u64.p = rt.u64CheckedSub(part as rt.u64, 1);
return undefined;
}
return rt.Err(err as DecodeError.DecodeError);
return err as DecodeError.DecodeError;
}

/** Read a varint length, then that many raw bytes. */
Expand Down
49 changes: 25 additions & 24 deletions ts/fixtures/codec/expected/harness.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { out$bool, out$ref_slice_u8, out$u16, out$u64, out$u8, out$usize } from "./lib.ts";
import { out$Option_u64, out$bool, out$ref_slice_u8, out$u16, out$u64, out$u8, out$usize } from "./lib.ts";
import * as DecodeError from "./coding/decode.DecodeError.ts";
import * as Decoder from "./coding/decode.Decoder.ts";
import * as EncodeError from "./coding/encode.EncodeError.ts";
Expand Down Expand Up @@ -224,41 +224,42 @@ export function decodeScript(dataData: Uint8Array, dataStart: number, dataLen: n
export function decodeStep(r: Decoder.Decoder, step: rt.u8, out: rt.ByteVec): rt.Result<undefined, DecodeError.DecodeError> {
const _5: rt.u8 = rt.u8Rem(step, 10);
if (_5 === 0) {
const part11: rt.u8 | undefined = Decoder.u8(r);
const err7: DecodeError.DecodeError | undefined = part11 === undefined ? out$u8.err as DecodeError.DecodeError : undefined;
const part12: rt.u8 | undefined = Decoder.u8(r);
const err8: DecodeError.DecodeError | undefined = part12 === undefined ? out$u8.err as DecodeError.DecodeError : undefined;
if (err8 === undefined) {
rt.byteVecPush(out, part12 as rt.u8);
} else {
return rt.Err(err8 as DecodeError.DecodeError);
}
} else if (_5 === 1) {
const part11: rt.u16 | undefined = Decoder.u16(r);
const err7: DecodeError.DecodeError | undefined = part11 === undefined ? out$u16.err as DecodeError.DecodeError : undefined;
if (err7 === undefined) {
rt.byteVecPush(out, part11 as rt.u8);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(part11 as rt.u16)));
} else {
return rt.Err(err7 as DecodeError.DecodeError);
}
} else if (_5 === 1) {
const part10: rt.u16 | undefined = Decoder.u16(r);
const err6: DecodeError.DecodeError | undefined = part10 === undefined ? out$u16.err as DecodeError.DecodeError : undefined;
} else if (_5 === 2) {
const err6: DecodeError.DecodeError | undefined = Decoder.bool(r);
const part10: boolean = out$bool.p;
if (err6 === undefined) {
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(part10 as rt.u16)));
rt.byteVecPush(out, part10 ? 1 : 0);
} else {
return rt.Err(err6 as DecodeError.DecodeError);
}
} else if (_5 === 2) {
const err5: DecodeError.DecodeError | undefined = Decoder.bool(r);
const part9: boolean = out$bool.p;
} else if (_5 === 3) {
const part9: rt.u64 | undefined = Decoder.varint(r);
const err5: DecodeError.DecodeError | undefined = part9 === undefined ? out$u64.err as DecodeError.DecodeError : undefined;
if (err5 === undefined) {
rt.byteVecPush(out, part9 ? 1 : 0);
putU64(out, part9 as rt.u64);
} else {
return rt.Err(err5 as DecodeError.DecodeError);
}
} else if (_5 === 3) {
const part8: rt.u64 | undefined = Decoder.varint(r);
const err4: DecodeError.DecodeError | undefined = part8 === undefined ? out$u64.err as DecodeError.DecodeError : undefined;
if (err4 === undefined) {
putU64(out, part8 as rt.u64);
} else {
return rt.Err(err4 as DecodeError.DecodeError);
}
} else if (_5 === 4) {
const _31: rt.Result<rt.u64 | undefined, DecodeError.DecodeError> = Decoder.varintOpt(r);
if (_31.ok) {
const val5: rt.u64 | undefined = _31.value;
const err4: DecodeError.DecodeError | undefined = Decoder.varintOpt(r);
const part8: rt.u64 | undefined = out$Option_u64.p;
if (err4 === undefined) {
const val5: rt.u64 | undefined = part8;
if (val5 === undefined) {
rt.byteVecPush(out, 0);
} else {
Expand All @@ -267,7 +268,7 @@ export function decodeStep(r: Decoder.Decoder, step: rt.u8, out: rt.ByteVec): rt
putU64(out, v);
}
} else {
return _31;
return rt.Err(err4 as DecodeError.DecodeError);
}
} else if (_5 === 5) {
const err3: DecodeError.DecodeError | undefined = Decoder.bytes(r);
Expand Down
2 changes: 2 additions & 0 deletions ts/fixtures/codec/expected/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export function copy$bench_Timestamp(v: Timestamp.Timestamp): Timestamp.Timestam
return { value: v.value, scale: v.scale };
}

export const out$Option_u64: { p: rt.u64 | undefined } = { p: undefined as never };

export const out$bool: { p: boolean } = { p: undefined as never };

export const out$ref_slice_u8: { pData: Uint8Array; pStart: number; pLen: number } = { pData: undefined as never, pStart: undefined as never, pLen: undefined as never };
Expand Down
33 changes: 21 additions & 12 deletions ts/fixtures/lang/cells/expected/cells.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { out$u64, out$u8 } from "./lib.ts";
import { out$Option_u8, out$u64, out$u8 } from "./lib.ts";
import { thenSome$0d5am30 } from "./core/bool.ts";
import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr } from "./core/option.ts";
import { err$1nbmqmx, map$1ma2nl9 as result_map$1ma2nl9, map$0l5uclx as result_map$0l5uclx, map$14n87rz as result_map$14n87rz, map$0h1vl84 as result_map$0h1vl84, map$03hz3l7 as result_map$03hz3l7, map$0yqi183 as result_map$0yqi183 } from "./core/result.ts";
Expand Down Expand Up @@ -153,20 +153,24 @@ export function cellOption(dataData: Uint8Array, dataStart: number, dataLen: num
const _166: rt.u8 = rt.u8Rem(v2, 4);
if (_166 === 0 || _166 === 1) {
result = rt.onceGetOrTryInit(cell, (): rt.Result<rt.u8 | undefined, rt.u8> => {
return made$Option_u8(make, v2);
const err4 = made$Option_u8(make, v2);
return err4 !== undefined ? rt.Err(err4 as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None);
} else if (_166 === 2) {
const _174: rt.OnceCell<rt.u8 | undefined, rt.None> = cell;
result = rt.onceGetOrTryInit(cell, (): rt.Result<rt.u8 | undefined, rt.u8> => {
rt.onceSet(_174, make(v2 ^ 1), rt.None);
return made$Option_u8(make, v2);
const err3 = made$Option_u8(make, v2);
return err3 !== undefined ? rt.Err(err3 as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None);
} else {
const _179: rt.OnceCell<rt.u8 | undefined, rt.None> = cell;
result = rt.onceGetOrTryInit(cell, (): rt.Result<rt.u8 | undefined, rt.u8> => {
return made$Option_u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$1ma2nl9(rt.onceGetOrTryInit(_179, (): rt.Result<rt.u8 | undefined, rt.u8> => {
return made$Option_u8(make, v2 ^ 1);
const err2 = made$Option_u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$1ma2nl9(rt.onceGetOrTryInit(_179, (): rt.Result<rt.u8 | undefined, rt.u8> => {
const err = made$Option_u8(make, v2 ^ 1);
return err !== undefined ? rt.Err(err as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None), (_22: rt.u8 | undefined): undefined => {})), v2));
return err2 !== undefined ? rt.Err(err2 as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None);
}
if (result.ok) {
Expand Down Expand Up @@ -1289,20 +1293,24 @@ export function lockOption(dataData: Uint8Array, dataStart: number, dataLen: num
const _166: rt.u8 = rt.u8Rem(v2, 4);
if (_166 === 0 || _166 === 1) {
result = rt.onceGetOrTryInit(cell, (): rt.Result<rt.u8 | undefined, rt.u8> => {
return made$Option_u8(make, v2);
const err4 = made$Option_u8(make, v2);
return err4 !== undefined ? rt.Err(err4 as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None);
} else if (_166 === 2) {
const _174: rt.OnceLock<rt.u8 | undefined, rt.None> = cell;
result = rt.onceGetOrTryInit(cell, (): rt.Result<rt.u8 | undefined, rt.u8> => {
rt.onceSet(_174, make(v2 ^ 1), rt.None);
return made$Option_u8(make, v2);
const err3 = made$Option_u8(make, v2);
return err3 !== undefined ? rt.Err(err3 as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None);
} else {
const _179: rt.OnceLock<rt.u8 | undefined, rt.None> = cell;
result = rt.onceGetOrTryInit(cell, (): rt.Result<rt.u8 | undefined, rt.u8> => {
return made$Option_u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$1ma2nl9(rt.onceGetOrTryInit(_179, (): rt.Result<rt.u8 | undefined, rt.u8> => {
return made$Option_u8(make, v2 ^ 1);
const err2 = made$Option_u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$1ma2nl9(rt.onceGetOrTryInit(_179, (): rt.Result<rt.u8 | undefined, rt.u8> => {
const err = made$Option_u8(make, v2 ^ 1);
return err !== undefined ? rt.Err(err as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None), (_22: rt.u8 | undefined): undefined => {})), v2));
return err2 !== undefined ? rt.Err(err2 as rt.u8) : rt.Ok(out$Option_u8.p);
}, undefined, rt.None);
}
if (result.ok) {
Expand Down Expand Up @@ -2309,11 +2317,12 @@ export function twin$ref_sync_OnceLock_unit(t: rt.OnceLock<undefined, rt.None>):
}

/** What `make` makes of `v`, or `v` back as an error for every third byte. */
export function made$Option_u8(make: (a0: rt.u8) => rt.u8 | undefined, v: rt.u8): rt.Result<rt.u8 | undefined, rt.u8> {
export function made$Option_u8(make: (a0: rt.u8) => rt.u8 | undefined, v: rt.u8): rt.u8 | undefined {
if (rt.u8Rem(v, 3) === 0) {
return rt.Err(v);
return v;
}
return rt.Ok(make(v));
out$Option_u8.p = make(v);
return undefined;
}

/** What `make` makes of `v`, or `v` back as an error for every third byte. */
Expand Down
2 changes: 2 additions & 0 deletions ts/fixtures/lang/cells/expected/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import * as rt from "./rt.ts";
import { cells } from "./cells.ts";

export const out$Option_u8: { p: rt.u8 | undefined } = { p: undefined as never };

export const out$u64: { err: unknown } = { err: undefined };

export const out$u8: { err: unknown } = { err: undefined };
Expand Down
7 changes: 7 additions & 0 deletions ts/fixtures/lang/returns/expected/core/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import * as rt from "../rt.ts";

export function thenSome$0b23a33(self: boolean, t: rt.u16): rt.u16 | undefined {
if (!self) {
return undefined;
}
return t;
}

export function then$0w2ymju(self: boolean, f: () => rt.ByteSlice): rt.ByteSlice | undefined {
if (!self) {
return undefined;
Expand Down
32 changes: 30 additions & 2 deletions ts/fixtures/lang/returns/expected/core/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
import * as rt from "../rt.ts";
import type * as returns_Error from "../returns.Error.ts";

export function unwrapOr$06r8g1t(self: rt.i128 | undefined, default_: rt.i128): rt.i128 {
if (self === undefined) {
return default_;
}
return self;
}

export function unwrapOr$1j4op5y(self: rt.u16 | undefined, default_: rt.u16): rt.u16 {
if (self === undefined) {
return default_;
}
return self;
}

export function unwrapOr$0r2bmpr(self: rt.u8 | undefined, default_: rt.u8): rt.u8 {
if (self === undefined) {
return default_;
Expand All @@ -24,21 +38,35 @@ export function map$1yogfym(self: [rt.u8, rt.ByteSlice] | undefined, f: (a0: [rt
return f(self);
}

export function map$1cf8u0y(self: rt.u64 | undefined, f: (a0: rt.u64) => rt.u64): rt.u64 | undefined {
if (self === undefined) {
return undefined;
}
return f(self);
}

export function mapOr$1surhce(self: rt.u8 | undefined, default_: rt.u8, f: (a0: rt.u8) => rt.u8): rt.u8 {
if (self === undefined) {
return default_;
}
return f(self);
}

export function okOr$1b6nkwr(self: rt.char | undefined, err: rt.u8): rt.Result<rt.char, rt.u8> {
if (self === undefined) {
return rt.Err(err);
}
return rt.Ok(self);
}

export function okOr$0gtm15y(self: rt.ByteSlice | undefined, err: rt.u8 | undefined): rt.Result<rt.ByteSlice, rt.u8 | undefined> {
export function okOr$12p77jh(self: rt.ByteSlice | undefined, err: returns_Error.Error): rt.Result<rt.ByteSlice, returns_Error.Error> {
if (self === undefined) {
return rt.Err(err);
}
return rt.Ok(self);
}

export function okOr$12p77jh(self: rt.ByteSlice | undefined, err: returns_Error.Error): rt.Result<rt.ByteSlice, returns_Error.Error> {
export function okOr$0gtm15y(self: rt.ByteSlice | undefined, err: rt.u8 | undefined): rt.Result<rt.ByteSlice, rt.u8 | undefined> {
if (self === undefined) {
return rt.Err(err);
}
Expand Down
12 changes: 11 additions & 1 deletion ts/fixtures/lang/returns/expected/core/result.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "../rt.ts";
import type * as returns_Error from "../returns.Error.ts";
import * as returns_Error from "../returns.Error.ts";

export function eq$11dy112(self: rt.Result<rt.u64 | undefined, returns_Error.Error>, other: rt.Result<rt.u64 | undefined, returns_Error.Error>): boolean {
if (!((self.ok ? 0 : 1) === (other.ok ? 0 : 1))) {
return false;
}
if (self.ok) {
return rt.eqOption(rt.u64Eq)(self.value, (other as { ok: true; value: rt.u64 | undefined }).value);
}
return returns_Error.eq$cmp_PartialEq(self.error, (other as { ok: false; error: returns_Error.Error }).error);
}

export function map$1rffkv6(self: rt.Result<rt.u32, returns_Error.Error>, _: (a0: rt.u32) => rt.u64): rt.Result<rt.u64, returns_Error.Error> {
if (self.ok) {
Expand Down
Loading