diff --git a/quest/m2/README.md b/quest/m2/README.md index 49eed83c9..e36757fc3 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -26,7 +26,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README ## Required -- [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read - [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures - [Translate moq-net's model to TypeScript](/quest/m2/ts-model/README.md) - the model translates with kio, moq-clock, and moq-pattern, for moq to compare against WASM - [Waiting: Swift 6.4 in nixpkgs-unstable](/quest/m2/uniffi-swift-arm.md) - the nightly UniFFI fixtures move back to arm diff --git a/quest/m2/ts-byte-views/README.md b/quest/m2/ts-byte-views/README.md deleted file mode 100644 index f99543d90..000000000 --- a/quest/m2/ts-byte-views/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# [M] Byte slices without objects - -## Goal - -Reading through a translated cursor allocates no object per read, so the codec's decode on -the [TS benchmark](/ts/README.md#bench) moves toward moq's hand-written cursor, with every -differential test still passing. - -## Plan - -`&[u8]` and `&[u8; N]` are an `rt.ByteSlice`, a range of a `Uint8Array`, so each split, the -tuples, `Option`s, and `Result`s around it, and each cursor update make objects: 10 per -multi-byte varint, none of which V8 removes. A hand edit of the codec's translation that -kept them as `(data, start, length)` made Chrome's varint decode 2.3-2.9x and frame decode -2.0-2.2x faster, with Firefox, Node, and Bun in step and encode unchanged (spiked on -2026-10-27, local branch `spike/ts-scalar-slices`). - -Decided with the maintainer on 2026-10-27: scalar-replace values built of byte slices, -scalars, tuples, `Option`, `Result`, and `ControlFlow`, materializing an object only where -one escapes, in pieces that each land and measure alone. -The precedents are `stepped` iterators, `range_nexts`, and `place_opts`, which a general -pass could later absorb. - -Slices now pass as their parts, a `Result` holding one comes back in a slot -([#160](https://github.com/moq-dev/rust2/pull/160)), and a private slice field is three -fields ([#379](https://github.com/moq-dev/rust2/pull/379)). What remains is a `Result` or -tuple of scalars alone: `Decoder::varint` still returns an `rt.Ok` per read, and the -bench's `frame_header` an `rt.Ok` of a pair per frame. - -Left here: iterating a byte slice makes one view, which `rt.iterSlice` and stepping in -place read. - -Measure Chrome, Firefox, Node, and Bun; Chrome decides. - -Public API: the translator's output for byte slices. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index bef7da922..78ffbf364 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -9,7 +9,7 @@ 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 returns its error or `undefined`, and leaves the `Ok`'s parts in a + and scalars, or of numbers alone, returns its error or `undefined`, and leaves the `Ok`'s parts in a module-level slot, such as `sliceOut`. 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 `readOut`. A root still takes an `rt.ByteSlice` and returns diff --git a/ts/README.md b/ts/README.md index 8fe754d1f..82559a8e1 100644 --- a/ts/README.md +++ b/ts/README.md @@ -696,8 +696,9 @@ they need. often than before stays one. `just ts bench` measured the codec's varint decode at 0.49 times the objects' time in Chrome, 0.57 in Firefox, 0.64 in Node, and 0.56 in Bun at 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 field is still a `ByteSlice`, as is a return but - for the `Result`s below ([byte slices without objects](/quest/m2/ts-byte-views/README.md)). + 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. - 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 @@ -713,7 +714,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, such as `Result<(u64, &[u8]), E>`, whose error is one JS + of byte slices and scalars, or of numbers alone, such as `Result<(u64, &[u8]), E>` or + `Result`, 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, named for the first of them in source order with its suffix, as `readOut` or `leadOut$u16`. diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index e77639d01..eca36456d 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -90,12 +90,13 @@ export function decodeVarints(wire: rt.ByteSlice, form: Form.Form, out: rt.ByteV let count: rt.usize = 0; let last: rt.u64 = 0; while (!Decoder.isEmpty(r)) { - const _9: rt.Result = Decoder.varint(r); - if (_9.ok) { - last = _9.value; + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + last = part as rt.u64; count = rt.usizeAdd(count, 1); } else { - return _9; + return rt.Err(err as DecodeError.DecodeError); } } putUsize(out, count); @@ -167,18 +168,20 @@ export function decodeFrames(wire: rt.ByteSlice, version: lite_version_Version.V let total: rt.u64 = 0; let last: rt.i64 = 0; while (!Decoder.isEmpty(r)) { - const _12: rt.Result = Decoder.varint(r); - if (_12.ok) { - last = unzigzag(_12.value); - const _18: rt.Result = Decoder.varint(r); - if (_18.ok) { - total = rt.u64Add(total, _18.value); + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + last = unzigzag(part as rt.u64); + const part2: rt.u64 | undefined = Decoder.varint(r); + const err2: DecodeError.DecodeError | undefined = part2 === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err2 === undefined) { + total = rt.u64Add(total, part2 as rt.u64); count = rt.usizeAdd(count, 1); } else { - return _18; + return rt.Err(err2 as DecodeError.DecodeError); } } else { - return _12; + return rt.Err(err as DecodeError.DecodeError); } } putUsize(out, count); @@ -210,20 +213,22 @@ export function decodeSplit(data: rt.ByteSlice, version: lite_version_Version.Ve continue l2; } const r: Decoder.Decoder = Decoder.new(rt.byteSliceRange(_6[1], start, end), Form.from$convert_From_lite_version_Version(version)); - const _32: rt.Result<[rt.i64, rt.u64], DecodeError.DecodeError> = frameHeader(r); - if (_32.ok) { - const delta: rt.i64 = _32.value[0]; - const size: rt.u64 = _32.value[1]; + const part: rt.i64 | undefined = frameHeader(r); + const err2: DecodeError.DecodeError | undefined = part === undefined ? frameHeaderOut.err as DecodeError.DecodeError : undefined; + const part2: rt.u64 = frameHeaderOut.p_1; + if (err2 === undefined) { + const delta: rt.i64 = part as rt.i64; + const size: rt.u64 = part2; start = rt.usizeSub(end, Decoder.remaining(r)); last = delta; total = rt.u64Add(total, size); count = rt.usizeAdd(count, 1); } else { - if (_32.error.tag === "Short") { + if ((err2 as DecodeError.DecodeError).tag === "Short") { short = rt.usizeAdd(short, 1); continue l2; } - return _32 as rt.Result; + return rt.Err(err2 as DecodeError.DecodeError); } } } @@ -238,19 +243,26 @@ export function decodeSplit(data: rt.ByteSlice, version: lite_version_Version.Ve } /** One FRAME header: the zigzag timestamp delta, then the size. */ -export function frameHeader(r: Decoder.Decoder): rt.Result<[rt.i64, rt.u64], DecodeError.DecodeError> { - const _3: rt.Result = Decoder.varint(r); - if (_3.ok) { - const delta: rt.i64 = unzigzag(_3.value); - const _9: rt.Result = Decoder.varint(r); - if (_9.ok) { - return rt.Ok([delta, _9.value] as [rt.i64, rt.u64]); +export function frameHeader(r: Decoder.Decoder): rt.i64 | undefined { + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const delta: rt.i64 = unzigzag(part as rt.u64); + const part2: rt.u64 | undefined = Decoder.varint(r); + const err2: DecodeError.DecodeError | undefined = part2 === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err2 === undefined) { + frameHeaderOut.p_1 = part2 as rt.u64; + return delta; } - return _9; + frameHeaderOut.err = err2 as DecodeError.DecodeError; + return undefined; } - return _3; + frameHeaderOut.err = err as DecodeError.DecodeError; + return undefined; } +export const frameHeaderOut: { err: unknown; p_1: rt.u64 } = { err: undefined, p_1: undefined as never }; + /** One size-prefixed GROUP header, starting at the first frame. */ export function encodeGroup(data: rt.ByteSlice, version: lite_version_Version.Version, out: rt.ByteVec): rt.Result { const _6: [rt.u64, rt.ByteSlice] = takeU64(data); diff --git a/ts/fixtures/codec/expected/coding/decode.Decoder.ts b/ts/fixtures/codec/expected/coding/decode.Decoder.ts index 9b0ad40cb..d228e84ad 100644 --- a/ts/fixtures/codec/expected/coding/decode.Decoder.ts +++ b/ts/fixtures/codec/expected/coding/decode.Decoder.ts @@ -103,7 +103,7 @@ export function sub(self: Decoder, len: rt.usize): rt.Result { +export function u8(self: Decoder): rt.u8 | undefined { const err: DecodeError.DecodeError | undefined = slice(self, 1); const part: Uint8Array = sliceOut.pData; const part2: number = sliceOut.pStart; @@ -112,13 +112,16 @@ export function u8(self: Decoder): rt.Result { if (0 >= part3) { rt.panic("index out of bounds: the len is " + part3 + " but the index is " + 0); } - return rt.Ok(part[part2]); + return part[part2]; } - return rt.Err(err as DecodeError.DecodeError); + u8Out.err = err as DecodeError.DecodeError; + return undefined; } +export const u8Out: { err: unknown } = { err: undefined }; + /** Read a big-endian `u16`. */ -export function u16(self: Decoder): rt.Result { +export function u16(self: Decoder): rt.u16 | undefined { const err: DecodeError.DecodeError | undefined = slice(self, 2); const part: Uint8Array = sliceOut.pData; const part2: number = sliceOut.pStart; @@ -131,30 +134,40 @@ export function u16(self: Decoder): rt.Result { if (1 >= part3) { rt.panic("index out of bounds: the len is " + part3 + " but the index is " + 1); } - return rt.Ok((_9 << 8) | part[part2 + 1]); + return (_9 << 8) | part[part2 + 1]; } - return rt.Err(err as DecodeError.DecodeError); + u16Out.err = err as DecodeError.DecodeError; + return undefined; } +export const u16Out: { err: unknown } = { err: undefined }; + /** Read a byte that must be 0 or 1. */ -export function bool(self: Decoder): rt.Result { - const _2: rt.Result = u8(self); - if (_2.ok) { - const val: rt.u8 = _2.value; +export function bool(self: Decoder): DecodeError.DecodeError | undefined { + const part: rt.u8 | undefined = u8(self); + const err: DecodeError.DecodeError | undefined = part === undefined ? u8Out.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const val: rt.u8 = part as rt.u8; if (val === 0) { - return rt.Ok(false); + boolOut.p = false; + return undefined; } if (val === 1) { - return rt.Ok(true); + boolOut.p = true; + return undefined; } - return rt.Err({ tag: "InvalidValue" } as DecodeError.DecodeError); + return { tag: "InvalidValue" } as DecodeError.DecodeError; } - return _2; + return err as DecodeError.DecodeError; } +export const boolOut: { p: boolean } = { p: undefined as never }; + /** Read a varint. */ -export function varint(self: Decoder): rt.Result { - let _0!: rt.Result; +export function varint(self: Decoder): rt.u64 | undefined { + let _0Err!: boolean; + let _02!: rt.u64; + let _03!: DecodeError.DecodeError; const part: rt.u64 | undefined = read(self.bufData, self.bufStart, self.bufLen, self.form); const err2: DecodeError.DecodeError | undefined = part === undefined ? readOut.err as DecodeError.DecodeError : undefined; const part2: Uint8Array = readOut.p_1Data; @@ -165,22 +178,34 @@ export function varint(self: Decoder): rt.Result { - const _3: rt.Result = varint(self); - if (_3.ok) { - return rt.Ok(rt.u64CheckedSub(_3.value, 1)); + const part: rt.u64 | undefined = varint(self); + const err: DecodeError.DecodeError | undefined = part === undefined ? varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + return rt.Ok(rt.u64CheckedSub(part as rt.u64, 1)); } - return _3; + return rt.Err(err as DecodeError.DecodeError); } /** Read a varint length, then that many raw bytes. */ @@ -189,27 +214,28 @@ export function bytes(self: Decoder): DecodeError.DecodeError | undefined { const startData: Uint8Array = self.bufData; const startStart: number = self.bufStart; const startLen: number = self.bufLen; - const _6: rt.Result = varint(self); - if (_6.ok) { - const _3: rt.Result = result_mapErr$06mnjtr(rt.usizeTryFrom(_6.value), (_2: rt.TryFromIntError): DecodeError.DecodeError => { + const part: rt.u64 | undefined = varint(self); + const err: DecodeError.DecodeError | undefined = part === undefined ? varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const _3: rt.Result = result_mapErr$06mnjtr(rt.usizeTryFrom(part as rt.u64), (_2: rt.TryFromIntError): DecodeError.DecodeError => { return { tag: "BoundsExceeded" } as DecodeError.DecodeError; }); if (_3.ok) { - const err: DecodeError.DecodeError | undefined = slice(self, _3.value); - const part: Uint8Array = sliceOut.pData; - const part2: number = sliceOut.pStart; - const part3: number = sliceOut.pLen; + const err2: DecodeError.DecodeError | undefined = slice(self, _3.value); + const part2: Uint8Array = sliceOut.pData; + const part3: number = sliceOut.pStart; + const part4: number = sliceOut.pLen; const _15: (a0: DecodeError.DecodeError) => undefined = (_22: DecodeError.DecodeError): undefined => { self.bufData = startData; self.bufStart = startStart; self.bufLen = startLen; }; - _0 = inspectErr$156koiv((err !== undefined ? rt.Err(err as DecodeError.DecodeError) : rt.Ok(new rt.ByteSlice(part, part2, part3))) as rt.Result, _15); + _0 = inspectErr$156koiv((err2 !== undefined ? rt.Err(err2 as DecodeError.DecodeError) : rt.Ok(new rt.ByteSlice(part2, part3, part4))) as rt.Result, _15); } else { _0 = _3; } } else { - _0 = _6; + _0 = rt.Err(err as DecodeError.DecodeError); } const result: rt.Result = _0; if (!result.ok) { diff --git a/ts/fixtures/codec/expected/coding/varint.ts b/ts/fixtures/codec/expected/coding/varint.ts index 0149bc5d3..18b9b7f8e 100644 --- a/ts/fixtures/codec/expected/coding/varint.ts +++ b/ts/fixtures/codec/expected/coding/varint.ts @@ -16,78 +16,66 @@ export function fromHalves(hi: rt.u32, lo: rt.u32): rt.u64 { } /** The bytes `value` takes on the wire in `form`, or [`BoundsExceeded`] if it does not fit. */ -export function size(value: rt.u64, form: Form.Form): rt.Result { - let _6!: rt.usize; +export function size(value: rt.u64, form: Form.Form): rt.usize | undefined { const _5: [rt.u32, rt.u32] = toHalves(value); - b1: { - const hi: rt.u32 = _5[0]; - const lo: rt.u32 = _5[1]; - if (form.tag === "Quic") { - if (hi === 0) { - if (lo < 64) { - _6 = 1; - break b1; - } - } - if (hi === 0) { - if (lo < 16384) { - _6 = 2; - break b1; - } - } - if (hi === 0) { - if (lo < 1073741824) { - _6 = 4; - break b1; - } - } - if (!(hi < 1073741824)) { - return rt.Err({}); - } - _6 = 8; - } else { - if (hi === 0) { - if (lo < 128) { - _6 = 1; - break b1; - } - } - if (hi === 0) { - if (lo < 16384) { - _6 = 2; - break b1; - } + const hi: rt.u32 = _5[0]; + const lo: rt.u32 = _5[1]; + if (form.tag === "Quic") { + if (hi === 0) { + if (lo < 64) { + return 1; } - if (hi === 0) { - if (lo < 2097152) { - _6 = 3; - break b1; - } + } + if (hi === 0) { + if (lo < 16384) { + return 2; } - if (hi === 0) { - if (lo < 268435456) { - _6 = 4; - break b1; - } + } + if (hi === 0) { + if (lo < 1073741824) { + return 4; } - if (!(hi < 8)) { - if (!(hi < 1024)) { - if (!(hi < 16777216)) { - _6 = 9; - } else { - _6 = 8; - } - } else { - _6 = 6; - } - } else { - _6 = 5; + } + if (!(hi < 1073741824)) { + sizeOut.err = {}; + return undefined; + } + return 8; + } + if (hi === 0) { + if (lo < 128) { + return 1; + } + } + if (hi === 0) { + if (lo < 16384) { + return 2; + } + } + if (hi === 0) { + if (lo < 2097152) { + return 3; + } + } + if (hi === 0) { + if (lo < 268435456) { + return 4; + } + } + if (!(hi < 8)) { + if (!(hi < 1024)) { + if (!(hi < 16777216)) { + return 9; } + return 8; } + return 6; } - return rt.Ok(_6); + return 5; } +export const sizeOut: { err: unknown } = { err: undefined }; + /** * Append the minimal encoding of `value` in `form`. * diff --git a/ts/fixtures/codec/expected/coding/version.Version.ts b/ts/fixtures/codec/expected/coding/version.Version.ts index cbb29e61f..8bb6565c7 100644 --- a/ts/fixtures/codec/expected/coding/version.Version.ts +++ b/ts/fixtures/codec/expected/coding/version.Version.ts @@ -15,11 +15,12 @@ export interface Version { /** Decode the version number. */ export function decode$coding_decode_Decode_coding_varint_Form(r: Decoder.Decoder, _2: Form.Form): rt.Result { - const _4: rt.Result = Decoder.varint(r); - if (_4.ok) { - return rt.Ok({ _0: _4.value }); + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + return rt.Ok({ _0: part as rt.u64 }); } - return _4; + return rt.Err(err as DecodeError.DecodeError); } /** Encode the value to the given encoder, which may hold partial output on error. */ diff --git a/ts/fixtures/codec/expected/coding/version.Versions.ts b/ts/fixtures/codec/expected/coding/version.Versions.ts index 666469815..897ece610 100644 --- a/ts/fixtures/codec/expected/coding/version.Versions.ts +++ b/ts/fixtures/codec/expected/coding/version.Versions.ts @@ -26,9 +26,10 @@ export interface Versions { /** Decode the version list. */ export function decode$coding_decode_Decode_coding_varint_Form(r: Decoder.Decoder, version: Form.Form): rt.Result { - const _3: rt.Result = Decoder.varint(r); - if (_3.ok) { - const count: rt.u64 = _3.value; + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const count: rt.u64 = part as rt.u64; const vs: coding_version_Version.Version[] = []; const iter: rt.Range = new rt.Range(0, count, rt.u64Step); while (iter.next() !== rt.DONE) { @@ -41,7 +42,7 @@ export function decode$coding_decode_Decode_coding_varint_Form(r: Decoder.Decode } return rt.Ok({ _0: vs }); } - return _3; + return rt.Err(err as DecodeError.DecodeError); } /** Encode the version list. */ diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index 9387c42a1..2344bb700 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -6,7 +6,7 @@ import * as Decoder from "./coding/decode.Decoder.ts"; import * as EncodeError from "./coding/encode.EncodeError.ts"; import * as Encoder from "./coding/encode.Encoder.ts"; import type * as Prefix from "./coding/encode.Prefix.ts"; -import { size, zigzag as varint_zigzag, unzigzag } from "./coding/varint.ts"; +import { size, sizeOut, zigzag as varint_zigzag, unzigzag } from "./coding/varint.ts"; import * as BoundsExceeded from "./coding/varint.BoundsExceeded.ts"; import * as Form from "./coding/varint.Form.ts"; import * as Versions from "./coding/version.Versions.ts"; @@ -223,32 +223,36 @@ export function decodeScript(dataData: Uint8Array, dataStart: number, dataLen: n export function decodeStep(r: Decoder.Decoder, step: rt.u8, out: rt.ByteVec): rt.Result { const _5: rt.u8 = rt.u8Rem(step, 10); if (_5 === 0) { - const _7: rt.Result = Decoder.u8(r); - if (_7.ok) { - rt.byteVecPush(out, _7.value); + const part11: rt.u8 | undefined = Decoder.u8(r); + const err7: DecodeError.DecodeError | undefined = part11 === undefined ? Decoder.u8Out.err as DecodeError.DecodeError : undefined; + if (err7 === undefined) { + rt.byteVecPush(out, part11 as rt.u8); } else { - return _7; + return rt.Err(err7 as DecodeError.DecodeError); } } else if (_5 === 1) { - const _15: rt.Result = Decoder.u16(r); - if (_15.ok) { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(_15.value))); + const part10: rt.u16 | undefined = Decoder.u16(r); + const err6: DecodeError.DecodeError | undefined = part10 === undefined ? Decoder.u16Out.err as DecodeError.DecodeError : undefined; + if (err6 === undefined) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(part10 as rt.u16))); } else { - return _15; + return rt.Err(err6 as DecodeError.DecodeError); } } else if (_5 === 2) { - const _21: rt.Result = Decoder.bool(r); - if (_21.ok) { - rt.byteVecPush(out, _21.value ? 1 : 0); + const err5: DecodeError.DecodeError | undefined = Decoder.bool(r); + const part9: boolean = Decoder.boolOut.p; + if (err5 === undefined) { + rt.byteVecPush(out, part9 ? 1 : 0); } else { - return _21; + return rt.Err(err5 as DecodeError.DecodeError); } } else if (_5 === 3) { - const _26: rt.Result = Decoder.varint(r); - if (_26.ok) { - putU64(out, _26.value); + const part8: rt.u64 | undefined = Decoder.varint(r); + const err4: DecodeError.DecodeError | undefined = part8 === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err4 === undefined) { + putU64(out, part8 as rt.u64); } else { - return _26; + return rt.Err(err4 as DecodeError.DecodeError); } } else if (_5 === 4) { const _31: rt.Result = Decoder.varintOpt(r); @@ -265,15 +269,15 @@ export function decodeStep(r: Decoder.Decoder, step: rt.u8, out: rt.ByteVec): rt return _31; } } else if (_5 === 5) { - const err2: DecodeError.DecodeError | undefined = Decoder.bytes(r); - const part4: Uint8Array = Decoder.sliceOut.pData; - const part5: number = Decoder.sliceOut.pStart; - const part6: number = Decoder.sliceOut.pLen; - if (err2 === undefined) { - putUsize(out, part6); - rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part4, part5, part6)); + const err3: DecodeError.DecodeError | undefined = Decoder.bytes(r); + const part5: Uint8Array = Decoder.sliceOut.pData; + const part6: number = Decoder.sliceOut.pStart; + const part7: number = Decoder.sliceOut.pLen; + if (err3 === undefined) { + putUsize(out, part7); + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part5, part6, part7)); } else { - return rt.Err(err2 as DecodeError.DecodeError); + return rt.Err(err3 as DecodeError.DecodeError); } } else if (_5 === 6) { const _49: rt.Result = Decoder.string(r); @@ -283,25 +287,26 @@ export function decodeStep(r: Decoder.Decoder, step: rt.u8, out: rt.ByteVec): rt return _49; } } else if (_5 === 7) { - const err: DecodeError.DecodeError | undefined = Decoder.slice(r, rt.u8Div(step, 10) as rt.usize); - const part: Uint8Array = Decoder.sliceOut.pData; - const part2: number = Decoder.sliceOut.pStart; - const part3: number = Decoder.sliceOut.pLen; - if (err === undefined) { - rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part, part2, part3)); + const err2: DecodeError.DecodeError | undefined = Decoder.slice(r, rt.u8Div(step, 10) as rt.usize); + const part2: Uint8Array = Decoder.sliceOut.pData; + const part3: number = Decoder.sliceOut.pStart; + const part4: number = Decoder.sliceOut.pLen; + if (err2 === undefined) { + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part2, part3, part4)); } else { - return rt.Err(err as DecodeError.DecodeError); + return rt.Err(err2 as DecodeError.DecodeError); } } else if (_5 === 8) { const _67: rt.Result = Decoder.sub(r, rt.u8Div(step, 10) as rt.usize); if (_67.ok) { const sub: Decoder.Decoder = _67.value; - const _76: rt.Result = Decoder.varint(sub); - if (_76.ok) { - putU64(out, _76.value); + const part: rt.u64 | undefined = Decoder.varint(sub); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + putU64(out, part as rt.u64); putUsize(out, Decoder.rest(sub).length); } else { - return _76; + return rt.Err(err as DecodeError.DecodeError); } } else { return _67; @@ -425,9 +430,9 @@ export function sizes(data: rt.ByteSlice, out: rt.ByteVec): void { let iterStart: rt.u8 = 0; const iterEnd: rt.u8 = 3; while ((_9 = iterStart++) < iterEnd) { - const _13: rt.Result = size(value, form(_9)); - if (_13.ok) { - putUsize(out, _13.value); + const part: rt.usize | undefined = size(value, form(_9)); + if ((part === undefined ? sizeOut.err as BoundsExceeded.BoundsExceeded : undefined) === undefined) { + putUsize(out, part as rt.usize); } else { rt.byteVecPush(out, 255); } diff --git a/ts/fixtures/codec/expected/lite/group.Group.ts b/ts/fixtures/codec/expected/lite/group.Group.ts index 36e26a6a0..20bd6e2a6 100644 --- a/ts/fixtures/codec/expected/lite/group.Group.ts +++ b/ts/fixtures/codec/expected/lite/group.Group.ts @@ -29,27 +29,30 @@ export interface Group { /** Decode a message body (without size prefix). */ export function decodeMsg$lite_message_Message(r: Decoder.Decoder, version: lite_version_Version.Version): rt.Result { let frameStart!: rt.u64; - const _3: rt.Result = Decoder.varint(r); - if (_3.ok) { - const subscribe: rt.u64 = _3.value; - const _8: rt.Result = Decoder.varint(r); - if (_8.ok) { - const sequence: rt.u64 = _8.value; + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const subscribe: rt.u64 = part as rt.u64; + const part2: rt.u64 | undefined = Decoder.varint(r); + const err2: DecodeError.DecodeError | undefined = part2 === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err2 === undefined) { + const sequence: rt.u64 = part2 as rt.u64; if (!lite_version_Version.hasFrameBounds(version)) { frameStart = 0; } else { - const _15: rt.Result = Decoder.varint(r); - if (_15.ok) { - frameStart = _15.value; + const part3: rt.u64 | undefined = Decoder.varint(r); + const err3: DecodeError.DecodeError | undefined = part3 === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err3 === undefined) { + frameStart = part3 as rt.u64; } else { - return _15; + return rt.Err(err3 as DecodeError.DecodeError); } } return rt.Ok({ subscribe, sequence, frameStart }); } - return _8; + return rt.Err(err2 as DecodeError.DecodeError); } - return _3; + return rt.Err(err as DecodeError.DecodeError); } /** Encode this message body (without size prefix). */ diff --git a/ts/fixtures/codec/expected/lite/message.ts b/ts/fixtures/codec/expected/lite/message.ts index aed4a6a7d..d3569d707 100644 --- a/ts/fixtures/codec/expected/lite/message.ts +++ b/ts/fixtures/codec/expected/lite/message.ts @@ -3,25 +3,29 @@ import * as rt from "../rt.ts"; import * as DecodeError from "../coding/decode.DecodeError.ts"; import * as Decoder from "../coding/decode.Decoder.ts"; +import { sizeOut } from "../coding/varint.ts"; import { mapErr$0577706 as result_mapErr$0577706, mapErr$04g1m7s as result_mapErr$04g1m7s, andThen$0hcfsqc as result_andThen$0hcfsqc, andThen$1kvlask as result_andThen$1kvlask, unwrapOr$0jeml8j as result_unwrapOr$0jeml8j } from "../core/result.ts"; import type * as Group from "../lite/group.Group.ts"; import type * as Probe from "../lite/probe.Probe.ts"; import type * as Control from "../stats.Control.ts"; /** Read a lite message's varint size prefix, refusing one past `max`. */ -export function decodeSize(r: Decoder.Decoder, max: rt.usize): rt.Result { - const _3: rt.Result = Decoder.varint(r); - if (_3.ok) { - const size: rt.u64 = _3.value; +export function decodeSize(r: Decoder.Decoder, max: rt.usize): rt.usize | undefined { + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const size: rt.u64 = part as rt.u64; const _8: rt.Result = rt.usizeTryFrom(size); if (_8.ok) { if (_8.value <= max) { - return _8 as rt.Result; + return _8.value; } } - return rt.Err({ tag: "MessageTooLarge", size: result_unwrapOr$0jeml8j(rt.usizeTryFrom(size), 4294967295), max } as DecodeError.DecodeError); + sizeOut.err = { tag: "MessageTooLarge", size: result_unwrapOr$0jeml8j(rt.usizeTryFrom(size), 4294967295), max } as DecodeError.DecodeError; + return undefined; } - return _3; + sizeOut.err = err as DecodeError.DecodeError; + return undefined; } /** @@ -29,9 +33,10 @@ export function decodeSize(r: Decoder.Decoder, max: rt.usize): rt.Result rt.Result): rt.Result { - const _3: rt.Result = decodeSize(r, 65535); - if (_3.ok) { - const _9: rt.Result = Decoder.sub(r, _3.value); + const part: rt.usize | undefined = decodeSize(r, 65535); + const err: DecodeError.DecodeError | undefined = part === undefined ? sizeOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const _9: rt.Result = Decoder.sub(r, part as rt.usize); if (_9.ok) { const body: Decoder.Decoder = _9.value; const result: rt.Result = result_andThen$0hcfsqc(result_mapErr$0577706(decode(body), DecodeError.complete), (msg: Group.Group): rt.Result => { @@ -47,7 +52,7 @@ export function decodeWith$lite_group_Group_coding_decode_Decode_lite_version_Ve } return _9; } - return _3; + return rt.Err(err as DecodeError.DecodeError); } /** @@ -55,9 +60,10 @@ export function decodeWith$lite_group_Group_coding_decode_Decode_lite_version_Ve * [`Message::decode_msg`], for a body whose bounds come from the session. */ export function decodeWith$lite_probe_Probe_coding_decode_Decode_lite_version_Version_T_decode_closure0_lite_probe_Probe(r: Decoder.Decoder, decode: (a0: Decoder.Decoder) => rt.Result): rt.Result { - const _3: rt.Result = decodeSize(r, 65535); - if (_3.ok) { - const _9: rt.Result = Decoder.sub(r, _3.value); + const part: rt.usize | undefined = decodeSize(r, 65535); + const err: DecodeError.DecodeError | undefined = part === undefined ? sizeOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const _9: rt.Result = Decoder.sub(r, part as rt.usize); if (_9.ok) { const body: Decoder.Decoder = _9.value; const result: rt.Result = result_andThen$1kvlask(result_mapErr$04g1m7s(decode(body), DecodeError.complete), (msg: Probe.Probe): rt.Result => { @@ -73,7 +79,7 @@ export function decodeWith$lite_probe_Probe_coding_decode_Decode_lite_version_Ve } return _9; } - return _3; + return rt.Err(err as DecodeError.DecodeError); } //# sourceMappingURL=message.ts.map diff --git a/ts/fixtures/codec/expected/lite/probe.Probe.ts b/ts/fixtures/codec/expected/lite/probe.Probe.ts index d2a0dda2a..b33486840 100644 --- a/ts/fixtures/codec/expected/lite/probe.Probe.ts +++ b/ts/fixtures/codec/expected/lite/probe.Probe.ts @@ -66,9 +66,10 @@ export function decodeMsg$lite_message_Message(r: Decoder.Decoder, version: lite if (version === "Lite01" || version === "Lite02") { return rt.Err({ tag: "Version" } as DecodeError.DecodeError); } - const _6: rt.Result = Decoder.varint(r); - if (_6.ok) { - const val: rt.u64 = _6.value; + const part: rt.u64 | undefined = Decoder.varint(r); + const err: DecodeError.DecodeError | undefined = part === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err === undefined) { + const val: rt.u64 = part as rt.u64; if (val === 0) { bitrate = undefined; } else { @@ -77,21 +78,22 @@ export function decodeMsg$lite_message_Message(r: Decoder.Decoder, version: lite if (!lite_version_Version.hasProbeRtt(version)) { rtt = undefined; } else { - const _13: rt.Result = Decoder.varint(r); - if (_13.ok) { - const val2: rt.u64 = _13.value; + const part2: rt.u64 | undefined = Decoder.varint(r); + const err2: DecodeError.DecodeError | undefined = part2 === undefined ? Decoder.varintOut.err as DecodeError.DecodeError : undefined; + if (err2 === undefined) { + const val2: rt.u64 = part2 as rt.u64; if (val2 === 0) { rtt = undefined; } else { rtt = val2; } } else { - return _13; + return rt.Err(err2 as DecodeError.DecodeError); } } return rt.Ok({ bitrate, rtt }); } - return _6; + return rt.Err(err as DecodeError.DecodeError); } /** Encode this message body (without size prefix). */ diff --git a/ts/fixtures/lang/expected/cells.ts b/ts/fixtures/lang/expected/cells.ts index bf9e6e2d6..d2be53587 100644 --- a/ts/fixtures/lang/expected/cells.ts +++ b/ts/fixtures/lang/expected/cells.ts @@ -518,20 +518,24 @@ export function cellU64(dataData: Uint8Array, dataStart: number, dataLen: number const _166: rt.u8 = rt.u8Rem(v2, 4); if (_166 === 0 || _166 === 1) { result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u64(make, v2); + const part4 = made$u64(make, v2); + return part4 === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part4); }); } else if (_166 === 2) { const _174: rt.OnceCell = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { rt.onceSet(_174, make(v2 ^ 1)); - return made$u64(make, v2); + const part3 = made$u64(make, v2); + return part3 === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part3); }); } else { const _179: rt.OnceCell = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u64(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$0h1vl84(rt.onceGetOrTryInit(_179, (): rt.Result => { - return made$u64(make, v2 ^ 1); + const part2 = made$u64(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$0h1vl84(rt.onceGetOrTryInit(_179, (): rt.Result => { + const part = made$u64(make, v2 ^ 1); + return part === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part); }), (_22: rt.u64): undefined => {})), v2)); + return part2 === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part2); }); } if (result.ok) { @@ -702,20 +706,24 @@ export function cellU8(dataData: Uint8Array, dataStart: number, dataLen: number, const _166: rt.u8 = rt.u8Rem(v2, 4); if (_166 === 0 || _166 === 1) { result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u8(make, v2); + const part4 = made$u8(make, v2); + return part4 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part4); }); } else if (_166 === 2) { const _174: rt.OnceCell = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { rt.onceSet(_174, make(v2 ^ 1)); - return made$u8(make, v2); + const part3 = made$u8(make, v2); + return part3 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part3); }); } else { const _179: rt.OnceCell = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$03hz3l7(rt.onceGetOrTryInit(_179, (): rt.Result => { - return made$u8(make, v2 ^ 1); + const part2 = made$u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$03hz3l7(rt.onceGetOrTryInit(_179, (): rt.Result => { + const part = made$u8(make, v2 ^ 1); + return part === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part); }), (_22: rt.u8): undefined => {})), v2)); + return part2 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part2); }); } if (result.ok) { @@ -1646,20 +1654,24 @@ export function lockU64(dataData: Uint8Array, dataStart: number, dataLen: number const _166: rt.u8 = rt.u8Rem(v2, 4); if (_166 === 0 || _166 === 1) { result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u64(make, v2); + const part4 = made$u64(make, v2); + return part4 === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part4); }); } else if (_166 === 2) { const _174: rt.OnceLock = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { rt.onceSet(_174, make(v2 ^ 1)); - return made$u64(make, v2); + const part3 = made$u64(make, v2); + return part3 === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part3); }); } else { const _179: rt.OnceLock = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u64(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$0h1vl84(rt.onceGetOrTryInit(_179, (): rt.Result => { - return made$u64(make, v2 ^ 1); + const part2 = made$u64(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$0h1vl84(rt.onceGetOrTryInit(_179, (): rt.Result => { + const part = made$u64(make, v2 ^ 1); + return part === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part); }), (_22: rt.u64): undefined => {})), v2)); + return part2 === undefined ? rt.Err(madeOut$u64.err as rt.u8) : rt.Ok(part2); }); } if (result.ok) { @@ -1830,20 +1842,24 @@ export function lockU8(dataData: Uint8Array, dataStart: number, dataLen: number, const _166: rt.u8 = rt.u8Rem(v2, 4); if (_166 === 0 || _166 === 1) { result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u8(make, v2); + const part4 = made$u8(make, v2); + return part4 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part4); }); } else if (_166 === 2) { const _174: rt.OnceLock = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { rt.onceSet(_174, make(v2 ^ 1)); - return made$u8(make, v2); + const part3 = made$u8(make, v2); + return part3 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part3); }); } else { const _179: rt.OnceLock = cell; result = rt.onceGetOrTryInit(cell, (): rt.Result => { - return made$u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$03hz3l7(rt.onceGetOrTryInit(_179, (): rt.Result => { - return made$u8(make, v2 ^ 1); + const part2 = made$u8(make, option_unwrapOr$0r2bmpr(err$1nbmqmx(result_map$03hz3l7(rt.onceGetOrTryInit(_179, (): rt.Result => { + const part = made$u8(make, v2 ^ 1); + return part === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part); }), (_22: rt.u8): undefined => {})), v2)); + return part2 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part2); }); } if (result.ok) { @@ -2316,19 +2332,21 @@ export function made$Vec_u8(make: (a0: rt.u8) => rt.ByteVec, v: rt.u8): rt.Resul } /** What `make` makes of `v`, or `v` back as an error for every third byte. */ -export function made$u64(make: (a0: rt.u8) => rt.u64, v: rt.u8): rt.Result { +export function made$u64(make: (a0: rt.u8) => rt.u64, v: rt.u8): rt.u64 | undefined { if (rt.u8Rem(v, 3) === 0) { - return rt.Err(v); + madeOut$u64.err = v; + return undefined; } - return rt.Ok(make(v)); + return make(v); } /** What `make` makes of `v`, or `v` back as an error for every third byte. */ -export function made$u8(make: (a0: rt.u8) => rt.u8, v: rt.u8): rt.Result { +export function made$u8(make: (a0: rt.u8) => rt.u8, v: rt.u8): rt.u8 | undefined { if (rt.u8Rem(v, 3) === 0) { - return rt.Err(v); + madeOut$u8.err = v; + return undefined; } - return rt.Ok(make(v)); + return make(v); } /** What `make` makes of `v`, or `v` back as an error for every third byte. */ @@ -2340,6 +2358,10 @@ export function made$unit(make: (a0: rt.u8) => undefined, v: rt.u8): rt.Result { + 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 { if (self === undefined) { return rt.Err(err); diff --git a/ts/fixtures/lang/expected/core/result.ts b/ts/fixtures/lang/expected/core/result.ts index eb059757c..1e8a81ea9 100644 --- a/ts/fixtures/lang/expected/core/result.ts +++ b/ts/fixtures/lang/expected/core/result.ts @@ -641,6 +641,13 @@ export function map$1g6jln5(self: rt.Result, rt.RefCell, rt.RefCell>, rt.RefCell>; } +export function map$1rffkv6(self: rt.Result, _: (a0: rt.u32) => rt.u64): rt.Result { + if (self.ok) { + return rt.Ok(self.value as rt.u64); + } + return self as rt.Result; +} + export function map$0r57egs(self: rt.Result, op: (a0: rt.u8) => rt.u8 | undefined): rt.Result { if (self.ok) { return rt.Ok(op(self.value)); diff --git a/ts/fixtures/lang/expected/results.ts b/ts/fixtures/lang/expected/results.ts index e93c1f84f..8cd77157f 100644 --- a/ts/fixtures/lang/expected/results.ts +++ b/ts/fixtures/lang/expected/results.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { copy$Result_array0_u8_u8, copy$Result_array1_u8_u8, copy$Result_array5_u8_u8, drop$Result_results_Logged_results_Logged } from "./lib.ts"; +import { madeOut$u8 } from "./cells.ts"; import { unwrapOr$17ntxfo as option_unwrapOr$17ntxfo, unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr, mapOr$1w5qtj7 as option_mapOr$1w5qtj7, mapOr$02k4ito as option_mapOr$02k4ito } from "./core/option.ts"; import { map$03hz3l7 as result_map$03hz3l7 } from "./core/result.ts"; import * as results_Logged from "./results.Logged.ts"; @@ -27,17 +28,19 @@ export function results(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void } /** `Ok` for a byte not divisible by 3, else `Err`. */ -export function split(b: rt.u8): rt.Result { +export function split(b: rt.u8): rt.u8 | undefined { if (!(rt.u8Rem(b, 3) === 0)) { - return rt.Ok(b); + return b; } - return rt.Err(b); + madeOut$u8.err = b; + return undefined; } /** Of bytes, which JS copies, so a `&mut` lends each as its place. */ export function copied(data: rt.ByteSlice, out: rt.ByteVec): void { const results: rt.Result[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): rt.Result => { - return split(_2); + const part = split(_2); + return part === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part); }), undefined, 2, true); for (const _9 of results) { rt.iterExtendBytes(out, rt.iterResult(_9), undefined, true); @@ -232,7 +235,9 @@ export function owned(data: rt.ByteSlice, out: rt.ByteVec): void { /** Of `()`, which JS holds as `undefined`, so an `Option` of one is `rt.None`. */ export function units(data: rt.ByteSlice, out: rt.ByteVec): void { const results: rt.Result[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): rt.Result => { - return result_map$03hz3l7(split(_2), (_24: rt.u8): undefined => {}); + const part: rt.u8 | undefined = split(_2); + const err: rt.u8 | undefined = part === undefined ? madeOut$u8.err as rt.u8 : undefined; + return result_map$03hz3l7((err !== undefined ? rt.Err(err as rt.u8) : rt.Ok(part as rt.u8)) as rt.Result, (_24: rt.u8): undefined => {}); }), undefined, 2, true); rt.byteVecPush(out, rt.iterCount(rt.iterFlatten(rt.iterSlice(results), (value: rt.Result) => rt.iterResult(value), false, undefined, true)) & 0xff); const iter: rt.Iter>> = rt.iterSliceMut(results); diff --git a/ts/fixtures/lang/expected/returns.Late.ts b/ts/fixtures/lang/expected/returns.Late.ts new file mode 100644 index 000000000..cd4137359 --- /dev/null +++ b/ts/fixtures/lang/expected/returns.Late.ts @@ -0,0 +1,29 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { madeOut$u64 } from "./cells.ts"; +import { big } from "./returns.ts"; +import type * as returns_Error from "./returns.Error.ts"; + +/** A guard whose drop writes the slot after its function's call did. */ +export class Late { + _0Data: Uint8Array; + _0Start: number; + _0Len: number; + + constructor(_0Data: Uint8Array, _0Start: number, _0Len: number) { + this._0Data = _0Data; + this._0Start = _0Start; + this._0Len = _0Len; + } + + [Symbol.dispose](): void { + drop$ops_Drop(this); + } +} + +export function drop$ops_Drop(self: Late): void { + big(new rt.ByteSlice(self._0Data, self._0Start, self._0Len)) === undefined ? madeOut$u64.err as returns_Error.Error : undefined; +} + +//# sourceMappingURL=returns.Late.ts.map diff --git a/ts/fixtures/lang/expected/returns.ts b/ts/fixtures/lang/expected/returns.ts index 95fdb309a..b5aaaf9a4 100644 --- a/ts/fixtures/lang/expected/returns.ts +++ b/ts/fixtures/lang/expected/returns.ts @@ -1,12 +1,14 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { madeOut$u64, madeOut$u8 } from "./cells.ts"; import { then$0w2ymju } from "./core/bool.ts"; -import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr, map$0c2keyb as option_map$0c2keyb, map$1yogfym as option_map$1yogfym, okOr$0gtm15y as option_okOr$0gtm15y, okOr$12p77jh as option_okOr$12p77jh, okOr$0tezn0w as option_okOr$0tezn0w } from "./core/option.ts"; -import { mapOr$0fbyajt as result_mapOr$0fbyajt, andThen$1yropo4 as result_andThen$1yropo4 } from "./core/result.ts"; +import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr, map$0c2keyb as option_map$0c2keyb, map$1yogfym as option_map$1yogfym, okOr$1b6nkwr as option_okOr$1b6nkwr, okOr$0gtm15y as option_okOr$0gtm15y, okOr$12p77jh as option_okOr$12p77jh, okOr$0tezn0w as option_okOr$0tezn0w } from "./core/option.ts"; +import { map$1rffkv6 as result_map$1rffkv6, mapOr$0fbyajt as result_mapOr$0fbyajt, andThen$1yropo4 as result_andThen$1yropo4 } from "./core/result.ts"; import * as At from "./returns.At.ts"; import * as returns_Error from "./returns.Error.ts"; import * as Guard from "./returns.Guard.ts"; +import * as Late from "./returns.Late.ts"; import * as Split from "./returns.Split.ts"; import * as returns_Wrapped from "./returns.Wrapped.ts"; @@ -335,233 +337,492 @@ export function wrapped(dataData: Uint8Array, dataStart: number, dataLen: number export const wrappedOut: { err: unknown; p_1Data: Uint8Array; p_1Start: number; p_1Len: number } = { err: undefined, p_1Data: undefined as never, p_1Start: undefined as never, p_1Len: undefined as never }; -export function returns(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { - let f!: (a0: rt.ByteSlice) => rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>; - b0: { - const _4: rt.u8 = rt.u8Rem(kind, 7); - if (_4 === 0) { - const err16: returns_Error.Error | undefined = take(data.data, data.start, data.length, rt.usizeRem(kind as rt.usize, 7)); - const part65: Uint8Array = takeOut.pData; - const part66: number = takeOut.pStart; - const part67: number = takeOut.pLen; - push(out, (err16 !== undefined ? rt.Err(err16 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part65, part66, part67))) as rt.Result); - const err17: returns_Error.Error | undefined = take(data.data, data.start, data.length, 201); - const part68: Uint8Array = takeOut.pData; - const part69: number = takeOut.pStart; - const part70: number = takeOut.pLen; - push(out, (err17 !== undefined ? rt.Err(err17 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part68, part69, part70))) as rt.Result); - const err18: returns_Error.Error | undefined = tail(data, 3); - const part71: Uint8Array = takeOut.pData; - const part72: number = takeOut.pStart; - const part73: number = takeOut.pLen; - push(out, (err18 !== undefined ? rt.Err(err18 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part71, part72, part73))) as rt.Result); - const part74: rt.u64 | undefined = number(data.data, data.start, data.length, rt.u8Rem(kind, 2) === 0); - const err19: returns_Error.Error | undefined = part74 === undefined ? wideOut.err as returns_Error.Error : undefined; - const part75: Uint8Array = wideOut.p_1Data; - const part76: number = wideOut.p_1Start; - const part77: number = wideOut.p_1Len; - pushNumber(out, (err19 !== undefined ? rt.Err(err19 as returns_Error.Error) : rt.Ok([part74 as rt.u64, new rt.ByteSlice(part75, part76, part77)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); - const part78: rt.u64 | undefined = count(data.data, data.start, data.length); - const err20: returns_Error.Error | undefined = part78 === undefined ? wideOut.err as returns_Error.Error : undefined; - const part79: Uint8Array = wideOut.p_1Data; - const part80: number = wideOut.p_1Start; - const part81: number = wideOut.p_1Len; - pushNumber(out, (err20 !== undefined ? rt.Err(err20 as returns_Error.Error) : rt.Ok([part78 as rt.u64, new rt.ByteSlice(part79, part80, part81)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); - } else if (_4 === 1) { - const kept: rt.Result[] = rt.iterToArray(rt.iterMap(new rt.Range(0, 4), (n3: rt.usize): rt.Result => { - const err13 = take(data.data, data.start, data.length, rt.usizeMul(n3, 3)); - return err13 !== undefined ? rt.Err(err13 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(takeOut.pData, takeOut.pStart, takeOut.pLen)); - }), undefined, 12, true); - const err14: returns_Error.Error | undefined = tail(data, 1); - const part57: Uint8Array = takeOut.pData; - const part58: number = takeOut.pStart; - const part59: number = takeOut.pLen; - for (const _29 of kept) { - push(out, _29); - } - push(out, (err14 !== undefined ? rt.Err(err14 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part57, part58, part59))) as rt.Result); - const part60: rt.u64 | undefined = both(data.data, data.start, data.length); - const err15: returns_Error.Error | undefined = part60 === undefined ? bothOut.err as returns_Error.Error : undefined; - const part61: rt.u64 = bothOut.p_1; - const part64: number = bothOut.p_2Len; - if (err15 === undefined) { - const a: rt.u64 = part60 as rt.u64; - const b: rt.u64 = part61; - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(a))); - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(b))); - rt.byteVecPush(out, part64 & 0xff); - } else { - rt.byteVecPush(out, returns_Error.code(err15 as returns_Error.Error)); - } - } else if (_4 === 2) { - if (!(rt.usizeRem(data.length, 2) === 0)) { - f = (a02: rt.ByteSlice): rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> => { - const part51 = narrow(a02.data, a02.start, a02.length); - const result3: rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> = part51 === undefined ? rt.Err(wideOut.err as returns_Error.Error) : rt.Ok([part51, new rt.ByteSlice(wideOut.p_1Data, wideOut.p_1Start, wideOut.p_1Len)] as [rt.u64, rt.ByteSlice]); - return result3; - }; - } else { - f = (a0: rt.ByteSlice): rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> => { - const part50 = wide(a0.data, a0.start, a0.length); - const result2: rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> = part50 === undefined ? rt.Err(wideOut.err as returns_Error.Error) : rt.Ok([part50, new rt.ByteSlice(wideOut.p_1Data, wideOut.p_1Start, wideOut.p_1Len)] as [rt.u64, rt.ByteSlice]); - return result2; - }; - } - pushNumber(out, f(data)); - for (const _71 of rt.iterToArray(rt.iterMap(rt.iterSlice([data, rt.byteSliceRange(data, rt.min(data.length, 3))]), (a03: rt.ByteSlice): rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> => { - const part53 = count(a03.data, a03.start, a03.length); - const result4: rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> = part53 === undefined ? rt.Err(wideOut.err as returns_Error.Error) : rt.Ok([part53, new rt.ByteSlice(wideOut.p_1Data, wideOut.p_1Start, wideOut.p_1Len)] as [rt.u64, rt.ByteSlice]); - return result4; - }), undefined, 16, true)) { - pushNumber(out, _71); - } - rt.iterExtendBytes(out, rt.iterOption(option_map$0c2keyb(option_map$1yogfym((data.length !== 0 ? [data.data[data.start], new rt.ByteSlice(data.data, data.start + 1, data.length - 1)] as [rt.u8, rt.ByteSlice] : undefined) as [rt.u8, rt.ByteSlice] | undefined, (_22: [rt.u8, rt.ByteSlice]): rt.ByteSlice => { - return _22[1]; - }), (s2: rt.ByteSlice): rt.u8 => { - const err12: returns_Error.Error | undefined = tail(s2, 2); - const part54: Uint8Array = takeOut.pData; - const part55: number = takeOut.pStart; - const part56: number = takeOut.pLen; - return result_mapOr$0fbyajt((err12 !== undefined ? rt.Err(err12 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part54, part55, part56))) as rt.Result, 0, (a04: rt.ByteSlice): rt.usize => { - return a04.length; - }) & 0xff; - })), undefined, true); - } else if (_4 === 3) { - const err9: rt.u8 | undefined = halves(data.data, data.start, data.length); - const part35: number = halvesOut.p_0Len; - const part36: Uint8Array = halvesOut.p_1Data; - const part37: number = halvesOut.p_1Start; - const part38: number = halvesOut.p_1Len; - const part39: Uint8Array = halvesOut.p_2Data; - const part40: number = halvesOut.p_2Start; - const part41: number = halvesOut.p_2Len; - if (err9 === undefined) { - rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part36, part37, part38)); - rt.byteVecPush(out, part35 & 0xff); - rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part39, part40, part41)); - } else { - rt.byteVecPush(out, err9 as rt.u8); - } - const other: Uint8Array = Uint8Array.of(255, 1, 2); - const _96: rt.ByteSlice = rt.byteSliceFrom(other); - const part42: rt.u64 | undefined = guarded(data.data, data.start, data.length, _96); - const err10: returns_Error.Error | undefined = part42 === undefined ? wideOut.err as returns_Error.Error : undefined; - const part43: Uint8Array = wideOut.p_1Data; - const part44: number = wideOut.p_1Start; - const part45: number = wideOut.p_1Len; - pushNumber(out, (err10 !== undefined ? rt.Err(err10 as returns_Error.Error) : rt.Ok([part42 as rt.u64, new rt.ByteSlice(part43, part44, part45)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); - const _100: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(other), 1); - const part46: rt.u64 | undefined = guarded(data.data, data.start, data.length, _100); - const err11: returns_Error.Error | undefined = part46 === undefined ? wideOut.err as returns_Error.Error : undefined; - const part47: Uint8Array = wideOut.p_1Data; - const part48: number = wideOut.p_1Start; - const part49: number = wideOut.p_1Len; - pushNumber(out, (err11 !== undefined ? rt.Err(err11 as returns_Error.Error) : rt.Ok([part46 as rt.u64, new rt.ByteSlice(part47, part48, part49)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); - } else if (_4 === 4) { - const _104: rt.Result = unitErr(data.data, data.start, data.length); - if (_104.ok) { - rt.byteVecPush(out, _104.value.length & 0xff); - } else { - rt.byteVecPush(out, 238); - } - const _110: rt.Result = noneErr(data); - if (_110.ok) { - rt.byteVecPush(out, _110.value.length & 0xff); - } else { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(238, option_unwrapOr$0r2bmpr(_110.error, 0)))); +/** A number alone, past 2^53 from eight bytes. */ +export function big(data: rt.ByteSlice): rt.u64 | undefined { + if (!(data.length >= 8)) { + if (!((rt.byteSliceFirst(data) === 255))) { + madeOut$u64.err = { tag: "Short", _0: rt.usizeSub(8, data.length) } as returns_Error.Error; + return undefined; + } + madeOut$u64.err = { tag: "Bad" } as returns_Error.Error; + return undefined; + } + return rt.u64FromBeBytes(rt.byteSliceToArray(new rt.ByteSlice(data.data, data.start, 8))); +} + +/** A signed number and an unsigned one, each through `?`, at an offset. */ +export function pair(data: rt.ByteSlice, at: rt.usize): rt.i64 | undefined { + const _3: rt.Result = option_okOr$12p77jh(rt.byteSliceGetRange(data, at), { tag: "Short", _0: at } as returns_Error.Error); + if (_3.ok) { + const part: rt.u64 | undefined = big(_3.value); + const err: returns_Error.Error | undefined = part === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + if (err === undefined) { + const _18: rt.i64 = rt.intWrappingNeg(rt.i64Cast(part as rt.u64), 64, true); + const part3: rt.u64 | undefined = big(rt.byteSliceRange(_3.value, rt.min(_3.value.length, 3))); + const err2: returns_Error.Error | undefined = part3 === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + if (err2 === undefined) { + const tuple: [rt.i64, rt.u64] = [_18, rt.u64Shr(part3 as rt.u64, 3)]; + pairOut.p_1 = tuple[1]; + return tuple[0]; } - const part24: rt.u16 | undefined = lead$u16(data.data, data.start, data.length); - const err7: returns_Error.Error | undefined = part24 === undefined ? leadOut$u16.err as returns_Error.Error : undefined; - const part25: Uint8Array = leadOut$u16.p_1Data; - const part26: number = leadOut$u16.p_1Start; - const part27: number = leadOut$u16.p_1Len; - const _123: rt.ByteSlice = rt.byteSliceRange(data, rt.min(data.length, 1)); - const part29: rt.u32 | undefined = lead$u32(_123.data, _123.start, _123.length); - const err8: returns_Error.Error | undefined = part29 === undefined ? leadOut$u32.err as returns_Error.Error : undefined; - const part30: Uint8Array = leadOut$u32.p_1Data; - const part31: number = leadOut$u32.p_1Start; - const part32: number = leadOut$u32.p_1Len; - if (err7 === undefined) { - if (err8 === undefined) { - const a3: rt.u16 = part24 as rt.u16; - const b3: rt.u32 = part29 as rt.u32; - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(a3))); - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(b3))); - rt.byteVecPush(out, part32 & 0xff); - break b0; - } + pairOut.err = err2 as returns_Error.Error; + return undefined; + } + pairOut.err = err as returns_Error.Error; + return undefined; + } + pairOut.err = (_3 as { ok: false; error: returns_Error.Error }).error; + return undefined; +} + +export const pairOut: { err: unknown; p_1: rt.u64 } = { err: undefined, p_1: undefined as never }; + +/** A flag, which is no number to return, so it goes in the slot. */ +export function odd(dataData: Uint8Array, dataStart: number, dataLen: number): returns_Error.Error | undefined { + const part: rt.u64 | undefined = big(new rt.ByteSlice(dataData, dataStart, dataLen)); + const err: returns_Error.Error | undefined = part === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + if (err === undefined) { + const n: rt.u64 = part as rt.u64; + if (!(n === 7)) { + oddOut.p = rt.u64Rem(n, 2) === 1; + return undefined; + } + return { tag: "Short", _0: 7 } as returns_Error.Error; + } + return err as returns_Error.Error; +} + +export const oddOut: { p: boolean } = { p: undefined as never }; + +/** A number whose error is a number too. */ +export function byte(data: rt.ByteSlice, i: rt.usize): rt.u8 | undefined { + const _3: rt.u8 | undefined = rt.byteSliceGet(data, i); + if (_3 === undefined) { + madeOut$u8.err = i & 0xff; + return undefined; + } + if (!(_3 < 128)) { + madeOut$u8.err = _3; + return undefined; + } + return _3; +} + +/** A float and a char. */ +export function mixed(dataData: Uint8Array, dataStart: number, dataLen: number): rt.f64 | undefined { + const part: rt.u8 | undefined = byte(new rt.ByteSlice(dataData, dataStart, dataLen), 0); + const err: rt.u8 | undefined = part === undefined ? madeOut$u8.err as rt.u8 : undefined; + if (err === undefined) { + const b: rt.u8 = part as rt.u8; + const part2: rt.u8 | undefined = byte(new rt.ByteSlice(dataData, dataStart, dataLen), 1); + const err2: rt.u8 | undefined = part2 === undefined ? madeOut$u8.err as rt.u8 : undefined; + if (err2 === undefined) { + const _7: rt.Result = option_okOr$1b6nkwr(rt.charFromU32(rt.u32Add(rt.u32Mul((part2 as rt.u8) as rt.u32, 37), 55280)), b); + if (_7.ok) { + const tuple: [rt.f64, rt.char] = [-(b as rt.f64) / 3.0, _7.value]; + mixedOut.p_1 = tuple[1]; + return tuple[0]; } - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(((err7 !== undefined ? rt.Err(err7 as returns_Error.Error) : rt.Ok([part24 as rt.u16, new rt.ByteSlice(part25, part26, part27)] as [rt.u16, rt.ByteSlice])) as rt.Result<[rt.u16, rt.ByteSlice], returns_Error.Error>).ok ? 1 : 0, ((err8 !== undefined ? rt.Err(err8 as returns_Error.Error) : rt.Ok([part29 as rt.u32, new rt.ByteSlice(part30, part31, part32)] as [rt.u32, rt.ByteSlice])) as rt.Result<[rt.u32, rt.ByteSlice], returns_Error.Error>).ok ? 1 : 0))); - } else if (_4 === 5) { - const err5: returns_Error.Error | undefined = afterFixed(data.data, data.start, data.length); - const part17: Uint8Array = takeOut.pData; - const part18: number = takeOut.pStart; - const part19: number = takeOut.pLen; + mixedOut.err = (_7 as { ok: false; error: rt.u8 }).error; + return undefined; + } + mixedOut.err = err2 as rt.u8; + return undefined; + } + mixedOut.err = err as rt.u8; + return undefined; +} + +export const mixedOut: { err: unknown; p_1: rt.char } = { err: undefined, p_1: undefined as never }; + +/** Each byte before a zero counted, recursively, without the rest. */ +export function depth(dataData: Uint8Array, dataStart: number, dataLen: number): rt.u32 | undefined { + if (dataLen === 0) { + depthOut.err = { tag: "Short", _0: 1 } as returns_Error.Error; + return undefined; + } + if (dataData[dataStart] === 0) { + return 0; + } + const part: rt.u32 | undefined = depth(dataData, dataStart + 1, dataLen - 1); + const err: returns_Error.Error | undefined = part === undefined ? depthOut.err as returns_Error.Error : undefined; + if (err === undefined) { + return rt.u32Add(part as rt.u32, 1); + } + depthOut.err = err as returns_Error.Error; + return undefined; +} + +export const depthOut: { err: unknown } = { err: undefined }; + +/** What either returns, returned as is. */ +export function either(data: rt.ByteSlice, first: boolean): rt.u64 | undefined { + let _0!: rt.Result; + if (!first) { + const part: rt.u32 | undefined = depth(data.data, data.start, data.length); + const err: returns_Error.Error | undefined = part === undefined ? depthOut.err as returns_Error.Error : undefined; + _0 = result_map$1rffkv6((err !== undefined ? rt.Err(err as returns_Error.Error) : rt.Ok(part as rt.u32)) as rt.Result, (a0: rt.u32): rt.u64 => { + return a0 as rt.u64; + }); + } else { + return big(data); + } + const result: rt.Result = _0; + if (!result.ok) { + madeOut$u64.err = result.error; + return undefined; + } + return result.value; +} + +export function late(dataData: Uint8Array, dataStart: number, dataLen: number, other: rt.ByteSlice): rt.u64 | undefined { + const part: rt.u64 | undefined = big(new rt.ByteSlice(dataData, dataStart, dataLen)); + const err: returns_Error.Error | undefined = part === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + new Late.Late(other.data, other.start, other.length)[Symbol.dispose](); + if (err !== undefined) { + madeOut$u64.err = err as returns_Error.Error; + return undefined; + } + return part as rt.u64; +} + +export function lateSelf(dataData: Uint8Array, dataStart: number, dataLen: number): rt.u64 | undefined { + return late(dataData, dataStart, dataLen, new rt.ByteSlice(dataData, dataStart, dataLen)); +} + +export function pushBig(out: rt.ByteVec, r: rt.Result): void { + if (r.ok) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(r.value))); + } else { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(238, returns_Error.code(r.error)))); + } +} + +/** + * Results of numbers alone: held as parts, kept whole, and read after the slot was + * written again. + */ +export function numbers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let f!: (a0: rt.ByteSlice) => rt.Result; + const part: rt.u64 | undefined = big(data); + const err: returns_Error.Error | undefined = part === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + pushBig(out, (err !== undefined ? rt.Err(err as returns_Error.Error) : rt.Ok(part as rt.u64)) as rt.Result); + const part2: rt.i64 | undefined = pair(data, rt.usizeRem(kind as rt.usize, 4)); + const err2: returns_Error.Error | undefined = part2 === undefined ? pairOut.err as returns_Error.Error : undefined; + const part3: rt.u64 = pairOut.p_1; + if (err2 === undefined) { + const a: rt.i64 = part2 as rt.i64; + const b: rt.u64 = part3; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i64ToBeBytes(a))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(b))); + } else { + rt.byteVecPush(out, returns_Error.code(err2 as returns_Error.Error)); + } + const err3: returns_Error.Error | undefined = odd(data.data, data.start, data.length); + const part4: boolean = oddOut.p; + if (err3 === undefined) { + rt.byteVecPush(out, part4 ? 1 : 0); + } else { + rt.byteVecPush(out, returns_Error.code(err3 as returns_Error.Error)); + } + const part5: rt.u64 | undefined = big(data); + const err4: returns_Error.Error | undefined = part5 === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + const part7: rt.u64 | undefined = big(rt.byteSliceRange(data, rt.min(data.length, 1))); + const err5: returns_Error.Error | undefined = part7 === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + b2: { + if (err4 === undefined) { if (err5 === undefined) { - rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part17, part18, part19)); - } else { - rt.byteVecPush(out, returns_Error.code(err5 as returns_Error.Error)); - } - const part20: rt.u8 | undefined = wrapped(data.data, data.start, data.length); - const err6: returns_Wrapped.Wrapped | undefined = part20 === undefined ? wrappedOut.err as returns_Wrapped.Wrapped : undefined; - const part23: number = wrappedOut.p_1Len; - if (err6 === undefined) { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(part20 as rt.u8, part23 & 0xff))); - } else { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(238, (err6 as returns_Wrapped.Wrapped)._0, (err6 as returns_Wrapped.Wrapped)._1))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.u64Xor(part5 as rt.u64, part7 as rt.u64)))); + break b2; } + } + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(((err4 !== undefined ? rt.Err(err4 as returns_Error.Error) : rt.Ok(part5 as rt.u64)) as rt.Result).ok ? 1 : 0, ((err5 !== undefined ? rt.Err(err5 as returns_Error.Error) : rt.Ok(part7 as rt.u64)) as rt.Result).ok ? 1 : 0))); + } + for (const _67 of rt.iterToArray(rt.iterMap(new rt.Range(0, 3), (i: rt.usize): rt.Result => { + const part9 = big(rt.byteSliceRange(data, rt.min(data.length, i))); + return part9 === undefined ? rt.Err(madeOut$u64.err as returns_Error.Error) : rt.Ok(part9); + }), undefined, 16, true)) { + pushBig(out, _67); + } + if (!(rt.u8Rem(kind, 2) === 0)) { + f = (a02: rt.ByteSlice): rt.Result => { + const part11 = lateSelf(a02.data, a02.start, a02.length); + const result3: rt.Result = part11 === undefined ? rt.Err(madeOut$u64.err as returns_Error.Error) : rt.Ok(part11); + return result3; + }; + } else { + f = (a0: rt.ByteSlice): rt.Result => { + const part10 = big(a0); + const result2: rt.Result = part10 === undefined ? rt.Err(madeOut$u64.err as returns_Error.Error) : rt.Ok(part10); + return result2; + }; + } + pushBig(out, f(data)); + const part12: rt.u64 | undefined = either(data, rt.u8Rem(kind, 3) === 0); + const err6: returns_Error.Error | undefined = part12 === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + pushBig(out, (err6 !== undefined ? rt.Err(err6 as returns_Error.Error) : rt.Ok(part12 as rt.u64)) as rt.Result); + const _86: rt.ByteSlice = rt.byteSliceRange(data, rt.min(data.length, 2)); + const part14: rt.u64 | undefined = late(data.data, data.start, data.length, _86); + const err7: returns_Error.Error | undefined = part14 === undefined ? madeOut$u64.err as returns_Error.Error : undefined; + pushBig(out, (err7 !== undefined ? rt.Err(err7 as returns_Error.Error) : rt.Ok(part14 as rt.u64)) as rt.Result); + const part15: rt.f64 | undefined = mixed(data.data, data.start, data.length); + const err8: rt.u8 | undefined = part15 === undefined ? mixedOut.err as rt.u8 : undefined; + const part16: rt.char = mixedOut.p_1; + if (err8 === undefined) { + const f2: rt.f64 = part15 as rt.f64; + const c: rt.char = part16; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.f64ToBits(f2)))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(c as rt.u32))); + } else { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(238, err8 as rt.u8))); + } + let rest: rt.ByteSlice = data; + let sum: rt.u64 = 0; + while (true) { + const _112: rt.ByteSlice = rest; + const part17: rt.u32 | undefined = depth(_112.data, _112.start, _112.length); + if ((part17 === undefined ? depthOut.err as returns_Error.Error : undefined) === undefined) { + const n: rt.u32 = part17 as rt.u32; + sum = rt.u64WrappingAdd(rt.u64WrappingMul(sum, 31), n as rt.u64); + rest = rt.byteSliceRange(rest, rt.usizeAdd(n as rt.usize, 1)); } else { - const at: At.At = { _0: rt.usizeRem(kind as rt.usize, 5) }; - const err: returns_Error.Error | undefined = splitWith$returns_At(at, data.data, data.start, data.length); - const part3: number = At.splitOut$returns_Split.p_0Len; - const part6: number = At.splitOut$returns_Split.p_1Len; - if (err === undefined) { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(part3 & 0xff, part6 & 0xff))); - } else { - rt.byteVecPush(out, returns_Error.code(err as returns_Error.Error)); - } - const _196: rt.Result<[rt.ByteSlice, rt.ByteSlice], returns_Error.Error> = new Split.of$returns_At(at).split$Split(data); - if (_196.ok) { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_196.value[1].length & 0xff, _196.value[0].length & 0xff))); - } else { - rt.byteVecPush(out, returns_Error.code(_196.error)); - } - const err2: returns_Error.Error | undefined = At.split$returns_Split(at, data.data, data.start, data.length); - const part7: Uint8Array = At.splitOut$returns_Split.p_0Data; - const part8: number = At.splitOut$returns_Split.p_0Start; - const part9: number = At.splitOut$returns_Split.p_0Len; - const part10: Uint8Array = At.splitOut$returns_Split.p_1Data; - const part11: number = At.splitOut$returns_Split.p_1Start; - const part12: number = At.splitOut$returns_Split.p_1Len; - const _210: rt.Result = result_andThen$1yropo4((err2 !== undefined ? rt.Err(err2 as returns_Error.Error) : rt.Ok([new rt.ByteSlice(part7, part8, part9), new rt.ByteSlice(part10, part11, part12)] as [rt.ByteSlice, rt.ByteSlice])) as rt.Result<[rt.ByteSlice, rt.ByteSlice], returns_Error.Error>, (_2: [rt.ByteSlice, rt.ByteSlice]): rt.Result => { - const err3 = take(_2[1].data, _2[1].start, _2[1].length, _2[0].length); - return err3 !== undefined ? rt.Err(err3 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(takeOut.pData, takeOut.pStart, takeOut.pLen)); - }); - if (_210.ok) { - rt.byteVecPush(out, _210.value.length & 0xff); + break; + } + } + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(sum))); +} + +export function returns(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let f!: (a0: rt.ByteSlice) => rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>; + b0: { + if (!(rt.u8Rem(kind, 8) === 7)) { + const _7: rt.u8 = rt.u8Rem(kind, 7); + if (_7 === 0) { + const err16: returns_Error.Error | undefined = take(data.data, data.start, data.length, rt.usizeRem(kind as rt.usize, 7)); + const part65: Uint8Array = takeOut.pData; + const part66: number = takeOut.pStart; + const part67: number = takeOut.pLen; + push(out, (err16 !== undefined ? rt.Err(err16 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part65, part66, part67))) as rt.Result); + const err17: returns_Error.Error | undefined = take(data.data, data.start, data.length, 201); + const part68: Uint8Array = takeOut.pData; + const part69: number = takeOut.pStart; + const part70: number = takeOut.pLen; + push(out, (err17 !== undefined ? rt.Err(err17 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part68, part69, part70))) as rt.Result); + const err18: returns_Error.Error | undefined = tail(data, 3); + const part71: Uint8Array = takeOut.pData; + const part72: number = takeOut.pStart; + const part73: number = takeOut.pLen; + push(out, (err18 !== undefined ? rt.Err(err18 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part71, part72, part73))) as rt.Result); + const part74: rt.u64 | undefined = number(data.data, data.start, data.length, rt.u8Rem(kind, 2) === 0); + const err19: returns_Error.Error | undefined = part74 === undefined ? wideOut.err as returns_Error.Error : undefined; + const part75: Uint8Array = wideOut.p_1Data; + const part76: number = wideOut.p_1Start; + const part77: number = wideOut.p_1Len; + pushNumber(out, (err19 !== undefined ? rt.Err(err19 as returns_Error.Error) : rt.Ok([part74 as rt.u64, new rt.ByteSlice(part75, part76, part77)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); + const part78: rt.u64 | undefined = count(data.data, data.start, data.length); + const err20: returns_Error.Error | undefined = part78 === undefined ? wideOut.err as returns_Error.Error : undefined; + const part79: Uint8Array = wideOut.p_1Data; + const part80: number = wideOut.p_1Start; + const part81: number = wideOut.p_1Len; + pushNumber(out, (err20 !== undefined ? rt.Err(err20 as returns_Error.Error) : rt.Ok([part78 as rt.u64, new rt.ByteSlice(part79, part80, part81)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); + } else if (_7 === 1) { + const kept: rt.Result[] = rt.iterToArray(rt.iterMap(new rt.Range(0, 4), (n3: rt.usize): rt.Result => { + const err13 = take(data.data, data.start, data.length, rt.usizeMul(n3, 3)); + return err13 !== undefined ? rt.Err(err13 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(takeOut.pData, takeOut.pStart, takeOut.pLen)); + }), undefined, 12, true); + const err14: returns_Error.Error | undefined = tail(data, 1); + const part57: Uint8Array = takeOut.pData; + const part58: number = takeOut.pStart; + const part59: number = takeOut.pLen; + for (const _32 of kept) { + push(out, _32); + } + push(out, (err14 !== undefined ? rt.Err(err14 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part57, part58, part59))) as rt.Result); + const part60: rt.u64 | undefined = both(data.data, data.start, data.length); + const err15: returns_Error.Error | undefined = part60 === undefined ? bothOut.err as returns_Error.Error : undefined; + const part61: rt.u64 = bothOut.p_1; + const part64: number = bothOut.p_2Len; + if (err15 === undefined) { + const a: rt.u64 = part60 as rt.u64; + const b: rt.u64 = part61; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(a))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(b))); + rt.byteVecPush(out, part64 & 0xff); + } else { + rt.byteVecPush(out, returns_Error.code(err15 as returns_Error.Error)); + } + } else if (_7 === 2) { + if (!(rt.usizeRem(data.length, 2) === 0)) { + f = (a02: rt.ByteSlice): rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> => { + const part51 = narrow(a02.data, a02.start, a02.length); + const result3: rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> = part51 === undefined ? rt.Err(wideOut.err as returns_Error.Error) : rt.Ok([part51, new rt.ByteSlice(wideOut.p_1Data, wideOut.p_1Start, wideOut.p_1Len)] as [rt.u64, rt.ByteSlice]); + return result3; + }; + } else { + f = (a0: rt.ByteSlice): rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> => { + const part50 = wide(a0.data, a0.start, a0.length); + const result2: rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> = part50 === undefined ? rt.Err(wideOut.err as returns_Error.Error) : rt.Ok([part50, new rt.ByteSlice(wideOut.p_1Data, wideOut.p_1Start, wideOut.p_1Len)] as [rt.u64, rt.ByteSlice]); + return result2; + }; + } + pushNumber(out, f(data)); + for (const _74 of rt.iterToArray(rt.iterMap(rt.iterSlice([data, rt.byteSliceRange(data, rt.min(data.length, 3))]), (a03: rt.ByteSlice): rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> => { + const part53 = count(a03.data, a03.start, a03.length); + const result4: rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error> = part53 === undefined ? rt.Err(wideOut.err as returns_Error.Error) : rt.Ok([part53, new rt.ByteSlice(wideOut.p_1Data, wideOut.p_1Start, wideOut.p_1Len)] as [rt.u64, rt.ByteSlice]); + return result4; + }), undefined, 16, true)) { + pushNumber(out, _74); + } + rt.iterExtendBytes(out, rt.iterOption(option_map$0c2keyb(option_map$1yogfym((data.length !== 0 ? [data.data[data.start], new rt.ByteSlice(data.data, data.start + 1, data.length - 1)] as [rt.u8, rt.ByteSlice] : undefined) as [rt.u8, rt.ByteSlice] | undefined, (_23: [rt.u8, rt.ByteSlice]): rt.ByteSlice => { + return _23[1]; + }), (s2: rt.ByteSlice): rt.u8 => { + const err12: returns_Error.Error | undefined = tail(s2, 2); + const part54: Uint8Array = takeOut.pData; + const part55: number = takeOut.pStart; + const part56: number = takeOut.pLen; + return result_mapOr$0fbyajt((err12 !== undefined ? rt.Err(err12 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(part54, part55, part56))) as rt.Result, 0, (a04: rt.ByteSlice): rt.usize => { + return a04.length; + }) & 0xff; + })), undefined, true); + } else if (_7 === 3) { + const err9: rt.u8 | undefined = halves(data.data, data.start, data.length); + const part35: number = halvesOut.p_0Len; + const part36: Uint8Array = halvesOut.p_1Data; + const part37: number = halvesOut.p_1Start; + const part38: number = halvesOut.p_1Len; + const part39: Uint8Array = halvesOut.p_2Data; + const part40: number = halvesOut.p_2Start; + const part41: number = halvesOut.p_2Len; + if (err9 === undefined) { + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part36, part37, part38)); + rt.byteVecPush(out, part35 & 0xff); + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part39, part40, part41)); + } else { + rt.byteVecPush(out, err9 as rt.u8); + } + const other: Uint8Array = Uint8Array.of(255, 1, 2); + const _99: rt.ByteSlice = rt.byteSliceFrom(other); + const part42: rt.u64 | undefined = guarded(data.data, data.start, data.length, _99); + const err10: returns_Error.Error | undefined = part42 === undefined ? wideOut.err as returns_Error.Error : undefined; + const part43: Uint8Array = wideOut.p_1Data; + const part44: number = wideOut.p_1Start; + const part45: number = wideOut.p_1Len; + pushNumber(out, (err10 !== undefined ? rt.Err(err10 as returns_Error.Error) : rt.Ok([part42 as rt.u64, new rt.ByteSlice(part43, part44, part45)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); + const _103: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(other), 1); + const part46: rt.u64 | undefined = guarded(data.data, data.start, data.length, _103); + const err11: returns_Error.Error | undefined = part46 === undefined ? wideOut.err as returns_Error.Error : undefined; + const part47: Uint8Array = wideOut.p_1Data; + const part48: number = wideOut.p_1Start; + const part49: number = wideOut.p_1Len; + pushNumber(out, (err11 !== undefined ? rt.Err(err11 as returns_Error.Error) : rt.Ok([part46 as rt.u64, new rt.ByteSlice(part47, part48, part49)] as [rt.u64, rt.ByteSlice])) as rt.Result<[rt.u64, rt.ByteSlice], returns_Error.Error>); + } else if (_7 === 4) { + const _107: rt.Result = unitErr(data.data, data.start, data.length); + if (_107.ok) { + rt.byteVecPush(out, _107.value.length & 0xff); + } else { + rt.byteVecPush(out, 238); + } + const _113: rt.Result = noneErr(data); + if (_113.ok) { + rt.byteVecPush(out, _113.value.length & 0xff); + } else { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(238, option_unwrapOr$0r2bmpr(_113.error, 0)))); + } + const part24: rt.u16 | undefined = lead$u16(data.data, data.start, data.length); + const err7: returns_Error.Error | undefined = part24 === undefined ? leadOut$u16.err as returns_Error.Error : undefined; + const part25: Uint8Array = leadOut$u16.p_1Data; + const part26: number = leadOut$u16.p_1Start; + const part27: number = leadOut$u16.p_1Len; + const _126: rt.ByteSlice = rt.byteSliceRange(data, rt.min(data.length, 1)); + const part29: rt.u32 | undefined = lead$u32(_126.data, _126.start, _126.length); + const err8: returns_Error.Error | undefined = part29 === undefined ? leadOut$u32.err as returns_Error.Error : undefined; + const part30: Uint8Array = leadOut$u32.p_1Data; + const part31: number = leadOut$u32.p_1Start; + const part32: number = leadOut$u32.p_1Len; + if (err7 === undefined) { + if (err8 === undefined) { + const a3: rt.u16 = part24 as rt.u16; + const b3: rt.u32 = part29 as rt.u32; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(a3))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(b3))); + rt.byteVecPush(out, part32 & 0xff); + break b0; + } + } + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(((err7 !== undefined ? rt.Err(err7 as returns_Error.Error) : rt.Ok([part24 as rt.u16, new rt.ByteSlice(part25, part26, part27)] as [rt.u16, rt.ByteSlice])) as rt.Result<[rt.u16, rt.ByteSlice], returns_Error.Error>).ok ? 1 : 0, ((err8 !== undefined ? rt.Err(err8 as returns_Error.Error) : rt.Ok([part29 as rt.u32, new rt.ByteSlice(part30, part31, part32)] as [rt.u32, rt.ByteSlice])) as rt.Result<[rt.u32, rt.ByteSlice], returns_Error.Error>).ok ? 1 : 0))); + } else if (_7 === 5) { + const err5: returns_Error.Error | undefined = afterFixed(data.data, data.start, data.length); + const part17: Uint8Array = takeOut.pData; + const part18: number = takeOut.pStart; + const part19: number = takeOut.pLen; + if (err5 === undefined) { + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(part17, part18, part19)); + } else { + rt.byteVecPush(out, returns_Error.code(err5 as returns_Error.Error)); + } + const part20: rt.u8 | undefined = wrapped(data.data, data.start, data.length); + const err6: returns_Wrapped.Wrapped | undefined = part20 === undefined ? wrappedOut.err as returns_Wrapped.Wrapped : undefined; + const part23: number = wrappedOut.p_1Len; + if (err6 === undefined) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(part20 as rt.u8, part23 & 0xff))); + } else { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(238, (err6 as returns_Wrapped.Wrapped)._0, (err6 as returns_Wrapped.Wrapped)._1))); + } } else { - rt.byteVecPush(out, returns_Error.code(_210.error)); - } - let restData4: Uint8Array = data.data; - let restStart4: number = data.start; - let restLen4: number = data.length; - let sum: rt.u64 = 0; - while (true) { - const part13: rt.u64 | undefined = narrow(restData4, restStart4, restLen4); - const err4: returns_Error.Error | undefined = part13 === undefined ? wideOut.err as returns_Error.Error : undefined; - const part14: Uint8Array = wideOut.p_1Data; - const part15: number = wideOut.p_1Start; - const part16: number = wideOut.p_1Len; - if (err4 === undefined) { - sum = rt.u64WrappingAdd(rt.u64WrappingMul(sum, 31), part13 as rt.u64); - restData4 = part14; - restStart4 = part15; - restLen4 = part16; + const at: At.At = { _0: rt.usizeRem(kind as rt.usize, 5) }; + const err: returns_Error.Error | undefined = splitWith$returns_At(at, data.data, data.start, data.length); + const part3: number = At.splitOut$returns_Split.p_0Len; + const part6: number = At.splitOut$returns_Split.p_1Len; + if (err === undefined) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(part3 & 0xff, part6 & 0xff))); + } else { + rt.byteVecPush(out, returns_Error.code(err as returns_Error.Error)); + } + const _199: rt.Result<[rt.ByteSlice, rt.ByteSlice], returns_Error.Error> = new Split.of$returns_At(at).split$Split(data); + if (_199.ok) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_199.value[1].length & 0xff, _199.value[0].length & 0xff))); } else { - break; + rt.byteVecPush(out, returns_Error.code(_199.error)); } + const err2: returns_Error.Error | undefined = At.split$returns_Split(at, data.data, data.start, data.length); + const part7: Uint8Array = At.splitOut$returns_Split.p_0Data; + const part8: number = At.splitOut$returns_Split.p_0Start; + const part9: number = At.splitOut$returns_Split.p_0Len; + const part10: Uint8Array = At.splitOut$returns_Split.p_1Data; + const part11: number = At.splitOut$returns_Split.p_1Start; + const part12: number = At.splitOut$returns_Split.p_1Len; + const _213: rt.Result = result_andThen$1yropo4((err2 !== undefined ? rt.Err(err2 as returns_Error.Error) : rt.Ok([new rt.ByteSlice(part7, part8, part9), new rt.ByteSlice(part10, part11, part12)] as [rt.ByteSlice, rt.ByteSlice])) as rt.Result<[rt.ByteSlice, rt.ByteSlice], returns_Error.Error>, (_2: [rt.ByteSlice, rt.ByteSlice]): rt.Result => { + const err3 = take(_2[1].data, _2[1].start, _2[1].length, _2[0].length); + return err3 !== undefined ? rt.Err(err3 as returns_Error.Error) : rt.Ok(new rt.ByteSlice(takeOut.pData, takeOut.pStart, takeOut.pLen)); + }); + if (_213.ok) { + rt.byteVecPush(out, _213.value.length & 0xff); + } else { + rt.byteVecPush(out, returns_Error.code(_213.error)); + } + let restData4: Uint8Array = data.data; + let restStart4: number = data.start; + let restLen4: number = data.length; + let sum: rt.u64 = 0; + while (true) { + const part13: rt.u64 | undefined = narrow(restData4, restStart4, restLen4); + const err4: returns_Error.Error | undefined = part13 === undefined ? wideOut.err as returns_Error.Error : undefined; + const part14: Uint8Array = wideOut.p_1Data; + const part15: number = wideOut.p_1Start; + const part16: number = wideOut.p_1Len; + if (err4 === undefined) { + sum = rt.u64WrappingAdd(rt.u64WrappingMul(sum, 31), part13 as rt.u64); + restData4 = part14; + restStart4 = part15; + restLen4 = part16; + } else { + break; + } + } + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(sum))); } - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(sum))); + } else { + numbers(kind, data, out); } } } diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index ddc80a53d..bc2897fd7 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { copy$Option_shapes_Shape, copy$Option_tuple2_ref_sync_Mutex_u8_u8, copy$Result_tuple2_ref_sync_Mutex_u8_u8_u8, copy$array_ref_sync_Mutex_u8, copy$array_shapes_Point, copy$array_u8, copy$ops_ControlFlow_ref_sync_Arc_u8_array1_u8, copy$ops_ControlFlow_unit_array2_u8, copy$shapes_Gauge, copy$shapes_Held, copy$shapes_Lens, copy$shapes_Level, copy$shapes_Point, copy$shapes_Shape, copy$task_Poll_array1_u8, copy$tuple2_ref_sync_Mutex_u8_u8, copy$tuple2_shapes_Point_u8, copy$tuple2_u8_u8 } from "./lib.ts"; +import { madeOut$u8 } from "./cells.ts"; import { thenSome$0ztng9q, then$1i9qaz4, then$1btzga3 } from "./core/bool.ts"; import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr, map$0600mq0 as option_map$0600mq0, map$18z3v8h as option_map$18z3v8h, mapOr$1a3irjf as option_mapOr$1a3irjf, mapOr$1t0muvr as option_mapOr$1t0muvr, mapOr$1ybqvup as option_mapOr$1ybqvup, mapOr$0rtdylj as option_mapOr$0rtdylj, mapOr$0aserv9 as option_mapOr$0aserv9, mapOr$1hjjwvv as option_mapOr$1hjjwvv, okOr$1vxk0yk as option_okOr$1vxk0yk } from "./core/option.ts"; import { map$1dbyvzi as result_map$1dbyvzi, mapErr$0dzqwjy as result_mapErr$0dzqwjy } from "./core/result.ts"; @@ -1407,11 +1408,12 @@ export function patterns(data: rt.ByteSlice, out: rt.ByteVec): void { } /** `n`'s result: `Ok` for an even `n`. */ -export function parity(n: rt.u8): rt.Result { +export function parity(n: rt.u8): rt.u8 | undefined { if (!(rt.u8Rem(n, 2) === 0)) { - return rt.Err(n); + madeOut$u8.err = n; + return undefined; } - return rt.Ok(n); + return n; } /** @@ -1427,7 +1429,8 @@ export function payloads(_dataData: Uint8Array, _dataStart: number, dataLen: num let _143!: rt.u8; let _149!: rt.u8; const n: rt.u8 = dataLen & 0xff; - let res: rt.Result = parity(n); + const part = parity(n); + let res: rt.Result = part === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part); const before: rt.Result = res; if (!res.ok) { res = rt.Err(rt.u8WrappingAdd(res.error, 1)); @@ -1443,7 +1446,9 @@ export function payloads(_dataData: Uint8Array, _dataStart: number, dataLen: num if (owned.ok) { owned = rt.Ok(rt.u8WrappingAdd(owned.value, 2)); } - const slot: shapes_Slot.Slot = { res: parity(rt.u8WrappingAdd(n, 1)) }; + const part2: rt.u8 | undefined = parity(rt.u8WrappingAdd(n, 1)); + const err2: rt.u8 | undefined = part2 === undefined ? madeOut$u8.err as rt.u8 : undefined; + const slot: shapes_Slot.Slot = { res: (err2 !== undefined ? rt.Err(err2 as rt.u8) : rt.Ok(part2 as rt.u8)) as rt.Result }; if (!slot.res.ok) { const _33: rt.u8 = rt.u8WrappingSub(slot.res.error, 4); slot.res = rt.Err(_33); @@ -1460,7 +1465,9 @@ export function payloads(_dataData: Uint8Array, _dataStart: number, dataLen: num if (poll !== rt.Pending) { poll = rt.u8WrappingSub(poll, 1); } - let nested = parity(rt.u8WrappingAdd(n, 2)) as rt.Poll | undefined>; + const part3: rt.u8 | undefined = parity(rt.u8WrappingAdd(n, 2)); + const err3: rt.u8 | undefined = part3 === undefined ? madeOut$u8.err as rt.u8 : undefined; + let nested = ((err3 !== undefined ? rt.Err(err3 as rt.u8) : rt.Ok(part3 as rt.u8)) as rt.Result) as rt.Poll | undefined>; if (nested !== rt.Pending) { if (nested !== undefined) { if (!nested.ok) { @@ -1468,7 +1475,8 @@ export function payloads(_dataData: Uint8Array, _dataStart: number, dataLen: num } } } - let caught: rt.Result = parity(rt.u8WrappingAdd(n, 3)); + const part4 = parity(rt.u8WrappingAdd(n, 3)); + let caught: rt.Result = part4 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part4); const bump: () => undefined = (): undefined => { if (!caught.ok) { caught = rt.Err(rt.u8WrappingAdd(caught.error, 5)); @@ -1476,7 +1484,8 @@ export function payloads(_dataData: Uint8Array, _dataStart: number, dataLen: num }; bump(); bump(); - let replaced: rt.Result = parity(rt.u8WrappingAdd(n, 4)); + const part5 = parity(rt.u8WrappingAdd(n, 4)); + let replaced: rt.Result = part5 === undefined ? rt.Err(madeOut$u8.err as rt.u8) : rt.Ok(part5); if (replaced.ok) { const old = replaced.value; replaced = rt.Ok(66); diff --git a/ts/fixtures/lang/src/returns.rs b/ts/fixtures/lang/src/returns.rs index 0d2c92d4c..17bd25413 100644 --- a/ts/fixtures/lang/src/returns.rs +++ b/ts/fixtures/lang/src/returns.rs @@ -1,5 +1,5 @@ -//! Results of byte slices returned by the crate's own functions, which hand back an -//! `Ok`'s parts in a slot their caller reads at once. +//! Results of byte slices, or of numbers alone, returned by the crate's own functions, +//! which hand back an `Ok`'s parts in a slot their caller reads at once. #[derive(Debug, Clone, Copy, PartialEq)] enum Error { @@ -184,7 +184,140 @@ fn wrapped(data: &[u8]) -> Result<(u8, &[u8]), Wrapped> { Ok((v, data)) } +/// A number alone, past 2^53 from eight bytes. +fn big(data: &[u8]) -> Result { + match data.split_first_chunk::<8>() { + Some((head, _)) => Ok(u64::from_be_bytes(*head)), + None if data.first() == Some(&0xff) => Err(Error::Bad), + None => Err(Error::Short(8 - data.len())), + } +} + +/// A signed number and an unsigned one, each through `?`, at an offset. +fn pair(data: &[u8], at: usize) -> Result<(i64, u64), Error> { + let rest = data.get(at..).ok_or(Error::Short(at))?; + let a = big(rest)? as i64; + Ok((a.wrapping_neg(), big(&rest[rest.len().min(3)..])? >> 3)) +} + +/// A flag, which is no number to return, so it goes in the slot. +fn odd(data: &[u8]) -> Result { + let n = big(data)?; + if n == 7 { + return Err(Error::Short(7)); + } + Ok(n % 2 == 1) +} + +/// A number whose error is a number too. +fn byte(data: &[u8], i: usize) -> Result { + match data.get(i) { + Some(&b) if b < 0x80 => Ok(b), + Some(&b) => Err(b), + None => Err(i as u8), + } +} + +/// A float and a char. +fn mixed(data: &[u8]) -> Result<(f64, char), u8> { + let b = byte(data, 0)?; + let c = char::from_u32(byte(data, 1)? as u32 * 37 + 0xd7f0).ok_or(b)?; + Ok((-(b as f64) / 3.0, c)) +} + +/// Each byte before a zero counted, recursively, without the rest. +fn depth(data: &[u8]) -> Result { + match data.split_first() { + Some((0, _)) => Ok(0), + Some((_, rest)) => Ok(depth(rest)? + 1), + None => Err(Error::Short(1)), + } +} + +/// What either returns, returned as is. +fn either(data: &[u8], first: bool) -> Result { + if first { + return big(data); + } + depth(data).map(u64::from) +} + +/// A guard whose drop writes the slot after its function's call did. +struct Late<'a>(&'a [u8]); + +impl Drop for Late<'_> { + fn drop(&mut self) { + let _ = big(self.0); + } +} + +fn late(data: &[u8], other: &[u8]) -> Result { + let _late = Late(other); + big(data) +} + +fn late_self(data: &[u8]) -> Result { + late(data, data) +} + +fn push_big(out: &mut Vec, r: Result) { + match r { + Ok(n) => out.extend_from_slice(&n.to_be_bytes()), + Err(e) => out.extend_from_slice(&[0xee, e.code()]), + } +} + +/// Results of numbers alone: held as parts, kept whole, and read after the slot was +/// written again. +fn numbers(kind: u8, data: &[u8], out: &mut Vec) { + push_big(out, big(data)); + match pair(data, kind as usize % 4) { + Ok((a, b)) => { + out.extend_from_slice(&a.to_be_bytes()); + out.extend_from_slice(&b.to_be_bytes()); + } + Err(e) => out.push(e.code()), + } + match odd(data) { + Ok(b) => out.push(b as u8), + Err(e) => out.push(e.code()), + } + // Both held while the slot is written again between them. + let (x, y) = (big(data), big(&data[data.len().min(1)..])); + match (x, y) { + (Ok(x), Ok(y)) => out.extend_from_slice(&(x ^ y).to_be_bytes()), + (x, y) => out.extend_from_slice(&[x.is_ok() as u8, y.is_ok() as u8]), + } + // Kept whole: in a collection, and through a function as a value. + let kept: Vec> = (0..3).map(|i| big(&data[data.len().min(i)..])).collect(); + for r in kept { + push_big(out, r); + } + let f: fn(&[u8]) -> Result = if kind % 2 == 0 { big } else { late_self }; + push_big(out, f(data)); + push_big(out, either(data, kind % 3 == 0)); + push_big(out, late(data, &data[data.len().min(2)..])); + match mixed(data) { + Ok((f, c)) => { + out.extend_from_slice(&f.to_bits().to_be_bytes()); + out.extend_from_slice(&(c as u32).to_be_bytes()); + } + Err(b) => out.extend_from_slice(&[0xee, b]), + } + // A loop that calls again while the last result is still read. + let mut rest = data; + let mut sum = 0u64; + while let Ok(n) = depth(rest) { + sum = sum.wrapping_mul(31).wrapping_add(n as u64); + rest = &rest[n as usize + 1..]; + } + out.extend_from_slice(&sum.to_be_bytes()); +} + pub fn returns(kind: u8, data: &[u8], out: &mut Vec) { + if kind % 8 == 7 { + return numbers(kind, data, out); + } match kind % 7 { 0 => { push(out, take(data, kind as usize % 7)); diff --git a/ts/fixtures/task/expected/drops.ts b/ts/fixtures/task/expected/drops.ts index d1645d00a..c3a20935f 100644 --- a/ts/fixtures/task/expected/drops.ts +++ b/ts/fixtures/task/expected/drops.ts @@ -86,12 +86,12 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { const arc: rt.Arc = new rt.Arc({ tag: "One", _0: guard(4) } as Held.Held); const shared: rt.Arc = rt.arcClone(arc); rt.arcDrop(arc, lib_drop$drops_Held); - const part3 = shared.strong; - const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part3, f)]; + const part5 = shared.strong; + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part5, f)]; Log.note(log, rt.fmtFormat(["arc ", args2[0]])); - const part4 = shared.strong; + const part6 = shared.strong; const cloned: rt.Arc = rt.arcClone(shared); - const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part4, f)]; + const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part6, f)]; Log.note(log, rt.fmtFormat(["counted ", args4[0]])); rt.arcDrop(cloned, lib_drop$drops_Held); const v2: Held.Held[] = []; @@ -151,10 +151,12 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { const part2 = clones.length; const args10: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part2, f)]; Log.note(log, rt.fmtFormat(["clones ", args10[0]])); - const early: rt.Result = bail(log, id, true); - const late: rt.Result = bail(log, rt.u32Add(id, 2), false); - const _256: boolean = !early.ok; - const _259: boolean = !late.ok; + const part3: rt.u32 | undefined = bail(log, id, true); + const err: rt.u32 | undefined = part3 === undefined ? bailOut.err as rt.u32 : undefined; + const part4: rt.u32 | undefined = bail(log, rt.u32Add(id, 2), false); + const err2: rt.u32 | undefined = part4 === undefined ? bailOut.err as rt.u32 : undefined; + const _256: boolean = !((err !== undefined ? rt.Err(err as rt.u32) : rt.Ok(part3 as rt.u32)) as rt.Result).ok; + const _259: boolean = !((err2 !== undefined ? rt.Err(err2 as rt.u32) : rt.Ok(part4 as rt.u32)) as rt.Result).ok; const args12: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(_256, f), (f: rt.Formatter) => rt.fmtBool(_259, f)]; Log.note(log, rt.fmtFormat(["bailed ", args12[0], " ", args12[1]])); rt.dropAll(clones, lib_drop$sync_Arc_drops_Wrap_drops_Pair_drops_Held); @@ -184,34 +186,48 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { * Returns early through `?`, if it `fails`, with an enum's guards and a generic `Drop` * type live. */ -export function bail(log: Log.Log, id: rt.u32, fails: boolean): rt.Result { +export function bail(log: Log.Log, id: rt.u32, fails: boolean): rt.u32 | undefined { + let _0Err!: boolean; + let _02!: rt.u32; + let _03!: rt.u32; const v: Held.Held[] = []; const _8 = { tag: "One", _0: Guard.new(rt.u32Add(id, 90), Log.clone$Clone(log)) } as Held.Held; v.push(_8); const held: Held.Held[] = v; const wrap: Wrap.Wrap = new Wrap.Wrap(rt.u32Add(id, 91), Log.clone$Clone(log), { tag: "Empty" } as Held.Held); - const _18: rt.Result = failed(fails); - if (_18.ok) { - const n: rt.u32 = _18.value; + const part: rt.u32 | undefined = failed(fails); + const err: rt.u32 | undefined = part === undefined ? bailOut.err as rt.u32 : undefined; + if (err === undefined) { + const n: rt.u32 = part as rt.u32; const _29: rt.usize = held.length; const _32: boolean = Held.isSome(wrap.inner); const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(_29, f), (f: rt.Formatter) => rt.fmtBool(_32, f)]; Log.note(log, rt.fmtFormat(["not bailed ", args2[0], " ", args2[1]])); - const _0: rt.Result = rt.Ok(n); + _0Err = false; + _02 = n; + lib_drop$drops_Wrap_drops_Held(wrap); + rt.dropAll(held, lib_drop$drops_Held); + } else { + _0Err = true; + _03 = err as rt.u32; lib_drop$drops_Wrap_drops_Held(wrap); rt.dropAll(held, lib_drop$drops_Held); - return _0; } - lib_drop$drops_Wrap_drops_Held(wrap); - rt.dropAll(held, lib_drop$drops_Held); - return _18; + if (_0Err) { + bailOut.err = _03; + return undefined; + } + return _02; } -export function failed(fails: boolean): rt.Result { +export const bailOut: { err: unknown } = { err: undefined }; + +export function failed(fails: boolean): rt.u32 | undefined { if (!fails) { - return rt.Ok(1); + return 1; } - return rt.Err(0); + bailOut.err = 0; + return undefined; } /** diff --git a/ts/src/lower/mir/scalar.rs b/ts/src/lower/mir/scalar.rs index 1317ceea4..bef487185 100644 --- a/ts/src/lower/mir/scalar.rs +++ b/ts/src/lower/mir/scalar.rs @@ -1,16 +1,16 @@ //! Locals of byte slices held as their parts. A `&[u8]` is an `rt.ByteSlice`, so each //! split, and each tuple, `Option`, `Result`, or `ControlFlow` around one, makes objects -//! V8 doesn't remove. A local built of them that nothing borrows or writes into, and -//! that only splits, `ok_or`, `?`, aggregates, and copies assign, is instead a value per -//! part: a slice's bytes, start, and length, a flag for which variant an enum is, and -//! anything else, as an error, whole. A read that takes it apart reads its parts; any -//! other read makes the object there. A local whose object would be made more than once -//! on a path, or in a loop that doesn't make its parts again, stays an object, so -//! nothing is made more often than before, as does one only ever assigned an object's -//! parts, which saves none. A function that hands back a `Result` in a slot holds its -//! result so, to hand back the `Ok`'s parts, and its callers read them back as parts. A -//! byte slice parameter is passed as its parts when its body holds it so and never makes -//! its object. +//! V8 doesn't remove, as does a tuple or `Result` of numbers alone. A local built of them +//! that nothing borrows or writes into, and that only splits, `ok_or`, `?`, aggregates, +//! and copies assign, is instead a value per part: a slice's bytes, start, and length, a +//! flag for which variant an enum is, and anything else, as an error, whole. A read that +//! takes it apart reads its parts; any other read makes the object there. A local whose +//! object would be made more than once on a path, or in a loop that doesn't make its +//! parts again, stays an object, so nothing is made more often than before, as does one +//! only ever assigned an object's parts, which saves none. A function that hands back a +//! `Result` in a slot holds its result so, to hand back the `Ok`'s parts, and its callers +//! read them back as parts; a local with no slice is held so only there. A byte slice +//! parameter is passed as its parts when its body holds it so and never makes its object. use rustc_abi::VariantIdx; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -65,6 +65,21 @@ impl<'tcx> Shape<'tcx> { } } + /// Whether its JS value is an object of its own, which making it from its parts makes + /// again: a tuple, a `Result` or `ControlFlow`, or an `Option` of one. + fn boxed(&self) -> bool { + match self { + Shape::Unit | Shape::Leaf(_) | Shape::Slice => false, + Shape::Tuple(..) => true, + Shape::Enum(ty, variants) => match ty.kind() { + ty::Adt(adt, _) if adt.variant(VariantIdx::from_usize(0)).name.as_str() == "None" => { + variants.iter().flatten().flatten().any(Shape::boxed) + } + _ => true, + }, + } + } + fn has_enum(&self) -> bool { match self { Shape::Enum(..) => true, @@ -305,11 +320,11 @@ impl<'tcx> crate::lower::Translator<'_, 'tcx> { /// Where `instance` hands back its `Ok`'s parts, if it does: a function of the crate's /// that only translated code calls directly, and that returns a `Result` of byte slices - /// and scalars whose error is one value that is never `undefined`, such as - /// `Result<(u64, &[u8]), E>`, writes the `Ok`'s parts into a module-level slot its - /// caller reads at once, and returns its error or `undefined`. One whose first part is - /// a number returns that, or `undefined` for an error, and writes the error into the - /// slot instead. + /// and scalars, or of numbers alone, whose error is one value that is never + /// `undefined`, such as `Result<(u64, &[u8]), E>` or `Result<(i64, u64), E>`, writes + /// the `Ok`'s parts into a module-level slot its caller reads at once, and returns its + /// error or `undefined`. One whose first part is a number returns that, or `undefined` + /// for an error, and writes the error into the slot instead. pub(crate) fn slotted(&mut self, instance: ty::Instance<'tcx>) -> Option> { if let Some(slotted) = self.slotted.get(&instance) { return slotted.clone(); @@ -337,11 +352,19 @@ impl<'tcx> crate::lower::Translator<'_, 'tcx> { return None; } let shape = self.shape(ok)?; - if !shape.has_slice() || shape.has_enum() || !matches!(self.shape(err), Some(Shape::Leaf(_))) { + if shape.has_enum() || !matches!(self.shape(err), Some(Shape::Leaf(_))) { return None; } let mut parts = Vec::new(); shape.parts("", &mut parts); + // Byte slices, or else numbers alone, whose `Ok` would be the one object made. + let numbers = !parts.is_empty() + && parts + .iter() + .all(|(_, part)| matches!(part, Part::Value(t) if number(*t))); + if !shape.has_slice() && !numbers { + return None; + } let erased = self.erases_to(err); if erased.undefined || erased.none || erased.param || erased.pending || erased.outer_pending { return None; @@ -591,6 +614,15 @@ fn slot_fail(slot: &Slotted<'_>, err: Expr, loc: Option) -> Vec< } } +/// The items of `value` if it is an array literal of `n` of them, as a tuple is made. +fn tuple_items(value: &Expr, n: usize) -> Option<&[Expr]> { + match value { + Expr::As(inner, _) => tuple_items(inner, n), + Expr::Array(items) if items.len() == n => Some(items), + _ => None, + } +} + /// Whether `ty` is a `&[u8]` or `&[u8; N]`, which is an `rt.ByteSlice`. fn slice_ref(ty: Ty<'_>) -> bool { matches!(ty.kind(), ty::Ref(_, inner, rustc_ast::Mutability::Not) @@ -689,6 +721,9 @@ struct Made { block: BasicBlock, local: Local, parts: std::ops::Range, + /// Whether it makes an object of its own, a tuple or `Result` say, and no slice, so + /// that object is what each of its parts makes. + boxed: bool, } impl<'tcx> Fx<'_, '_, '_, 'tcx> { @@ -967,11 +1002,12 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { continue; } if let Some(shape) = self.shape(ty) - && shape.has_slice() + && (shape.has_slice() || shape.boxed()) { shapes.insert(local, shape); } } + self.slot_reach(&mut shapes); let mut parted_calls: FxHashMap> = FxHashMap::default(); for (bb, data) in mir.basic_blocks.iter_enumerated() { if let TerminatorKind::Call { func, .. } = &data.terminator().kind { @@ -1001,6 +1037,67 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { } } + /// Keep of `shapes` those with no slice only where they meet a slot: what a slotted + /// call returns, the result of a function with a slot, and what is assigned from or + /// into those. Elsewhere its own object is all one saves, and a std function such as + /// `Poll::map_ok` hands back the one it was given. + fn slot_reach(&self, shapes: &mut FxHashMap>) { + struct Reads(Vec); + impl<'tcx> Visitor<'tcx> for Reads { + fn visit_local(&mut self, local: Local, _: PlaceContext, _: Location) { + self.0.push(local); + } + } + let mut roots = Vec::new(); + if self.slot.is_some() { + roots.push(mir::RETURN_PLACE); + } + let mut edges: FxHashMap> = FxHashMap::default(); + let mut link = |a: Local, reads: Vec| { + for b in reads.into_iter().filter(|b| shapes.contains_key(b)) { + edges.entry(a).or_default().push(b); + edges.entry(b).or_default().push(a); + } + }; + for (bb, data) in self.mir.basic_blocks.iter_enumerated() { + for (i, stmt) in data.statements.iter().enumerate() { + if let StatementKind::Assign(assign) = &stmt.kind + && let Some(local) = assign.0.as_local().filter(|l| shapes.contains_key(l)) + { + let mut reads = Reads(Vec::new()); + reads.visit_rvalue( + &assign.1, + Location { + block: bb, + statement_index: i, + }, + ); + link(local, reads.0); + } + } + if let TerminatorKind::Call { args, destination, .. } = &data.terminator().kind + && let Some(local) = destination.as_local().filter(|l| shapes.contains_key(l)) + { + if self.slotted_calls.contains_key(&bb) { + roots.push(local); + } + let reads = args + .iter() + .filter_map(|arg| arg.node.place()) + .map(|p| p.local) + .collect(); + link(local, reads); + } + } + let mut reached: FxHashSet = FxHashSet::default(); + while let Some(local) = roots.pop() { + if reached.insert(local) { + roots.extend(edges.get(&local).into_iter().flatten().copied()); + } + } + shapes.retain(|local, shape| shape.has_slice() || reached.contains(local)); + } + /// The locals of `shapes` that can't be held as their parts, given the others are: one /// assigned other than by what makes its parts, or written into, or whose object would /// be made more often than now. Then the parameters passed as parts whose object some @@ -1175,6 +1272,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { block: at.block, local: place.local, parts: node.parts(), + boxed: node.shape.boxed() && !node.shape.has_slice(), }); } } @@ -1212,7 +1310,10 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { let none = FxHashSet::default(); let objects = |made: &Made| -> FxHashSet<(Local, usize)> { let mut parts = Vec::new(); - shapes[&made.local].objects(0, &mut parts); + match made.boxed { + true => parts.extend(made.parts.clone()), + false => shapes[&made.local].objects(0, &mut parts), + } parts .into_iter() .filter(|p| made.parts.contains(p)) @@ -2048,6 +2149,15 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { .map(|key| (super::place::slice_member(slice.clone(), key), true)) .collect() } + // A tuple made here, or held back to make where it is read: its items, and no + // tuple. + Shape::Tuple(_, fields) if let Some(items) = self.made_tuple(&value, fields, span, out) => { + let mut parts = Vec::new(); + for (item, field) in items.into_iter().zip(fields) { + parts.extend(self.take_apart(item, field, span, out)); + } + parts + } Shape::Tuple(ty, fields) => { let ts_ty = self.b.t.ty(*ty, span); let (tuple, _) = self.steady_value(value, "tuple", ts_ty, out); @@ -2131,6 +2241,47 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { } } + /// The items of `value`, a tuple of `fields`, if it is made here or held back to make + /// where it is read, each read now, in order, unless it reads the same later and does + /// nothing; the tuple is then never made. + fn made_tuple( + &mut self, + value: &Expr, + fields: &[Shape<'tcx>], + span: Span, + out: &mut Vec, + ) -> Option> { + let held = match value { + Expr::Ident(name) => Some( + self.pending + .iter() + .position(|p| p.name == *name && !p.effect && !p.copy)?, + ), + _ => None, + }; + let made = held.map_or(value, |i| &self.pending[i].value); + let items = tuple_items(made, fields.len())?; + // Only a value held whole can be read now into a variable of its own. + if !std::iter::zip(items, fields).all(|(item, field)| matches!(field, Shape::Leaf(_)) || self.steady_expr(item)) + { + return None; + } + let items = items.to_vec(); + if let Some(i) = held { + self.pending.remove(i); + } + let items = std::iter::zip(items, fields) + .map(|(item, field)| match field { + Shape::Leaf(ty) => { + let ty = self.b.t.part_ty(Part::Value(*ty), span); + self.steady_value(item, "item", ty, out).0 + } + _ => item, + }) + .collect(); + Some(items) + } + /// A value held whole as a part, and whether reading it later reads the same: one /// held back that does and reads nothing is taken from where it is held, to build /// only where it is read, as an error `ok_or` makes in place. diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 41e37c162..c73a6de37 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -251,9 +251,7 @@ fn shake(out: &Path) { kept.sort_unstable(); let want = [ // The runtime's. - "Err", "ByteSlice", - "Ok", "Panic", "byteSliceFrom", "checked", @@ -271,6 +269,7 @@ fn shake(out: &Path) { "short", "slice", "u8", + "u8Out", ]; let mut want: Vec = want.map(unnumbered).to_vec(); want.sort_unstable();