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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions quest/m2/ts-byte-views/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Byte slices without objects
# [M] Byte slices without objects

## Goal

Expand All @@ -17,17 +17,19 @@ kept them as `(data, start, length)` made Chrome's varint decode 2.3-2.9x and fr

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 children below).
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.

## Required

- [Byte slices in fields](/quest/m2/ts-byte-views/fields.md) - a private slice field is three fields
25 changes: 0 additions & 25 deletions quest/m2/ts-byte-views/fields.md

This file was deleted.

3 changes: 3 additions & 0 deletions ts/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
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
the `Result`.
- A struct's private `&[u8]` or `&[u8; N]` field is three fields, its bytes, start, and
length, named for it: `buf` is `bufData`, `bufStart`, and `bufLen`, and a class's
constructor takes the three in its place. A `pub` field is still an `rt.ByteSlice`.
- `rt.btreeCollect` takes whether it sorts and the keys' `!ne` after the entries, and
`rt.btreeCollectKeys` the keys' `PartialOrd::lt` and `!ne`, as in
`rt.btreeCollect(m, entries, true, Key.eq$cmp_PartialEq)`; neither takes a test for keys
Expand Down
7 changes: 5 additions & 2 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,11 @@ crate translation reaches is refused, and so are two versions of one crate it re
`byteOffset` and `byteLength`, and copy it before transferring it.

An `rt.ByteSlice` is a range of a `Uint8Array`: its `data`, from `start`, for `length`
bytes. A caller passes bytes as `rt.byteSliceFrom(array)` and reads a slice it gets as
`rt.byteSliceView(slice)`, a view of it. A mutable part of a slice of other values, as
bytes. A struct's private `&[u8]` or `&[u8; N]` field is its three parts, `bufData`,
`bufStart`, and `bufLen` for `buf`, so a cursor moving through it makes no object; a
read that needs the slice whole makes it there. A caller passes bytes as
`rt.byteSliceFrom(array)` and reads a slice it gets as `rt.byteSliceView(slice)`, a view
of it. A mutable part of a slice of other values, as
`&mut v[a..b]`, `split_at_mut`, `chunks_mut`, and `split_mut` lend it, is a `Proxy` of an
array whose elements are the slice's, read and written where they are, since a JS array
has no views.
Expand Down
49 changes: 31 additions & 18 deletions ts/fixtures/codec/expected/coding/decode.Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import type * as Control from "../stats.Control.ts";
* [`DecodeError::Short`] can be retried once more bytes arrive.
*/
export interface Decoder {
buf: rt.ByteSlice;
bufData: Uint8Array;
bufStart: number;
bufLen: number;
form: Form.Form;
/** The length of the buffer this decoder started with. */
len: rt.usize;
Expand All @@ -31,7 +33,7 @@ export interface Decoder {

/** Read `buf`, with varints in the given form. */
function new_(buf: rt.ByteSlice, form: Form.Form): Decoder {
return { buf, form, len: buf.length, need: 0, control: undefined, uncounted: 0 };
return { bufData: buf.data, bufStart: buf.start, bufLen: buf.length, form, len: buf.length, need: 0, control: undefined, uncounted: 0 };
}

/**
Expand All @@ -45,28 +47,32 @@ export function need(self: Decoder): rt.usize {

/** Record that a read wanted `more` bytes past the current position. */
export function short(self: Decoder, more: rt.usize): DecodeError.DecodeError {
const need: rt.usize = rt.usizeSaturatingAdd(rt.usizeSub(self.len, self.buf.length), more);
const need: rt.usize = rt.usizeSaturatingAdd(rt.usizeSub(self.len, self.bufLen), more);
self.need = rt.max(self.need, need);
return { tag: "Short" } as DecodeError.DecodeError;
}

/** The number of unread bytes. */
export function remaining(self: Decoder): rt.usize {
return self.buf.length;
return self.bufLen;
}

/** Whether every byte has been read. */
export function isEmpty(self: Decoder): boolean {
return self.buf.length === 0;
return self.bufLen === 0;
}

/** Read `len` raw bytes. */
export function slice(self: Decoder, len: rt.usize): DecodeError.DecodeError | undefined {
const slice: rt.ByteSlice = self.buf;
if (len <= slice.length) {
self.buf = new rt.ByteSlice(slice.data, slice.start + len, slice.length - len);
sliceOut.pData = slice.data;
sliceOut.pStart = slice.start;
const _8Data: Uint8Array = self.bufData;
const _8Start: number = self.bufStart;
const _8Len: number = self.bufLen;
if (len <= _8Len) {
self.bufData = _8Data;
self.bufStart = _8Start + len;
self.bufLen = _8Len - len;
sliceOut.pData = _8Data;
sliceOut.pStart = _8Start;
sliceOut.pLen = len;
return undefined;
}
Expand All @@ -77,8 +83,10 @@ export const sliceOut: { pData: Uint8Array; pStart: number; pLen: number } = { p

/** Read every remaining byte. */
export function rest(self: Decoder): rt.ByteSlice {
const taken = self.buf;
self.buf = rt.byteSliceFrom(new Uint8Array(0));
const taken = new rt.ByteSlice(self.bufData, self.bufStart, self.bufLen);
self.bufData = new Uint8Array(0);
self.bufStart = 0;
self.bufLen = 0;
return taken;
}

Expand Down Expand Up @@ -147,18 +155,19 @@ export function bool(self: Decoder): rt.Result<boolean, DecodeError.DecodeError>
/** Read a varint. */
export function varint(self: Decoder): rt.Result<rt.u64, DecodeError.DecodeError> {
let _0!: rt.Result<rt.u64, DecodeError.DecodeError>;
const _13: rt.ByteSlice = self.buf;
const part: rt.u64 | undefined = read(_13.data, _13.start, _13.length, self.form);
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;
const part3: number = readOut.p_1Start;
const part4: number = readOut.p_1Len;
if (err2 === undefined) {
const value: rt.u64 = part as rt.u64;
self.buf = new rt.ByteSlice(part2, part3, part4);
self.bufData = part2;
self.bufStart = part3;
self.bufLen = part4;
_0 = rt.Ok(value);
} else if ((err2 as DecodeError.DecodeError).tag === "Short") {
_0 = rt.Err(short(self, rt.usizeAdd(self.buf.length, 1)));
_0 = rt.Err(short(self, rt.usizeAdd(self.bufLen, 1)));
} else {
_0 = rt.Err(err2 as DecodeError.DecodeError);
}
Expand All @@ -177,7 +186,9 @@ export function varintOpt(self: Decoder): rt.Result<rt.u64 | undefined, DecodeEr
/** Read a varint length, then that many raw bytes. */
export function bytes(self: Decoder): DecodeError.DecodeError | undefined {
let _0!: rt.Result<rt.ByteSlice, DecodeError.DecodeError>;
const start: rt.ByteSlice = self.buf;
const startData: Uint8Array = self.bufData;
const startStart: number = self.bufStart;
const startLen: number = self.bufLen;
const _6: rt.Result<rt.u64, DecodeError.DecodeError> = varint(self);
if (_6.ok) {
const _3: rt.Result<rt.usize, DecodeError.DecodeError> = result_mapErr$06mnjtr(rt.usizeTryFrom(_6.value), (_2: rt.TryFromIntError): DecodeError.DecodeError => {
Expand All @@ -189,7 +200,9 @@ export function bytes(self: Decoder): DecodeError.DecodeError | undefined {
const part2: number = sliceOut.pStart;
const part3: number = sliceOut.pLen;
const _15: (a0: DecodeError.DecodeError) => undefined = (_22: DecodeError.DecodeError): undefined => {
self.buf = start;
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<rt.ByteSlice, DecodeError.DecodeError>, _15);
} else {
Expand Down
10 changes: 6 additions & 4 deletions ts/fixtures/lang/expected/chunks.Header.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Generated by rust2-ts. Do not edit.

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

/** A header split off the front of its input, keeping both parts. */
export interface Header {
tag: rt.ByteSlice;
rest: rt.ByteSlice;
tagData: Uint8Array;
tagStart: number;
tagLen: number;
restData: Uint8Array;
restStart: number;
restLen: number;
}

//# sourceMappingURL=chunks.Header.ts.map
8 changes: 4 additions & 4 deletions ts/fixtures/lang/expected/chunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mapOr$1jgrjsu as option_mapOr$1jgrjsu } from "./core/option.ts";

export function header(dataData: Uint8Array, dataStart: number, dataLen: number): Header.Header | undefined {
if (dataLen >= 2) {
return { tag: new rt.ByteSlice(dataData, dataStart, 2), rest: new rt.ByteSlice(dataData, dataStart + 2, dataLen - 2) };
return { tagData: dataData, tagStart: dataStart, tagLen: 2, restData: dataData, restStart: dataStart + 2, restLen: dataLen - 2 };
}
return undefined;
}
Expand Down Expand Up @@ -64,9 +64,9 @@ export function chunks(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
const _6: Header.Header | undefined = header(data.data, data.start, data.length);
if (_6 !== undefined) {
const h: Header.Header = _6;
rt.byteVecExtendFromSlice(out, h.tag);
rt.byteVecPush(out, h.tag.data[h.tag.start + 1]);
rt.byteVecPush(out, h.rest.length & 0xff);
rt.byteVecExtendFromSlice(out, new rt.ByteSlice(h.tagData, h.tagStart, h.tagLen));
rt.byteVecPush(out, h.tagData[h.tagStart + 1]);
rt.byteVecPush(out, h.restLen & 0xff);
}
const err: rt.u8 | undefined = take4(data.data, data.start, data.length);
const part3: Uint8Array = take4Out.p_0Data;
Expand Down
6 changes: 4 additions & 2 deletions ts/fixtures/lang/expected/clones.Plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr } from "./core/option.ts";

/** What `turns` reads by: the case's bytes, each step's taken in order. */
export interface Plan {
data: rt.ByteSlice;
dataData: Uint8Array;
dataStart: number;
dataLen: number;
at: rt.usize;
}

export function next(self: Plan): rt.u8 {
const b: rt.u8 = option_unwrapOr$0r2bmpr(rt.byteSliceGet(self.data, rt.usizeRem(self.at, rt.max(self.data.length, 1))), 0);
const b: rt.u8 = option_unwrapOr$0r2bmpr(rt.byteSliceGet(new rt.ByteSlice(self.dataData, self.dataStart, self.dataLen), rt.usizeRem(self.at, rt.max(self.dataLen, 1))), 0);
self.at = rt.usizeAdd(self.at, 1);
return b ^ rt.u8WrappingMul(self.at & 0xff, 29);
}
Expand Down
2 changes: 1 addition & 1 deletion ts/fixtures/lang/expected/clones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6603,7 +6603,7 @@ export function iters(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
let _263!: [rt.ByteSlice, rt.ByteSlice];
let _269!: rt.usize;
let it!: rt.Peekable<rt.u8>;
const plan: Plan.Plan = { data, at: kind as rt.usize };
const plan: Plan.Plan = { dataData: data.data, dataStart: data.start, dataLen: data.length, at: kind as rt.usize };
const n: rt.usize = option_mapOr$1surhce(rt.byteSliceFirst(data), 1, (_2: rt.u8): rt.u8 => {
return rt.u8Add(rt.u8Rem(_2, 4), 1);
}) as rt.usize;
Expand Down
7 changes: 7 additions & 0 deletions ts/fixtures/lang/expected/core/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,13 @@ export function okOr$0tezn0w(self: rt.ByteSlice | undefined, _: undefined): rt.R
return rt.Ok(self);
}

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

export function okOr$0q0ogpn(self: [rt.ByteSlice, rt.ByteSlice] | undefined, err: rt.u8 | undefined): rt.Result<[rt.ByteSlice, rt.ByteSlice], rt.u8 | undefined> {
if (self === undefined) {
return rt.Err(err);
Expand Down
14 changes: 14 additions & 0 deletions ts/fixtures/lang/expected/core/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ export function isErrAnd$0b01a39(self: rt.Result<rt.u8, rt.u16>, f: (a0: rt.u16)
return f(self.error);
}

export function ok$04yj0hm(self: rt.Result<rt.ByteSlice, rt.usize>): rt.ByteSlice | undefined {
if (self.ok) {
return self.value;
}
return undefined;
}

export function ok$0lceojf(self: rt.Result<[rt.usize, rt.u8], rt.Arc<Any.Any>>): [rt.usize, rt.u8] | undefined {
let _0!: [rt.usize, rt.u8] | undefined;
if (self.ok) {
Expand Down Expand Up @@ -718,6 +725,13 @@ export function inspect$16o3gle(self: rt.Result<rt.u8, rt.u16>, f: (a0: rt.u8) =
return self;
}

export function inspectErr$0g3nypv(self: rt.Result<rt.ByteSlice, rt.usize>, f: (a0: rt.usize) => undefined): rt.Result<rt.ByteSlice, rt.usize> {
if (!self.ok) {
f(self.error);
}
return self;
}

export function inspectErr$06tv5sa(self: rt.Result<rt.u8, rt.u16>, f: (a0: rt.u16) => undefined): rt.Result<rt.u8, rt.u16> {
if (!self.ok) {
f(self.error);
Expand Down
33 changes: 33 additions & 0 deletions ts/fixtures/lang/expected/fields.Couple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Generated by rust2-ts. Do not edit.

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

export function fmt$fmt_Debug(self: Couple, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
const arg = new rt.ByteSlice(self.headData, self.headStart, self.headLen);
const _5: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg), f, rt.u8Debug);
const arg2 = new rt.ByteSlice(self.wordData, self.wordStart, self.wordLen);
const _3: string = "Couple";
const _4: string = "head";
const _7: string = "word";
const _8: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg2), f, rt.u8Debug);
return rt.debugStructFinish(rt.debugStructField(rt.debugStructField(rt.fmtDebugStruct(f, _3), _4, _5), _7, _8));
}

export function eq$cmp_PartialEq(self: Couple, other: Couple): boolean {
if (!rt.byteSliceEq(new rt.ByteSlice(self.headData, self.headStart, self.headLen), new rt.ByteSlice(other.headData, other.headStart, other.headLen))) {
return false;
}
return rt.eqBytes(new rt.ByteSlice(self.wordData, self.wordStart, self.wordLen), new rt.ByteSlice(other.wordData, other.wordStart, other.wordLen));
}

/** A `Copy` struct of a slice and an array's reference. */
export interface Couple {
headData: Uint8Array;
headStart: number;
headLen: number;
wordData: Uint8Array;
wordStart: number;
wordLen: number;
}

//# sourceMappingURL=fields.Couple.ts.map
20 changes: 20 additions & 0 deletions ts/fixtures/lang/expected/fields.Exposed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Generated by rust2-ts. Do not edit.

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

export function fmt$fmt_Debug(self: Exposed, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
const _3: string = "Exposed";
const _4: string = "buf";
const _5: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(self.buf), f, rt.u8Debug);
const _7: string = "at";
const _8: rt.Fmt = (f: rt.Formatter) => rt.u8Debug(self.at, f);
return rt.debugStructFinish(rt.debugStructField(rt.debugStructField(rt.fmtDebugStruct(f, _3), _4, _5), _7, _8));
}

/** A `pub` field, which stays whole. */
export interface Exposed {
buf: rt.ByteSlice;
at: rt.u8;
}

//# sourceMappingURL=fields.Exposed.ts.map
24 changes: 24 additions & 0 deletions ts/fixtures/lang/expected/fields.Glued.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Generated by rust2-ts. Do not edit.

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

export function fmt$fmt_Debug(self: Glued, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
const arg = new rt.ByteSlice(self._0Data, self._0Start, self._0Len);
const _4: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg), f, rt.u8Debug);
const arg2 = new rt.ByteSlice(self._1Data, self._1Start, self._1Len);
const _3: string = "Glued";
const _6: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg2), f, rt.u8Debug);
return rt.debugTupleFinish(rt.debugTupleField(rt.debugTupleField(rt.fmtDebugTuple(f, _3), _4), _6));
}

/** A tuple struct of two slices, one a constant's. */
export interface Glued {
_0Data: Uint8Array;
_0Start: number;
_0Len: number;
_1Data: Uint8Array;
_1Start: number;
_1Len: number;
}

//# sourceMappingURL=fields.Glued.ts.map
Loading