From f900abcd4376afc8a7e8e7c9cc68f2b20e1c6c2c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:32:32 -0700 Subject: [PATCH 1/2] chore(quest): claim quest/m3/ts-byte-key-refs.md Quest-Claim: 8fb63f20dd30ede5 From 3fd141fb096722f349c4265cdaf124b7a3ce5aed Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:03:23 -0700 Subject: [PATCH 2/2] feat(ts): lend the byte arrays a collection holds as slices What a map, set, deque, or heap of `[u8; N]` keys or values lends out, by `keys`, `iter`, `get`, `get_key_value`, `range`, an entry's `key`, `Index`, `for` over a reference, or `retain`'s and `or_insert_with_key`'s closures, is an `rt.ByteSlice` of the bytes it holds, as a `&[u8; N]` is elsewhere. `HashMap::get_key_value` and `BTreeSet::get` translate, and `map[&key]` finds a byte-array key. Extending one by references to arrays, or holding a `&[u8; N]` beside a `[u8; N]`, is refused with a span. Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 3 +- quest/m3/ts-byte-key-refs.md | 17 - quest/m3/ts-byte-refs-taken.md | 20 + quest/m3/ts-set-ops.md | 16 + ts/README.md | 5 +- ts/fixtures/lang/expected/core/option.ts | 14 + ts/fixtures/lang/expected/harness.ts | 6 +- ts/fixtures/lang/expected/keys.ts | 528 ++++++++++++++++++++++- ts/fixtures/lang/src/harness.rs | 2 + ts/fixtures/lang/src/keys.rs | 252 ++++++++++- ts/fixtures/reject/census.md | 30 +- ts/fixtures/reject/expected.txt | 3 +- ts/fixtures/reject/src/lib.rs | 11 +- ts/fixtures/reject/ts.toml | 1 + ts/runtime/rt.ts | 5 + ts/runtime/rt/collections.ts | 18 +- ts/src/lower/collections.rs | 212 ++++++++- ts/src/lower/mir/call.rs | 22 +- 18 files changed, 1083 insertions(+), 82 deletions(-) delete mode 100644 quest/m3/ts-byte-key-refs.md create mode 100644 quest/m3/ts-byte-refs-taken.md create mode 100644 quest/m3/ts-set-ops.md diff --git a/quest/m3/README.md b/quest/m3/README.md index e2b7da99a..0f048a659 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -28,7 +28,8 @@ generic std gap is planned here even when moq never calls it. - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies - [A B-tree's drop order](/quest/m3/ts-btree-drop.md) - dropping a whole `BTreeMap` or `BTreeSet` drops its entries node by node, as std's does - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do -- [References to byte-array keys a collection yields](/quest/m3/ts-byte-key-refs.md) - `keys`, `iter`, `get_key_value`, and `HashSet::get` of a `[u8; N]` key translate +- [Set operations](/quest/m3/ts-set-ops.md) - `union`, `intersection`, `difference`, and `symmetric_difference` of hash and B-tree sets translate +- [References to byte arrays std takes](/quest/m3/ts-byte-refs-taken.md) - `extend` by references to arrays, adaptors' closures over held arrays, and a collection of both forms translate - [Vec and slice accessors of elements whose None is rt.None](/quest/m3/ts-option-elements.md) - `Vec::pop` and `slice::last` of `Option` elements keep `None` apart from `Some(None)` - [Size hints of std's iterators](/quest/m3/ts-size-hints.md) - `size_hint` translates, and `Vec`'s collect and extend panic past `isize::MAX` as std's do - [Peekable::next_if_eq against another type](/quest/m3/ts-next-if-eq-other.md) - `next_if_eq` with an expected value of another type than the item, such as a `&str` of `String` items, translates diff --git a/quest/m3/ts-byte-key-refs.md b/quest/m3/ts-byte-key-refs.md deleted file mode 100644 index 09e86562d..000000000 --- a/quest/m3/ts-byte-key-refs.md +++ /dev/null @@ -1,17 +0,0 @@ -# [S] References to byte-array keys a collection yields - -## Goal - -rust2-ts translates what lends out a collection's `[u8; N]` key, such as `keys`, `iter`, -`get_key_value`, `Entry::key`, and `HashSet::get` and `iter`, each reference an -`rt.ByteSlice` of the key's bytes, as a `&[u8; N]` is elsewhere. Proven by differential -tests against native Rust. - -## Plan - -Each refuses with a span today, as "`HashMap::keys` of a reference to an array of bytes it -is generic over" (`bytes_across` in `src/lower/mir/call.rs`): the collection holds the key -as a typed array, which its mapping would hand back where the caller reads a slice. -Lookups by such a reference already translate, since they only read the key. - -Public API: the translator's output. diff --git a/quest/m3/ts-byte-refs-taken.md b/quest/m3/ts-byte-refs-taken.md new file mode 100644 index 000000000..09e58bcb6 --- /dev/null +++ b/quest/m3/ts-byte-refs-taken.md @@ -0,0 +1,20 @@ +# [M] References to byte arrays std takes + +## Goal + +The calls still refused as "of a reference to an array of bytes it is generic over" +translate, proven by differential tests against native Rust: a collection of `[u8; N]` +extended by references to arrays, std's adaptors handing their closure a reference to a +`[u8; N]` item they hold (`filter` over owned arrays, `map` then `collect` over a map's +`iter`), and a collection holding a `&[u8; N]` beside a `[u8; N]`. + +## Plan + +The reject fixture's `byte_extended`, `filter_owned_bytes`, and `byte_key` show each. +`extend` would store the `rt.ByteSlice` it is given where the collection holds a +`Uint8Array`, so it copies each. An adaptor's closure takes a slice of the item, as +`collections::lend_closure` wraps a collection's. A collection holding both forms can't +tell a held `&[u8; N]` from a lent one by the instantiated type alone; the method's generic +signature, `&K` against `K`, can. + +Public API: the translator's output. diff --git a/quest/m3/ts-set-ops.md b/quest/m3/ts-set-ops.md new file mode 100644 index 000000000..822568155 --- /dev/null +++ b/quest/m3/ts-set-ops.md @@ -0,0 +1,16 @@ +# [S] Set operations + +## Goal + +`union`, `intersection`, `difference`, and `symmetric_difference` of a `HashSet` and a +`BTreeSet` translate, each yielding what std's iterator yields in std's order for a +B-tree, proven by a differential test against native Rust that covers `[u8; N]` elements. + +## Plan + +Each is an unsupported call today for every element type. A hash set's order is the +runtime's, so the hash forms need only yield the same elements; a B-tree's merge order is +std's. Their items are `&T`, which the collection mappings already lend as a slice where +`T` is a `[u8; N]`. + +Public API: the translator's output. diff --git a/ts/README.md b/ts/README.md index 7578507f4..06bf0ffa4 100644 --- a/ts/README.md +++ b/ts/README.md @@ -273,8 +273,9 @@ finds any other key, a 64-bit one past 2^53 included, through its translated `Ha `Option` orders `None` first, an `Arc` compares, orders, and hashes as what it holds, so a map of `Arc` finds a `&str`, and byte arrays, slices, and vectors order by their bytes and then their length, and compare and hash alike whatever their form, so a map of -`Vec` finds a `&[u8]`. A collection's reference to a byte array it holds as a key, as -`keys` and `HashSet::get` return, is refused. A `&mut` to a value a map holds that JS copies, +`Vec` finds a `&[u8]`. A collection's reference to a byte array it holds, as `keys`, +`HashSet::get`, and `retain`'s closure get one, is an `rt.ByteSlice` of its bytes, as a +`&[u8; N]` is elsewhere; a `&mut` writes where the collection holds them. A `&mut` to a value a map holds that JS copies, such as a number, from `get_mut` or the entry API, is an `rt.Place` whose `v` reads and writes the map's slot. A hash map iterates in an order of the runtime's, mostly insertion order, which std leaves unspecified, and hashes a key diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 968055a4f..60eca48b1 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -736,6 +736,13 @@ export function mapOr$1t0muvr(self: rt.u8 | undefined, default_: rt.usize, f: (a return f(self); } +export function mapOr$1w1gbwl(self: rt.ByteSlice | undefined, default_: rt.u8, f: (a0: rt.ByteSlice) => rt.u8): rt.u8 { + if (self === undefined) { + return default_; + } + return f(self); +} + export function mapOr$0sl5app(self: Uint8Array | undefined, default_: rt.u8, f: (a0: Uint8Array) => rt.u8): rt.u8 { if (self === undefined) { return default_; @@ -743,6 +750,13 @@ export function mapOr$0sl5app(self: Uint8Array | undefined, default_: rt.u8, f: return f(self); } +export function mapOr$1be3nsj(self: [rt.ByteSlice, rt.u8] | undefined, default_: rt.u8, f: (a0: [rt.ByteSlice, rt.u8]) => rt.u8): rt.u8 { + if (self === undefined) { + return default_; + } + return f(self); +} + export function mapOr$0z7fb2w(self: rt.ByteSlice | undefined, default_: Uint8Array, f: (a0: rt.ByteSlice) => Uint8Array): Uint8Array { if (self === undefined) { return default_; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 22487a7bf..4d5be675d 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -25,7 +25,7 @@ import { values, random, bytes as hashes_bytes } from "./hashes.ts"; import { holds } from "./holds.ts"; import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast as ints_cast, convert, parses as ints_parses, widenings } from "./ints.ts"; import { adaptors, consumers, reducers, skips, ranked, ranges, options, own, values as iters_values, returned } from "./iters.ts"; -import { keys, overrides, bytes as keys_bytes } from "./keys.ts"; +import { keys, overrides, bytes as keys_bytes, lentKeys, lentValues } from "./keys.ts"; import { locals } from "./locals.ts"; import { locks, relock, rwlocks } from "./locks.ts"; import { nested } from "./nested.ts"; @@ -90,7 +90,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) refs(data21, out21); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2121: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _396: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _3100: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2123: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); @@ -180,7 +180,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) made(data65, out66); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::as_slices", (_2166: rt.u8, data66: rt.ByteSlice, out67: rt.ByteVec): void => { asSlices(data66, out67); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::widenings", widenings] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::widenings", widenings] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::lent_keys", lentKeys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::lent_values", lentValues] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/keys.ts b/ts/fixtures/lang/expected/keys.ts index 0533ac71e..b927b055f 100644 --- a/ts/fixtures/lang/expected/keys.ts +++ b/ts/fixtures/lang/expected/keys.ts @@ -3,7 +3,7 @@ import * as rt from "./rt.ts"; import { copy$Hop, copy$Horizon, copy$Option_u8s, copy$u8_Horizon, copy$u8s, drop$u8_Reverse_Arc_Lopsided } from "./lib.ts"; import { thenSome$0rsp0eu } from "./core/bool.ts"; -import { unwrapOr$1tvry1i, unwrapOr$1j4op5y, unwrapOr$0r2bmpr, map$120lney, map$146awhd, map$0lcwx9x, map$0gn4nwv, mapOr$04khdaj, mapOr$1dgdmlk, mapOr$1surhce, mapOr$02k4ito } from "./core/option.ts"; +import { unwrapOr$1tvry1i, unwrapOr$1j4op5y, unwrapOr$0r2bmpr, map$120lney, map$146awhd, map$0lcwx9x, map$0gn4nwv, mapOr$04khdaj, mapOr$1dgdmlk, mapOr$1a3irjf, mapOr$1surhce, mapOr$1w1gbwl, mapOr$1be3nsj, mapOr$0z7fb2w, mapOr$02k4ito } from "./core/option.ts"; import * as Hop from "./keys.Hop.ts"; import * as Horizon from "./keys.Horizon.ts"; import * as Liar from "./keys.Liar.ts"; @@ -1044,4 +1044,530 @@ export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.hashDrop(shared, rt.drop); } +/** + * Keys that are `[u8; N]`, lent as std lends them by every map and set: read, compared + * with one another and the keys they were made from, looked up by, and copied out and + * written, which leaves the collection's own as they were. A hash collection's order is + * the runtime's, so what it yields is sorted first. + */ +export function lentKeys(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const pairs: Uint8Array[] = rt.iterToArray(rt.iterMap(rt.byteSliceWindows(data, 2), (w: rt.ByteSlice): Uint8Array => { + rt.idx(w, 0); + const _3: rt.u8 = rt.u8Rem(w.data[w.start], 4); + rt.idx(w, 1); + return Uint8Array.of(_3, rt.u8Rem(w.data[w.start + 1], 4)); + })); + const probe: Uint8Array = Uint8Array.of(rt.u8Rem(kind, 4), rt.u8Rem(rt.u8Div(kind, 4), 4)); + const map: rt.HashMap = rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashMap; + const tree: rt.BTreeMap = rt.btreeNew(rt.cmpBytes) as rt.BTreeMap; + const set: rt.HashSet = rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet; + const ordered: rt.BTreeSet = rt.btreeNew(rt.cmpBytes) as rt.BTreeSet; + const iter: rt.Iter<[rt.usize, rt.ByteSlice]> = rt.iterEnumerate(rt.iterMap(rt.iterSlice(pairs), rt.byteSliceFrom)); + while (true) { + const _24: [rt.usize, rt.ByteSlice] | rt.Done = iter.next(); + if (_24 === rt.DONE) { + break; + } + const i: rt.usize = _24[0]; + const key: Uint8Array = copy$u8s(rt.byteSliceToArray(_24[1])); + rt.hashInsert(map, copy$u8s(key), i & 0xff); + rt.btreeInsert(tree, copy$u8s(key), i & 0xff); + rt.hashAdd(set, copy$u8s(key)); + rt.btreeAdd(ordered, copy$u8s(key)); + key[0] = key[0] ^ 16; + rt.byteVecPush(out, rt.hashContains(map, rt.byteSliceView(rt.byteSliceFrom(key))) ? 1 : 0); + } + const lent: rt.ByteSlice[] = rt.iterToArray(rt.iterMap(rt.hashKeys(map), rt.byteSliceFrom)); + rt.sliceSort(lent, rt.cmpLt(rt.cmpBytes)); + for (const k of lent) { + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(k.data[k.start], k.data[k.start + 1], rt.hashIndex(map, rt.byteSliceView(k)), rt.hashContains(map, rt.byteSliceView(k)) ? 1 : 0, rt.eqBytes(k, rt.byteSliceFrom(probe)) ? 1 : 0, rt.cmpBytes(k, rt.byteSliceFrom(probe)) === -1 ? 1 : 0))); + rt.byteVecPush(out, unwrapOr$0r2bmpr(rt.btreeGet(tree, rt.byteSliceView(k)), 221)); + rt.byteVecPush(out, mapOr$1w1gbwl(rt.optionMap(rt.hashKey(set, rt.byteSliceView(k)), rt.byteSliceFrom), 221, (s: rt.ByteSlice): rt.u8 => { + return rt.eqBytes(s, k) ? 1 : 0; + })); + const copy: Uint8Array = rt.byteSliceToArray(k); + copy[1] = copy[1] ^ 32; + rt.byteVecPush(out, rt.hashContains(map, rt.byteSliceView(rt.byteSliceFrom(copy))) ? 1 : 0); + } + rt.byteVecPush(out, rt.iterAll(rt.iterSlice(lent), (k3: rt.ByteSlice): boolean => { + return rt.hashContains(map, rt.byteSliceView(k3)); + }) ? 1 : 0); + const entries: [Uint8Array, rt.u8][] = []; + const iter3: rt.Iter<[rt.ByteSlice, rt.u8]> = rt.iterMap(rt.hashIter(map), (held): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held[0]), held[1]]; + }); + while (true) { + const _120: [rt.ByteSlice, rt.u8] | rt.Done = iter3.next(); + if (_120 === rt.DONE) { + break; + } + entries.push([rt.byteSliceToArray(_120[0]), _120[1]] as [Uint8Array, rt.u8]); + } + rt.sliceSort(entries, rt.cmpLt(rt.cmpTuple([rt.cmpBytes, rt.cmp]))); + for (const _136 of entries) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(_136[0])); + rt.byteVecPush(out, _136[1]); + } + let sum: rt.u8 = 0; + const iter5: rt.Iter<[rt.ByteSlice, rt.Place]> = rt.iterMap(rt.hashIterMut(map), (held2): [rt.ByteSlice, rt.Place] => { + return [rt.byteSliceFrom(held2[0]), held2[1]]; + }); + while (true) { + const _149: [rt.ByteSlice, rt.Place] | rt.Done = iter5.next(); + if (_149 === rt.DONE) { + break; + } + _149[1].v = rt.u8WrappingAdd(_149[1].v, _149[0].data[_149[0].start]); + sum = rt.u8WrappingAdd(rt.u8WrappingAdd(sum, _149[0].data[_149[0].start + 1]), _149[1].v); + } + const iter6: rt.Iter<[rt.ByteSlice, rt.Place]> = rt.iterMap(rt.hashIterMut(map), (held3): [rt.ByteSlice, rt.Place] => { + return [rt.byteSliceFrom(held3[0]), held3[1]]; + }); + while (true) { + const _170: [rt.ByteSlice, rt.Place] | rt.Done = iter6.next(); + if (_170 === rt.DONE) { + break; + } + _170[1].v = _170[1].v ^ _170[0].data[_170[0].start + 1]; + } + const iter7: rt.Iter<[rt.ByteSlice, rt.u8]> = rt.iterMap(rt.hashIter(map), (held4): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held4[0]), held4[1]]; + }); + while (true) { + const _181: [rt.ByteSlice, rt.u8] | rt.Done = iter7.next(); + if (_181 === rt.DONE) { + break; + } + sum = rt.u8WrappingAdd(sum, _181[0].data[_181[0].start] ^ _181[1]); + } + rt.byteVecPush(out, sum); + rt.byteVecPush(out, mapOr$1be3nsj(rt.optionMap(rt.hashGetKeyValue(map, rt.byteSliceView(rt.byteSliceFrom(probe))), (held5): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held5[0]), held5[1]]; + }), 221, (_2: [rt.ByteSlice, rt.u8]): rt.u8 => { + return _2[0].data[_2[0].start] ^ _2[0].data[_2[0].start + 1] ^ _2[1]; + })); + rt.byteVecPush(out, mapOr$1be3nsj(rt.optionMap(rt.btreeGetKeyValue(tree, rt.byteSliceView(rt.byteSliceFrom(probe))), (held6): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held6[0]), held6[1]]; + }), 221, (_22: [rt.ByteSlice, rt.u8]): rt.u8 => { + return _22[0].data[_22[0].start] ^ _22[0].data[_22[0].start + 1] ^ _22[1]; + })); + const _206: rt.Entry = rt.entry(map, copy$u8s(probe)); + if (rt.entryOccupied(_206)) { + const o: rt.Entry = _206; + const _212: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(o)); + const _211: rt.u8 = _212.data[_212.start]; + const _217: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(o)); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(1, _211, _217.data[_217.start + 1], rt.entryGet(o)))); + } else { + const v: rt.Entry = _206; + const _227: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(v)); + const _226: rt.u8 = _227.data[_227.start]; + const _232: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(v)); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(0, _226, _232.data[_232.start + 1]))); + } + const _239: rt.Entry = rt.entry(map, copy$u8s(probe)); + const f = (k4: rt.ByteSlice): rt.u8 => { + return rt.u8WrappingMul(k4.data[k4.start], 3) ^ k4.data[k4.start + 1]; + }; + rt.byteVecPush(out, rt.entryOrInsertWithMut(_239, (held7) => f(rt.byteSliceFrom(held7))).v); + const _243: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(rt.entry(map, Uint8Array.of(3, 3)))); + rt.byteVecPush(out, _243.data[_243.start + 1]); + const _251: rt.Entry = rt.entry(tree, copy$u8s(probe)); + if (!rt.entryOccupied(_251)) { + const v2: rt.Entry = _251; + const _272: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(v2)); + const _271: rt.u8 = _272.data[_272.start]; + const _277: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(v2)); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(0, _271, _277.data[_277.start + 1]))); + } else { + const o2: rt.Entry = _251; + const _257: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(o2)); + const _256: rt.u8 = _257.data[_257.start]; + const _262: rt.ByteSlice = rt.byteSliceFrom(rt.entryKey(o2)); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(1, _256, _262.data[_262.start + 1], rt.entryGet(o2)))); + } + const _284: rt.Entry = rt.entry(tree, copy$u8s(probe)); + const f2 = (k5: rt.ByteSlice): rt.u8 => { + return rt.u8WrappingMul(k5.data[k5.start + 1], 5) ^ k5.data[k5.start]; + }; + rt.byteVecPush(out, rt.entryOrInsertWithMut(_284, (held8) => f2(rt.byteSliceFrom(held8))).v); + const _286: rt.Entry | undefined = rt.btreeFirstEntry(tree); + if (_286 !== undefined) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.entryKey(_286))); + } + const _294: rt.Entry | undefined = rt.btreeFirstEntry(tree, true); + if (_294 !== undefined) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.entryKey(_294))); + } + const members: Uint8Array[] = rt.iterToArray(rt.iterCopied(rt.iterMap(rt.hashKeys(set), rt.byteSliceFrom), rt.byteSliceToArray)); + rt.sliceSort(members, rt.cmpLt(rt.cmpBytes)); + const iter8: rt.Iter = rt.iterMap(rt.iterSlice(members), rt.byteSliceFrom); + while (true) { + const _312: rt.ByteSlice | rt.Done = iter8.next(); + if (_312 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _312); + } + const iter9: rt.Iter = rt.iterMap(rt.hashKeys(set), rt.byteSliceFrom); + while (true) { + const _321: rt.ByteSlice | rt.Done = iter9.next(); + if (_321 === rt.DONE) { + break; + } + const _329: rt.ByteSlice | undefined = rt.optionMap(rt.btreeGetKey(ordered, rt.byteSliceView(_321)), rt.byteSliceFrom); + rt.byteVecPush(out, rt.eqOption(rt.eqBytes)(_329, _321) ? 1 : 0); + } + const window: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 2)); + rt.byteVecPush(out, mapOr$1w1gbwl(rt.optionMap(rt.hashKey(set, rt.byteSliceView(window)), rt.byteSliceFrom), 221, (k6: rt.ByteSlice): rt.u8 => { + return k6.data[k6.start] ^ ((k6.data[k6.start + 1] << 2) & 0xff); + })); + const _345: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(probe), 0); + rt.byteVecPush(out, mapOr$1w1gbwl(rt.optionMap(rt.hashKey(set, rt.byteSliceView(_345)), rt.byteSliceFrom), 221, (k7: rt.ByteSlice): rt.u8 => { + return k7.data[k7.start] ^ ((k7.data[k7.start + 1] << 2) & 0xff); + })); + const _353: rt.ByteSlice = rt.byteSliceFrom(probe); + rt.byteVecPush(out, mapOr$1w1gbwl(rt.optionMap(rt.hashKey(set, rt.byteSliceView(rt.byteSliceFrom(probe))), rt.byteSliceFrom), 221, (k8: rt.ByteSlice): rt.u8 => { + return rt.eqBytes(k8, _353) ? 1 : 0; + })); + const _358: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(probe), 0); + rt.byteVecPush(out, mapOr$1w1gbwl(rt.optionMap(rt.btreeGetKey(ordered, rt.byteSliceView(_358)), rt.byteSliceFrom), 221, (k9: rt.ByteSlice): rt.u8 => { + return k9.data[k9.start] ^ ((k9.data[k9.start + 1] << 2) & 0xff); + })); + const iter10: rt.Iter<[rt.ByteSlice, rt.u8]> = rt.iterRev(rt.iterMap(rt.btreeIter(tree), (held9): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held9[0]), held9[1]]; + })); + while (true) { + const _365: [rt.ByteSlice, rt.u8] | rt.Done = iter10.next(); + if (_365 === rt.DONE) { + break; + } + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_365[0].data[_365[0].start], _365[0].data[_365[0].start + 1], _365[1]))); + } + const iter11: rt.Iter = rt.iterMap(rt.btreeKeys(tree), rt.byteSliceFrom); + while (true) { + const _383: rt.ByteSlice | rt.Done = iter11.next(); + if (_383 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _383); + } + const iter12: rt.Iter<[rt.ByteSlice, rt.u8]> = rt.iterMap(rt.btreeRange(tree, new rt.Range(Uint8Array.of(1, 0), Uint8Array.of(3, 0))), (held10): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held10[0]), held10[1]]; + }); + while (true) { + const _396: [rt.ByteSlice, rt.u8] | rt.Done = iter12.next(); + if (_396 === rt.DONE) { + break; + } + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_396[0].data[_396[0].start], _396[0].data[_396[0].start + 1], _396[1]))); + } + const iter13: rt.Iter<[rt.ByteSlice, rt.Place]> = rt.iterMap(rt.btreeRangeMut(tree, { start: copy$u8s(probe) }), (held11): [rt.ByteSlice, rt.Place] => { + return [rt.byteSliceFrom(held11[0]), held11[1]]; + }); + while (true) { + const _415: [rt.ByteSlice, rt.Place] | rt.Done = iter13.next(); + if (_415 === rt.DONE) { + break; + } + _415[1].v = rt.u8WrappingAdd(_415[1].v, _415[0].data[_415[0].start]); + } + const iter14: rt.Iter<[rt.ByteSlice, rt.u8]> = rt.iterMap(rt.btreeIter(tree), (held12): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held12[0]), held12[1]]; + }); + while (true) { + const _428: [rt.ByteSlice, rt.u8] | rt.Done = iter14.next(); + if (_428 === rt.DONE) { + break; + } + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_428[0].data[_428[0].start + 1], _428[1]))); + } + const iter15: rt.Iter<[rt.ByteSlice, rt.Place]> = rt.iterMap(rt.btreeIterMut(tree), (held13): [rt.ByteSlice, rt.Place] => { + return [rt.byteSliceFrom(held13[0]), held13[1]]; + }); + while (true) { + const _442: [rt.ByteSlice, rt.Place] | rt.Done = iter15.next(); + if (_442 === rt.DONE) { + break; + } + _442[1].v = _442[1].v ^ _442[0].data[_442[0].start]; + } + rt.byteVecPush(out, mapOr$1be3nsj(rt.optionMap(rt.btreeFirst(tree), (held14): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held14[0]), held14[1]]; + }), 221, (_23: [rt.ByteSlice, rt.u8]): rt.u8 => { + return _23[0].data[_23[0].start] ^ _23[1]; + })); + rt.byteVecPush(out, mapOr$1be3nsj(rt.optionMap(rt.btreeFirst(tree, true), (held15): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held15[0]), held15[1]]; + }), 221, (_25: [rt.ByteSlice, rt.u8]): rt.u8 => { + return _25[0].data[_25[0].start + 1] ^ _25[1]; + })); + const iter16: rt.Iter = rt.iterRev(rt.iterMap(rt.btreeKeys(ordered), rt.byteSliceFrom)); + while (true) { + const _463: rt.ByteSlice | rt.Done = iter16.next(); + if (_463 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _463); + } + const iter17: rt.Iter = rt.iterMap(rt.btreeRangeKeys(ordered, { end: copy$u8s(probe), inclusive: true }), rt.byteSliceFrom); + while (true) { + const _474: rt.ByteSlice | rt.Done = iter17.next(); + if (_474 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _474); + } + const iter18: rt.Iter = rt.iterMap(rt.btreeKeys(ordered), rt.byteSliceFrom); + while (true) { + const _483: rt.ByteSlice | rt.Done = iter18.next(); + if (_483 === rt.DONE) { + break; + } + rt.byteVecPush(out, _483.data[_483.start] ^ _483.data[_483.start + 1]); + } + rt.iterExtendBytes(out, rt.iterSlice(mapOr$0z7fb2w(rt.optionMap(rt.btreeFirstKey(ordered), rt.byteSliceFrom), rt.bytes(2).fill(221), (k10: rt.ByteSlice): Uint8Array => { + return rt.byteSliceToArray(k10); + }))); + rt.iterExtendBytes(out, rt.iterSlice(mapOr$0z7fb2w(rt.optionMap(rt.btreeFirstKey(ordered, true), rt.byteSliceFrom), rt.bytes(2).fill(221), (k11: rt.ByteSlice): Uint8Array => { + return rt.byteSliceToArray(k11); + }))); + const f3 = (k12: rt.ByteSlice, v3: rt.Place): boolean => { + if (!(k12.data[k12.start] <= k12.data[k12.start + 1])) { + return rt.u8Rem(v3.v, 2) === 0; + } + return true; + }; + rt.hashRetainMut(map, (held16, held17) => f3(rt.byteSliceFrom(held16), held17)); + const f4 = (k13: rt.ByteSlice, _316: rt.Place): boolean => { + return k13.data[k13.start] !== k13.data[k13.start + 1]; + }; + rt.btreeRetainMut(tree, (held18, held19) => f4(rt.byteSliceFrom(held18), held19)); + const _512: rt.ByteSlice = rt.byteSliceFrom(probe); + const _511: (a0: rt.ByteSlice) => boolean = (k14: rt.ByteSlice): boolean => { + return k14.data[k14.start + 1] !== _512.data[_512.start + 1]; + }; + rt.hashRetain(set, (held20) => _511(rt.byteSliceFrom(held20))); + const f5 = (k15: rt.ByteSlice): boolean => { + return k15.data[k15.start] < 2; + }; + rt.btreeRetain(ordered, (held21) => f5(rt.byteSliceFrom(held21))); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.hashLen(map) & 0xff, rt.btreeLen(tree) & 0xff, rt.hashLen(set) & 0xff, rt.btreeLen(ordered) & 0xff))); + const _529: rt.ByteSlice | undefined = rt.optionMap(rt.btreeFirstKey(ordered), rt.byteSliceFrom); + if (_529 !== undefined) { + const copy2: Uint8Array = rt.byteSliceToArray(_529); + copy2[0] = 255; + rt.byteVecPush(out, rt.btreeContains(ordered, rt.byteSliceView(rt.byteSliceFrom(copy2))) ? 1 : 0); + } + rt.byteVecPush(out, rt.iterAll(rt.iterMap(rt.btreeKeys(ordered), rt.byteSliceFrom), (k16: rt.ByteSlice): boolean => { + return k16.data[k16.start] < 2; + }) ? 1 : 0); + const _548: rt.Iter<[rt.ByteVec, rt.u8]> = rt.iterMap(rt.byteSliceChunks(data, 3), (c: rt.ByteSlice): [rt.ByteVec, rt.u8] => { + return [rt.byteSliceToByteVec(c), c.length & 0xff] as [rt.ByteVec, rt.u8]; + }); + const lens: rt.ByteVec[] = rt.iterToArray(rt.hashKeys(rt.hashExtend(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashMap, _548))); + rt.sliceSort(lens, rt.cmpLt(rt.cmpBytes)); + for (const _559 of lens) { + rt.byteVecPush(out, _559.length & 0xff); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_559)); + } + const _569: rt.Iter = rt.iterMap(rt.byteSliceChunks(data, 2), (a0: rt.ByteSlice): rt.ByteVec => { + return rt.byteSliceToByteVec(a0); + }); + rt.byteVecPush(out, mapOr$1a3irjf(rt.hashKey(rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet, _569), window), 221, (k17: rt.ByteVec): rt.u8 => { + return k17.length & 0xff; + })); + const _576: rt.Iter = rt.byteSliceChunks(data, 2); + const borrowed: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmpBytes) as rt.BTreeSet, _576, true); + const iter20: rt.Iter = rt.btreeKeys(borrowed); + while (true) { + const _580: rt.ByteSlice | rt.Done = iter20.next(); + if (_580 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _580); + } + rt.byteVecPush(out, mapOr$1w1gbwl(rt.btreeGetKey(borrowed, window), 221, (k18: rt.ByteSlice): rt.u8 => { + return k18.length & 0xff; + })); + const empty: rt.BTreeMap = rt.btreeNew(rt.cmpBytes) as rt.BTreeMap; + const none: rt.HashSet = rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.iterCount(rt.iterMap(rt.btreeKeys(empty), rt.byteSliceFrom)) & 0xff, rt.optionMap(rt.btreeFirst(empty), (held22): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held22[0]), held22[1]]; + }) === undefined ? 1 : 0, rt.iterCount(rt.iterMap(rt.btreeRange(empty, { start: copy$u8s(probe) }), (held23): [rt.ByteSlice, rt.u8] => { + return [rt.byteSliceFrom(held23[0]), held23[1]]; + })) & 0xff, rt.iterCount(rt.iterMap(rt.hashKeys(none), rt.byteSliceFrom)) & 0xff, rt.optionMap(rt.hashKey(none, rt.byteSliceView(rt.byteSliceFrom(probe))), rt.byteSliceFrom) === undefined ? 1 : 0))); +} + +/** + * Values that are `[u8; 2]`, and the items of a deque and a heap, lent as keys are: a + * `&` reads them and a `&mut` writes where the collection holds them. + */ +export function lentValues(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const map: rt.HashMap = rt.hashNew() as rt.HashMap; + const tree: rt.BTreeMap = rt.btreeNew(rt.cmp) as rt.BTreeMap; + const deque: rt.VecDeque = rt.dequeNew() as rt.VecDeque; + const heap: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; + const iter: rt.Iter = rt.byteSliceWindows(data, 2); + while (true) { + const _11: rt.ByteSlice | rt.Done = iter.next(); + if (_11 === rt.DONE) { + break; + } + rt.idx(_11, 0); + const _16: rt.u8 = _11.data[_11.start]; + rt.idx(_11, 1); + const value: Uint8Array = Uint8Array.of(_16, rt.u8Rem(_11.data[_11.start + 1], 8)); + rt.idx(_11, 0); + rt.hashInsert(map, rt.u8Rem(_11.data[_11.start], 8), copy$u8s(value)); + rt.idx(_11, 1); + rt.btreeInsert(tree, rt.u8Rem(_11.data[_11.start + 1], 8), copy$u8s(value)); + rt.dequePushBack(deque, copy$u8s(value)); + rt.heapPush(heap, copy$u8s(value), rt.cmpLe(rt.cmpBytes)); + } + const at: rt.u8 = rt.u8Rem(kind, 8); + rt.iterExtendBytes(out, rt.iterSlice(mapOr$0z7fb2w(rt.optionMap(rt.hashGet(map, at), rt.byteSliceFrom), rt.bytes(2).fill(221), (v2: rt.ByteSlice): Uint8Array => { + return rt.byteSliceToArray(v2); + }))); + rt.iterExtendBytes(out, rt.iterSlice(mapOr$0z7fb2w(rt.optionMap(rt.btreeGet(tree, at), rt.byteSliceFrom), rt.bytes(2).fill(221), (v3: rt.ByteSlice): Uint8Array => { + return rt.byteSliceToArray(v3); + }))); + const _60: rt.ByteSlice | undefined = rt.optionMap(rt.hashGet(map, at), rt.byteSliceFrom); + if (_60 !== undefined) { + _60.data[_60.start] = _60.data[_60.start] ^ 64; + } + const _67: rt.ByteSlice | undefined = rt.optionMap(rt.btreeGet(tree, at), rt.byteSliceFrom); + if (_67 !== undefined) { + _67.data[_67.start + 1] = _67.data[_67.start + 1] ^ 64; + } + if (rt.hashContains(map, at)) { + rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceToArray(rt.byteSliceFrom(rt.hashIndex(map, at))))); + } + if (rt.btreeContains(tree, at)) { + rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceToArray(rt.byteSliceFrom(rt.btreeIndex(tree, at))))); + } + const _90: rt.ByteSlice = rt.byteSliceFrom(rt.entryOrInsert(rt.entry(map, at), Uint8Array.of(at, at))); + _90.data[_90.start + 1] = _90.data[_90.start + 1] ^ 1; + const _97: rt.Entry = rt.entry(map, at); + const f = (v4: rt.ByteSlice): void => { + const _3: rt.u8 = rt.u8WrappingAdd(v4.data[v4.start], 1); + v4.data[v4.start] = _3; + }; + rt.entryAndModify(_97, (held) => f(rt.byteSliceFrom(held))); + const _99: rt.ByteSlice = rt.byteSliceFrom(rt.entryOrInsertWith(rt.entry(tree, at), (): Uint8Array => { + return Uint8Array.of(at, 0); + })); + _99.data[_99.start] = _99.data[_99.start] ^ 2; + const iter2: rt.Iter = rt.iterMap(rt.hashValues(map), rt.byteSliceFrom); + while (true) { + const _110: rt.ByteSlice | rt.Done = iter2.next(); + if (_110 === rt.DONE) { + break; + } + const _114: rt.u8 = rt.u8WrappingMul(_110.data[_110.start + 1], 3); + _110.data[_110.start + 1] = _114; + } + const iter3: rt.Iter<[rt.u8, rt.ByteSlice]> = rt.iterMap(rt.btreeIter(tree), (held2): [rt.u8, rt.ByteSlice] => { + return [held2[0], rt.byteSliceFrom(held2[1])]; + }); + while (true) { + const _124: [rt.u8, rt.ByteSlice] | rt.Done = iter3.next(); + if (_124 === rt.DONE) { + break; + } + const _128: rt.u8 = rt.u8WrappingAdd(_124[1].data[_124[1].start], _124[1].data[_124[1].start + 1]); + _124[1].data[_124[1].start] = _128; + } + const values: Uint8Array[] = rt.iterToArray(rt.iterCopied(rt.iterMap(rt.hashValues(map), rt.byteSliceFrom), rt.byteSliceToArray)); + rt.sliceSort(values, rt.cmpLt(rt.cmpBytes)); + const iter4: rt.Iter = rt.iterMap(rt.iterSlice(values), rt.byteSliceFrom); + while (true) { + const _147: rt.ByteSlice | rt.Done = iter4.next(); + if (_147 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _147); + } + const iter5: rt.Iter<[rt.u8, rt.ByteSlice]> = rt.iterMap(rt.btreeIter(tree), (held3): [rt.u8, rt.ByteSlice] => { + return [held3[0], rt.byteSliceFrom(held3[1])]; + }); + while (true) { + const _156: [rt.u8, rt.ByteSlice] | rt.Done = iter5.next(); + if (_156 === rt.DONE) { + break; + } + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_156[0], _156[1].data[_156[1].start], _156[1].data[_156[1].start + 1]))); + } + rt.iterExtendBytes(out, rt.iterSlice(mapOr$0z7fb2w(rt.optionMap(rt.dequeEnd(deque), rt.byteSliceFrom), rt.bytes(2).fill(221), (v5: rt.ByteSlice): Uint8Array => { + return rt.byteSliceToArray(v5); + }))); + rt.iterExtendBytes(out, rt.iterSlice(mapOr$0z7fb2w(rt.optionMap(rt.dequeEnd(deque, true), rt.byteSliceFrom), rt.bytes(2).fill(221), (v6: rt.ByteSlice): Uint8Array => { + return rt.byteSliceToArray(v6); + }))); + const _180: rt.ByteSlice | undefined = rt.optionMap(rt.dequeEnd(deque), rt.byteSliceFrom); + if (_180 !== undefined) { + _180.data[_180.start] = _180.data[_180.start] ^ 128; + } + const _186: rt.ByteSlice | undefined = rt.optionMap(rt.dequeGet(deque, rt.usizeRem(kind as rt.usize, 4)), rt.byteSliceFrom); + if (_186 !== undefined) { + _186.data[_186.start + 1] = _186.data[_186.start + 1] ^ 128; + } + if (!((deque.n === 0))) { + const _201: rt.ByteSlice = rt.byteSliceFrom(rt.dequeIndex(deque, rt.usizeSub(deque.n, 1))); + _201.data[_201.start] = _201.data[_201.start] ^ 1; + rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceToArray(rt.byteSliceFrom(rt.dequeIndex(deque, 0))))); + } + const iter6: rt.Iter = rt.iterMap(rt.dequeIter(deque), rt.byteSliceFrom); + while (true) { + const _213: rt.ByteSlice | rt.Done = iter6.next(); + if (_213 === rt.DONE) { + break; + } + const _217: rt.u8 = rt.u8WrappingAdd(_213.data[_213.start + 1], 1); + _213.data[_213.start + 1] = _217; + } + const f2 = (v7: rt.ByteSlice): boolean => { + return rt.u8Rem(v7.data[v7.start], 3) !== 0; + }; + rt.dequeRetain(deque, (held4) => f2(rt.byteSliceFrom(held4))); + const iter7: rt.Iter = rt.iterMap(rt.dequeIter(deque), rt.byteSliceFrom); + while (true) { + const _228: rt.ByteSlice | rt.Done = iter7.next(); + if (_228 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _228); + } + rt.byteVecPush(out, rt.iterCount(rt.iterFilter(rt.iterMap(rt.dequeIter(deque), rt.byteSliceFrom), (v8: rt.ByteSlice): boolean => { + const _63: rt.ByteSlice = v8; + return _63.data[_63.start + 1] > 4; + })) & 0xff); + rt.iterExtendBytes(out, rt.iterSlice(mapOr$0z7fb2w(rt.optionMap(heap.d[0], rt.byteSliceFrom), rt.bytes(2).fill(221), (v9: rt.ByteSlice): Uint8Array => { + return rt.byteSliceToArray(v9); + }))); + const _247: (a0: rt.ByteSlice) => boolean = (v10: rt.ByteSlice): boolean => { + return v10.data[v10.start + 1] !== rt.u8Rem(kind, 8); + }; + rt.heapRetain(heap, (held5) => _247(rt.byteSliceFrom(held5)), rt.cmpLe(rt.cmpBytes), rt.cmpGe(rt.cmpBytes), rt.cmpLt(rt.cmpBytes)); + const iter8: rt.Iter = rt.iterMap(rt.heapIter(heap), rt.byteSliceFrom); + while (true) { + const _253: rt.ByteSlice | rt.Done = iter8.next(); + if (_253 === rt.DONE) { + break; + } + rt.byteVecExtendFromSlice(out, _253); + } + const iter9: rt.Iter = rt.iterMap(rt.heapIter(heap), rt.byteSliceFrom); + while (true) { + const _262: rt.ByteSlice | rt.Done = iter9.next(); + if (_262 === rt.DONE) { + break; + } + rt.byteVecPush(out, _262.data[_262.start] ^ _262.data[_262.start + 1]); + } + for (const _278 of rt.heapIntoSortedVec(heap, rt.cmpLe(rt.cmpBytes), rt.cmpGe(rt.cmpBytes), rt.cmpLt(rt.cmpBytes))) { + rt.iterExtendBytes(out, rt.iterSlice(_278)); + } +} + //# sourceMappingURL=keys.ts.map diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 3313e4eca..7b1419694 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -154,6 +154,8 @@ fn cases() -> Vec<(&'static str, Case)> { ("pointers::made", |_, data, out| pointers::made(data, out)), ("arrays::as_slices", |_, data, out| arrays::as_slices(data, out)), ("ints::widenings", ints::widenings), + ("keys::lent_keys", keys::lent_keys), + ("keys::lent_values", keys::lent_values), ] } diff --git a/ts/fixtures/lang/src/keys.rs b/ts/fixtures/lang/src/keys.rs index f8f13b2a0..294ec6764 100644 --- a/ts/fixtures/lang/src/keys.rs +++ b/ts/fixtures/lang/src/keys.rs @@ -6,7 +6,7 @@ use std::borrow::Borrow; use std::cmp::{Ordering, Reverse}; -use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, btree_map, hash_map}; +use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque, btree_map, hash_map}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::sync::Arc; @@ -692,3 +692,253 @@ pub fn bytes(kind: u8, data: &[u8], out: &mut Vec) { out.push(set.len() as u8); out.push(set.contains(&data[..data.len().min(2)]) as u8); } + +/// Keys that are `[u8; N]`, lent as std lends them by every map and set: read, compared +/// with one another and the keys they were made from, looked up by, and copied out and +/// written, which leaves the collection's own as they were. A hash collection's order is +/// the runtime's, so what it yields is sorted first. +pub fn lent_keys(kind: u8, data: &[u8], out: &mut Vec) { + let pairs: Vec<[u8; 2]> = data.windows(2).map(|w| [w[0] % 4, w[1] % 4]).collect(); + let probe = [kind % 4, (kind / 4) % 4]; + let mut map: HashMap<[u8; 2], u8> = HashMap::new(); + let mut tree: BTreeMap<[u8; 2], u8> = BTreeMap::new(); + let mut set: HashSet<[u8; 2]> = HashSet::new(); + let mut ordered: BTreeSet<[u8; 2]> = BTreeSet::new(); + for (i, &pair) in pairs.iter().enumerate() { + let mut key = pair; + map.insert(key, i as u8); + tree.insert(key, i as u8); + set.insert(key); + ordered.insert(key); + // The collections hold copies, which a write to the key given them leaves alone. + key[0] ^= 0x10; + out.push(map.contains_key(&key) as u8); + } + // Each key lent, ordered among the others, compared, looked up by, and copied out and + // written. + let mut lent: Vec<&[u8; 2]> = map.keys().collect(); + lent.sort(); + for &k in &lent { + out.extend([ + k[0], + k[1], + map[k], + map.contains_key(k) as u8, + (*k == probe) as u8, + (k < &probe) as u8, + ]); + out.push(tree.get(k).copied().unwrap_or(0xdd)); + out.push(set.get(k).map_or(0xdd, |s| (s == k) as u8)); + let mut copy = *k; + copy[1] ^= 0x20; + out.push(map.contains_key(©) as u8); + } + out.push(lent.iter().all(|k| map.contains_key(*k)) as u8); + let mut entries: Vec<([u8; 2], u8)> = Vec::new(); + for (k, v) in map.iter() { + entries.push((*k, *v)); + } + entries.sort(); + for (k, v) in &entries { + out.extend_from_slice(k); + out.push(*v); + } + let mut sum = 0u8; + for (k, v) in &mut map { + *v = v.wrapping_add(k[0]); + sum = sum.wrapping_add(k[1]).wrapping_add(*v); + } + for (k, v) in map.iter_mut() { + *v ^= k[1]; + } + for (k, v) in &map { + sum = sum.wrapping_add(k[0] ^ *v); + } + out.push(sum); + // A lookup lends the key the map holds, found by an equal one. + out.push(map.get_key_value(&probe).map_or(0xdd, |(k, v)| k[0] ^ k[1] ^ *v)); + out.push(tree.get_key_value(&probe).map_or(0xdd, |(k, v)| k[0] ^ k[1] ^ *v)); + match map.entry(probe) { + hash_map::Entry::Occupied(o) => out.extend([1, o.key()[0], o.key()[1], *o.get()]), + hash_map::Entry::Vacant(v) => out.extend([0, v.key()[0], v.key()[1]]), + } + out.push(*map.entry(probe).or_insert_with_key(|k| k[0].wrapping_mul(3) ^ k[1])); + out.push(map.entry([3, 3]).key()[1]); + match tree.entry(probe) { + btree_map::Entry::Occupied(o) => out.extend([1, o.key()[0], o.key()[1], *o.get()]), + btree_map::Entry::Vacant(v) => out.extend([0, v.key()[0], v.key()[1]]), + } + out.push(*tree.entry(probe).or_insert_with_key(|k| k[1].wrapping_mul(5) ^ k[0])); + if let Some(o) = tree.first_entry() { + out.extend_from_slice(o.key()); + } + if let Some(o) = tree.last_entry() { + out.extend_from_slice(o.key()); + } + // A set lends the key it holds, found by an equal array or a borrowed `&[u8]`. + let mut members: Vec<[u8; 2]> = set.iter().copied().collect(); + members.sort(); + for m in &members { + out.extend_from_slice(m); + } + for k in &set { + out.push((ordered.get(k) == Some(k)) as u8); + } + let window = &data[..data.len().min(2)]; + out.push(set.get(window).map_or(0xdd, |k| k[0] ^ (k[1] << 2))); + out.push(set.get(&probe[..]).map_or(0xdd, |k| k[0] ^ (k[1] << 2))); + out.push(set.get(&probe).map_or(0xdd, |k| (k == &probe) as u8)); + out.push(ordered.get(&probe[..]).map_or(0xdd, |k| k[0] ^ (k[1] << 2))); + // A B-tree lends its keys in order, from either end. + for (k, v) in tree.iter().rev() { + out.extend([k[0], k[1], *v]); + } + for k in tree.keys() { + out.extend_from_slice(k); + } + for (k, v) in tree.range([1, 0]..[3, 0]) { + out.extend([k[0], k[1], *v]); + } + for (k, v) in tree.range_mut(probe..) { + *v = v.wrapping_add(k[0]); + } + for (k, v) in &tree { + out.extend([k[1], *v]); + } + for (k, v) in &mut tree { + *v ^= k[0]; + } + out.push(tree.first_key_value().map_or(0xdd, |(k, v)| k[0] ^ *v)); + out.push(tree.last_key_value().map_or(0xdd, |(k, v)| k[1] ^ *v)); + for k in ordered.iter().rev() { + out.extend_from_slice(k); + } + for k in ordered.range(..=probe) { + out.extend_from_slice(k); + } + for k in &ordered { + out.push(k[0] ^ k[1]); + } + out.extend(ordered.first().map_or([0xdd; 2], |k| *k)); + out.extend(ordered.last().map_or([0xdd; 2], |k| *k)); + // `retain` hands its closure each key. + map.retain(|k, v| k[0] <= k[1] || *v % 2 == 0); + tree.retain(|k, _| k[0] != k[1]); + set.retain(|k| k[1] != probe[1]); + ordered.retain(|k| k[0] < 2); + out.extend([map.len() as u8, tree.len() as u8, set.len() as u8, ordered.len() as u8]); + // What a copy of a lent key writes, the collection's own keys never see. + if let Some(first) = ordered.first() { + let mut copy = *first; + copy[0] = 0xff; + out.push(ordered.contains(©) as u8); + } + out.push(ordered.iter().all(|k| k[0] < 2) as u8); + // Keys of bytes in other forms, lent as they are held. + let vecs: HashMap, u8> = data.chunks(3).map(|c| (c.to_vec(), c.len() as u8)).collect(); + let mut lens: Vec<&Vec> = vecs.keys().collect(); + lens.sort(); + for k in lens { + out.push(k.len() as u8); + out.extend_from_slice(k); + } + let slices: HashSet> = data.chunks(2).map(<[u8]>::to_vec).collect(); + out.push(slices.get(window).map_or(0xdd, |k| k.len() as u8)); + let borrowed: BTreeSet<&[u8]> = data.chunks(2).collect(); + for k in &borrowed { + out.extend_from_slice(k); + } + out.push(borrowed.get(window).map_or(0xdd, |k| k.len() as u8)); + // Empty collections lend nothing. + let empty: BTreeMap<[u8; 2], u8> = BTreeMap::new(); + let none: HashSet<[u8; 2]> = HashSet::new(); + out.extend([ + empty.keys().count() as u8, + empty.first_key_value().is_none() as u8, + empty.range(probe..).count() as u8, + none.iter().count() as u8, + none.get(&probe).is_none() as u8, + ]); +} + +/// Values that are `[u8; 2]`, and the items of a deque and a heap, lent as keys are: a +/// `&` reads them and a `&mut` writes where the collection holds them. +pub fn lent_values(kind: u8, data: &[u8], out: &mut Vec) { + let mut map: HashMap = HashMap::new(); + let mut tree: BTreeMap = BTreeMap::new(); + let mut deque: VecDeque<[u8; 2]> = VecDeque::new(); + let mut heap: BinaryHeap<[u8; 2]> = BinaryHeap::new(); + for w in data.windows(2) { + let value = [w[0], w[1] % 8]; + map.insert(w[0] % 8, value); + tree.insert(w[1] % 8, value); + deque.push_back(value); + heap.push(value); + } + let at = kind % 8; + out.extend(map.get(&at).map_or([0xdd; 2], |v| *v)); + out.extend(tree.get(&at).map_or([0xdd; 2], |v| *v)); + if let Some(v) = map.get_mut(&at) { + v[0] ^= 0x40; + } + if let Some(v) = tree.get_mut(&at) { + v[1] ^= 0x40; + } + if map.contains_key(&at) { + out.extend(map[&at]); + } + if tree.contains_key(&at) { + out.extend(tree[&at]); + } + map.entry(at).or_insert([at, at])[1] ^= 1; + map.entry(at).and_modify(|v| v[0] = v[0].wrapping_add(1)); + tree.entry(at).or_insert_with(|| [at, 0])[0] ^= 2; + for v in map.values_mut() { + v[1] = v[1].wrapping_mul(3); + } + for (_, v) in tree.iter_mut() { + v[0] = v[0].wrapping_add(v[1]); + } + let mut values: Vec<[u8; 2]> = map.values().copied().collect(); + values.sort(); + for v in &values { + out.extend_from_slice(v); + } + for (k, v) in &tree { + out.extend([*k, v[0], v[1]]); + } + // A deque's items, read and written from either end and by index. + out.extend(deque.front().map_or([0xdd; 2], |v| *v)); + out.extend(deque.back().map_or([0xdd; 2], |v| *v)); + if let Some(v) = deque.front_mut() { + v[0] ^= 0x80; + } + if let Some(v) = deque.get_mut(usize::from(kind) % 4) { + v[1] ^= 0x80; + } + if !deque.is_empty() { + let last = deque.len() - 1; + deque[last][0] ^= 1; + out.extend(deque[0]); + } + for v in deque.iter_mut() { + v[1] = v[1].wrapping_add(1); + } + deque.retain(|v| v[0] % 3 != 0); + for v in &deque { + out.extend_from_slice(v); + } + out.push(deque.iter().filter(|v| v[1] > 4).count() as u8); + // A heap's greatest item, and the rest in its own order. + out.extend(heap.peek().map_or([0xdd; 2], |v| *v)); + heap.retain(|v| v[1] != kind % 8); + for v in heap.iter() { + out.extend_from_slice(v); + } + for v in &heap { + out.push(v[0] ^ v[1]); + } + for v in heap.into_sorted_vec() { + out.extend(v); + } +} diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 84d19070e..0ae816390 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 128 -- Function instances: 150 +- Refusals: 129 +- Function instances: 151 - Generic functions no instance reached, lowered over their type parameters: 10 ## By construct @@ -12,9 +12,9 @@ | 12 | a raw pointer to `_` in `_`, which the crate may put another object in place of | fixtures/reject/src/lib.rs:661:10 | | 8 | a raw pointer to `_` in a variable assigned again while it may point there | fixtures/reject/src/lib.rs:669:10 | | 5 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:372:4 | +| 4 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | fixtures/reject/src/lib.rs:242:16 | | 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:377:2 | | 3 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:41:4 | -| 3 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | fixtures/reject/src/lib.rs:242:16 | | 3 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:252:14 | | 3 | an assignment through a reference to `_` | fixtures/reject/src/lib.rs:102:2 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:110:2 | @@ -93,6 +93,7 @@ The type or module each refused type or path belongs to. | 4 | `{closure}` | fixtures/reject/src/lib.rs:364:13 | | 3 | `Iterator` | fixtures/reject/src/lib.rs:149:2 | | 3 | `Spent` | fixtures/reject/src/lib.rs:861:4 | +| 3 | `std::collections::HashMap` | fixtures/reject/src/lib.rs:53:13 | | 3 | `std::sync::Arc` | fixtures/reject/src/lib.rs:313:2 | | 3 | `std::vec::Vec` | fixtures/reject/src/lib.rs:355:16 | | 3 | `u8` | fixtures/reject/src/lib.rs:281:6 | @@ -106,7 +107,6 @@ The type or module each refused type or path belongs to. | 2 | `slice` | fixtures/reject/src/lib.rs:21:4 | | 2 | `std::any::TypeId` | fixtures/reject/src/lib.rs:591:2 | | 2 | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:263:75 | -| 2 | `std::collections::HashMap` | fixtures/reject/src/lib.rs:53:13 | | 2 | `std::iter::Iterator` | fixtures/reject/src/lib.rs:242:16 | | 2 | `std::iter::Map` | fixtures/reject/src/lib.rs:155:23 | | 2 | `std::mem::MaybeUninit` | fixtures/reject/src/lib.rs:1015:19 | @@ -149,8 +149,8 @@ The type or module each refused type or path belongs to. | 1 | `poll` | fixtures/reject/src/lib.rs:548:2 | | 1 | `rt.Poll` | fixtures/reject/src/lib.rs:548:2 | | 1 | `std::any::Any` | fixtures/reject/src/lib.rs:591:4 | -| 1 | `std::collections::HashSet` | fixtures/reject/src/lib.rs:899:6 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:182:51 | +| 1 | `std::iter::Extend` | fixtures/reject/src/lib.rs:1023:6 | | 1 | `std::iter::Rev` | fixtures/reject/src/lib.rs:176:14 | | 1 | `std::net::AddrParseError` | fixtures/reject/src/lib.rs:484:32 | | 1 | `std::net::Ipv4Addr` | fixtures/reject/src/lib.rs:484:32 | @@ -198,7 +198,8 @@ The type or module each refused type or path belongs to. | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `Vec::pop`, `Option` | fixtures/reject/src/lib.rs:41:4 | | 1 | `_` of `_`, whose `clone_from` is its own, which is not supported yet | `slice::clone_from_slice`, `Reused` | fixtures/reject/src/lib.rs:331:4 | | 1 | `_` of `_`, whose `clone_from` is its own, which is not supported yet | `slice::fill`, `Reused` | fixtures/reject/src/lib.rs:21:4 | -| 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::collections::HashSet::get` | fixtures/reject/src/lib.rs:899:6 | +| 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::collections::HashMap::into_keys` | fixtures/reject/src/lib.rs:899:6 | +| 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Extend::extend` | fixtures/reject/src/lib.rs:1023:6 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::count` | fixtures/reject/src/lib.rs:242:38 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::filter` | fixtures/reject/src/lib.rs:242:16 | | 1 | `_` through a held type's own, which is not supported yet | `::ne` | fixtures/reject/src/lib.rs:313:2 | @@ -332,6 +333,15 @@ The type or module each refused type or path belongs to. +
`_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it (4) + +- fixtures/reject/src/lib.rs:242:16: `std::iter::Iterator::filter` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it +- fixtures/reject/src/lib.rs:242:38: `std::iter::Iterator::count` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it +- fixtures/reject/src/lib.rs:899:6: `std::collections::HashMap::::into_keys` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it +- fixtures/reject/src/lib.rs:1023:6: `std::iter::Extend::extend` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it + +
+
a variant of `_`, which is not supported: either is the same JS string (4) - fixtures/reject/src/lib.rs:377:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string @@ -349,14 +359,6 @@ The type or module each refused type or path belongs to.
-
`_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it (3) - -- fixtures/reject/src/lib.rs:242:16: `std::iter::Iterator::filter` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it -- fixtures/reject/src/lib.rs:242:38: `std::iter::Iterator::count` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it -- fixtures/reject/src/lib.rs:899:6: `std::collections::HashSet::::get` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it - -
-
a copy of a closure that changes what it captured (3) - fixtures/reject/src/lib.rs:252:14: a copy of a closure that changes what it captured diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index f183bad35..704f74288 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -110,7 +110,7 @@ passing `Spent`, which drops, to `Vec::drain`, which is not supported yet at fix passing `Spent`, which drops, to `VecDeque::drain`, which is not supported yet at fixtures/reject/src/lib.rs:861:29 passing `Spent`, which drops, to `BTreeMap::range`, which is not supported yet at fixtures/reject/src/lib.rs:861:56 passing `{closure@fixtures/reject/src/lib.rs:867:11: 867:19}`, which drops, to `String::retain`, which is not supported yet at fixtures/reject/src/lib.rs:867:4 -`std::collections::HashSet::::get` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it at fixtures/reject/src/lib.rs:899:6 +`std::collections::HashMap::::into_keys` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it at fixtures/reject/src/lib.rs:899:6 `next_if_eq` of `std::string::String` items with a `str`, which is not supported yet at fixtures/reject/src/lib.rs:904:27 a raw pointer to `(u32, u32)` in `Plain`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:919:10 a raw pointer to `(u32, u32)` in `Track`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:945:19 @@ -120,6 +120,7 @@ a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object i a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:1003:58 a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:1015:19 a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:1016:19 +`std::iter::Extend::extend` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it at fixtures/reject/src/lib.rs:1023:6 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:149:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:169:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:203:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 18b45ed73..662555f1c 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -894,9 +894,9 @@ impl std::fmt::Write for Sink { } } -/// A lookup by a byte array translates, but this one hands back the set's own key. -pub fn byte_key(set: &std::collections::HashSet<[u8; 2]>, key: [u8; 2]) -> u8 { - set.get(&key).map_or(0, |k| k[0]) +/// Keys held as `&[u8; 2]` beside `[u8; 2]` values, lent as `&[u8; 2]` too. +pub fn byte_key(map: std::collections::HashMap<&[u8; 2], [u8; 2]>) -> u8 { + map.into_keys().next().map_or(0, |k| k[0]) } /// `next_if_eq` compares the item to another type by `PartialEq`, not the item's own. @@ -1017,3 +1017,8 @@ pub fn uninit_part_replaced(n: u32) -> u32 { reset_gap(&mut gap); unsafe { (*p).0 + (*q).1 } } + +/// A set of `[u8; 2]` extended by references to arrays, which it would keep as slices. +pub fn byte_extended(set: &mut std::collections::HashSet<[u8; 2]>, pairs: &[[u8; 2]]) { + set.extend(pairs); +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index d0744595d..4db9193f3 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -100,5 +100,6 @@ roots = [ "closure_replaced", "made_part_replaced", "uninit_part_replaced", + "byte_extended", ] printable = ["Meters"] diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index bb8f8f15c..5c5a9595a 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -218,6 +218,11 @@ export const None: unique symbol = /* @__PURE__ */ Symbol("None"); /** The type of `None`. */ export type None = typeof None; +/** `f` of what `o` holds, or `None`: `Option::map` of a payload that is never `undefined`. */ +export function optionMap(o: T | undefined, f: (value: T) => U): U | undefined { + return o === undefined ? undefined : f(o); +} + /** `Option::unwrap`: the value, or a panic. */ export function optionUnwrap(o: T | undefined): T; export function optionUnwrap(o: T | None, none: None): T; diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index eac9474cc..d0ccc4fbb 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -377,6 +377,15 @@ export function hashGet(m: HashMap, key: K, none?: None): V | None | return v !== undefined || none === undefined || m.p.has(key) ? v : none; } +/** `HashMap::get_key_value`: the equal key the map holds, and its value. */ +export function hashGetKeyValue(m: HashMap, key: K): [K, V] | undefined { + if (hashed(m, key)) { + const s = slot(m, key); + return s === undefined ? undefined : [s.k, s.v]; + } + return m.p.has(key) ? [key, m.p.get(key) as V] : undefined; +} + /** `HashMap::get_mut` of a value JS copies: its place, if the map holds `key`. */ export function hashGetMut(m: HashMap, key: K): Place | undefined { if (hashed(m, key)) return slot(m, key); @@ -872,13 +881,20 @@ export function btreeGetMut(m: BTreeMap, key: K): Place | undefin return i < 0 ? undefined : new AtIndex(l.v, i); } -/** `BTreeMap::get_key_value`, and `BTreeSet::get`, which return the equal key the map holds. */ +/** `BTreeMap::get_key_value`: the equal key the map holds, and its value. */ export function btreeGetKeyValue(m: BTreeMap, key: K): [K, V] | undefined { const l = leafOf(m, key); const i = search(l.k, key, m.c); return i < 0 ? undefined : [l.k[i], l.v[i]]; } +/** `BTreeSet::get`: the equal key the set holds. */ +export function btreeGetKey(m: BTreeMap, key: K): K | undefined { + const l = leafOf(m, key); + const i = search(l.k, key, m.c); + return i < 0 ? undefined : l.k[i]; +} + export function btreeContains(m: BTreeMap, key: K): boolean { return search(leafOf(m, key).k, key, m.c) >= 0; } diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index ef5ffdede..4aa365544 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -7,14 +7,15 @@ //! B-tree orders every key by its translated `Ord`. Each value a collection stores is its //! own copy, as in Rust. -use rustc_middle::ty::{self, Instance, Ty}; +use rustc_hir::def_id::DefId; +use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty}; use rustc_span::Span; use super::Translator; use super::body::Body; use super::library::{self, Call, Std}; use super::num::Int; -use crate::ts::{Expr, Stmt}; +use crate::ts::{Expr, Param, Stmt, StmtKind}; /// A call of a standard library function on these types, or its arguments back if it is /// none of theirs. @@ -196,6 +197,16 @@ pub fn call<'tcx>( } } } + // What std hands a closure of the bytes the collection holds, lent as it returns them. + if lending(std) { + let inputs = b.t.sig(call.def_id, call.args).inputs().to_vec(); + for (value, input) in args.iter_mut().zip(inputs).skip(1) { + if let Some((params, _)) = closure_sig(tcx, input) { + let f = std::mem::replace(value, Expr::undefined()); + *value = lend_closure(b, f, ¶ms, span, out); + } + } + } let e = match std { Std::HashMap | Std::HashSet => hash(b, coll, call, owner, name, args, out), Std::BTreeMap | Std::BTreeSet => btree(b, coll, call, owner, name, args), @@ -223,18 +234,174 @@ pub fn call<'tcx>( droppers.resize(lost.len(), Expr::undefined()); droppers.push(none); } - if droppers.is_empty() { - return Ok(Some(e)); - } - Ok(Some(match e { - Expr::Call(f, mut args) => { + let e = match e { + Expr::Call(f, mut args) if !droppers.is_empty() => { args.extend(droppers); Expr::Call(f, args) } e => e, + }; + Ok(Some(match lending(std) { + true => lend(b, b.t.sig(call.def_id, call.args).output(), e, span), + false => e, })) } +/// Whether the mappings of a collection of type `std` hand out the bytes it holds as +/// [`lend`] makes them: in what each returns and hands a closure. +fn lending(std: Std) -> bool { + matches!( + std, + Std::HashMap | Std::HashSet | Std::BTreeMap | Std::BTreeSet | Std::Entry | Std::VecDeque | Std::BinaryHeap + ) +} + +/// Whether `def_id`, a method of a collection [`lending`] the bytes it holds, takes and +/// returns them only where its mapping and [`lend`] make each what translated code reads: +/// a `[u8; N]` it holds is a `Uint8Array`, where a `&[u8; N]` is an `rt.ByteSlice`. `None` +/// if it is no such method. +pub fn lends_call<'tcx>(t: &Translator<'_, 'tcx>, def_id: DefId, args: GenericArgsRef<'tcx>) -> Option { + let tcx = t.tcx; + // An inherent method's type, or a trait method's `Self`. + let coll = match tcx.inherent_impl_of_assoc(def_id) { + Some(imp) => { + let generics = tcx.generics_of(imp).count(); + tcx.type_of(imp).instantiate(tcx, &args[..generics]).skip_norm_wip() + } + None => args.types().next()?.peel_refs(), + }; + let ty::Adt(_, held) = coll.kind() else { + return None; + }; + if !t.std_of(coll).is_some_and(lending) { + return None; + } + // One it holds as a reference to bytes, which a reference to one it holds is too. + if held.types().any(holds_bytes_ref) { + return Some(false); + } + let sig = t.sig(def_id, args); + let taken = sig.inputs().iter().skip(1).all(|&input| match closure_sig(tcx, input) { + Some((params, output)) => params.iter().all(|&p| lends(t, p).is_some()) && !holds_bytes_ref(output), + // A key looked up or compared by a reference to its bytes, which the runtime reads in + // either form, or what holds none, nor yields any as `extend` takes what it stores. + None => { + let yielded = super::iter::iter_of(t, input).and_then(|iter| t.iter_item(iter)); + super::bytes_ref(input) + || (lends(t, input) == Some(false) && yielded.is_none_or(|item| lends(t, item) == Some(false))) + } + }); + Some(taken && lends(t, sig.output()).is_some()) +} + +/// Whether a `ty` a collection hands out holds a `&[u8; N]` of bytes it holds: `Some(true)` +/// if it does, by itself or in a tuple, an `Option`, or an iterator's items, which [`lend`] +/// reaches, `Some(false)` if it holds none, and `None` if it holds one [`lend`] cannot reach. +fn lends<'tcx>(t: &Translator<'_, 'tcx>, ty: Ty<'tcx>) -> Option { + let parts = match ty.kind() { + ty::Ref(_, inner, _) if super::byte_array(*inner) => return Some(true), + // A reference to a `&[u8; N]` it holds, which is the `rt.ByteSlice` already. + ty::Ref(_, inner, _) if super::bytes_ref(*inner) => return Some(false), + ty::Tuple(parts) => parts.to_vec(), + _ if t.std_of(ty) == Some(Std::Option) => vec![arg(ty, 0)], + _ => match t.iter_item(ty) { + Some(item) => vec![item], + None => return (!holds_bytes_ref(ty)).then_some(false), + }, + }; + parts + .into_iter() + .try_fold(false, |any, part| Some(lends(t, part)? || any)) +} + +/// Whether `ty` holds a `&[u8; N]`, as a closure's signature does if it takes one. +pub fn holds_bytes_ref(ty: Ty<'_>) -> bool { + ty.walk().any(|arg| arg.as_type().is_some_and(super::bytes_ref)) +} + +/// The parameter types and output of a closure or function `ty`. +fn closure_sig<'tcx>(tcx: ty::TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<(Vec>, Ty<'tcx>)> { + let (sig, tupled) = match ty.kind() { + ty::Closure(_, args) => (args.as_closure().sig(), true), + ty::FnDef(..) | ty::FnPtr(..) => (ty.fn_sig(tcx), false), + _ => return None, + }; + let sig = tcx.instantiate_bound_regions_with_erased(sig); + let params = match (tupled, sig.inputs()) { + (true, [params]) => params.tuple_fields().to_vec(), + (_, params) => params.to_vec(), + }; + Some((params, sig.output())) +} + +/// `value`, a `ty` as a collection's runtime function hands it out, as translated code +/// reads it: each `&[u8; N]` in it, of bytes the collection holds as a `Uint8Array`, an +/// `rt.ByteSlice` of them, as `&[u8; N]` is elsewhere. It shares them, which nothing +/// writes through a `&`, and a `&mut` writes where the collection holds them. +fn lend<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, span: Span) -> Expr { + if lends(b.t, ty) != Some(true) { + return value; + } + if let Some(inner) = option_of(b, ty) { + let f = lend_fn(b, inner, span); + return Expr::rt("optionMap", vec![value, f]); + } + if !matches!(ty.kind(), ty::Ref(..) | ty::Tuple(_)) + && let Some(item) = b.t.iter_item(ty) + { + let f = lend_fn(b, item, span); + return Expr::rt("iterMap", vec![value, f]); + } + if let ty::Tuple(parts) = ty.kind() + && value.is_path() + { + let parts: Vec = (parts.iter().enumerate()) + .map(|(i, part)| lend(b, part, value.clone().index(Expr::lit(i.to_string())), span)) + .collect(); + return Expr::Array(parts); + } + lend_fn(b, ty, span).call(vec![value]) +} + +/// A function that [`lend`]s a `ty`. +fn lend_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Expr { + if matches!(ty.kind(), ty::Ref(..)) && lends(b.t, ty) == Some(true) { + return Expr::Rt("byteSliceFrom".into()); + } + let name = b.fresh("held"); + let body = lend(b, ty, Expr::ident(&name), span); + let param = Param { name, ty: None }; + Expr::Arrow( + vec![param], + b.t.ty(ty, span), + vec![StmtKind::Return(Some(body)).at(None)], + ) +} + +/// `f`, a closure std calls with `params`, given each as [`lend`] makes it. +fn lend_closure<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + f: Expr, + params: &[Ty<'tcx>], + span: Span, + out: &mut Vec, +) -> Expr { + if !params.iter().any(|&p| lends(b.t, p) == Some(true)) { + return f; + } + // Made once, as std takes it. + let f = match f.is_path() { + true => f, + false => b.spill(f, "f", out), + }; + let names: Vec = params.iter().map(|_| b.fresh("held")).collect(); + let values = (params.iter().zip(&names)) + .map(|(&p, name)| lend(b, p, Expr::ident(name), span)) + .collect(); + let params = names.into_iter().map(|name| Param { name, ty: None }).collect(); + Expr::Lambda(params, Box::new(f.call(values))) +} + /// What a method of a collection of type `std` may drop rather than return or keep, as the /// indices of the collection's type arguments whose runtime function takes what drops each, /// after its own arguments: the key `insert` and `entry` are given if an equal one is there @@ -282,7 +449,10 @@ fn lookup_ty<'tcx>(b: &Body<'_, '_, 'tcx>, std: Std, call: &Call<'tcx>, owner: & return None; } match (owner, name) { - ("Index", "index") => Some(library::targ(call.args, 1)), + ("Index", "index") => match library::targ(call.args, 1).kind() { + ty::Ref(_, by, _) => Some(*by), + _ => None, + }, ( _, "get" | "get_mut" | "get_key_value" | "contains_key" | "contains" | "remove" | "remove_entry" | "take" @@ -422,6 +592,7 @@ fn hash<'tcx>( (_, "get_mut") if !set && b.t.needs_place(arg(coll, 1)) => Expr::rt("hashGetMut", values), (_, "get" | "get_mut") if !set => Expr::rt("hashGet", values), (_, "get") => Expr::rt("hashKey", values), + (_, "get_key_value") => Expr::rt("hashGetKeyValue", values), (_, "take") => { values.push(Expr::lit("true")); Expr::rt("hashKey", values) @@ -702,6 +873,7 @@ fn btree<'tcx>( (_, "contains_key" | "contains") => Expr::rt("btreeContains", values), (_, "get_mut") if !set && b.t.needs_place(arg(coll, 1)) => Expr::rt("btreeGetMut", values), (_, "get" | "get_mut") if !set => Expr::rt("btreeGet", values), + (_, "get") => Expr::rt("btreeGetKey", values), (_, "get_key_value") => Expr::rt("btreeGetKeyValue", values), (_, "insert") if set => Expr::rt("btreeAdd", values), (_, "insert") => Expr::rt("btreeInsert", values), @@ -986,10 +1158,7 @@ fn heap<'tcx>( let droppers = entry_droppers(b, coll, span); Expr::rt("heapDrain", super::iter::tail(values, droppers)) } - (_, "iter") => { - let by_ref = Ty::new_imm_ref(b.t.tcx, b.t.tcx.lifetimes.re_erased, coll); - into_iter(b, by_ref, values.pop()?, span)? - } + (_, "iter") => Expr::rt("heapIter", values), (_, "reserve" | "reserve_exact" | "shrink_to_fit" | "shrink_to") => { discard(values, out); Expr::undefined() @@ -1571,10 +1740,6 @@ pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, sp Std::HashMap if places(1) => "hashIterMut", Std::BTreeMap if places(1) => "btreeIterMut", Std::VecDeque if places(0) => "dequeIterMut", - // A heap's elements in order, as its `Vec`'s, which by reference are borrowed. - Std::BinaryHeap if ty.is_ref() => { - return Some(super::iter::by_ref(Expr::rt("heapIter", vec![value]), arg(coll, 0))); - } Std::BinaryHeap => "heapIter", Std::HashMap => "hashIter", Std::HashSet => "hashKeys", @@ -1583,11 +1748,16 @@ pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, sp Std::VecDeque => "dequeIter", _ => return None, }; - let droppers = match ty.is_ref() { - true => Vec::new(), - false => entry_droppers(b, coll, span), - }; - Some(Expr::rt(name, super::iter::tail(vec![value], droppers))) + if !ty.is_ref() { + let droppers = entry_droppers(b, coll, span); + return Some(Expr::rt(name, super::iter::tail(vec![value], droppers))); + } + // What it holds, borrowed. + let iter = Expr::rt(name, vec![value]); + Some(match super::iter::iter_of(b.t, ty) { + Some(into) => lend(b, into, iter, span), + None => iter, + }) } /// `collect` into a collection of type `ty` from `iter`, an iterator of type `from`. diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 01627771d..552cbee5c 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -9,6 +9,7 @@ use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TyCtxt}; use rustc_span::{Span, Spanned}; use super::{Fx, Slot}; +use crate::lower::collections::holds_bytes_ref; use crate::lower::fmt::{self, Piece}; use crate::lower::library::{self, Flow, Std}; use crate::lower::num::Int; @@ -163,22 +164,17 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { if t.translated(def_id, gargs) || !gargs.types().any(owns_bytes) { return false; } + // A collection's mapping lends the bytes it holds as slices, and finds a key by one. + if let Some(lends) = crate::lower::collections::lends_call(t, def_id, gargs) { + return !lends; + } let self_ty = gargs.types().next(); - // A lookup only reads the key it is given, so only what it returns can hold a slice. - let mut lookup = false; // Mappings that make the slice, or take it. if let Some((owner, name)) = library::key(tcx, def_id) { let handled = match (owner.as_str(), name.as_str()) { ("Index" | "IndexMut", "index" | "index_mut") | ("Clone", "clone") | ("Argument", _) => true, // The runtime compares and hashes bytes in whichever form it is given them. ("PartialOrd" | "Ord" | "PartialEq", _) | ("Hash", "hash") => true, - ( - "HashMap" | "HashSet" | "BTreeMap" | "BTreeSet", - "get" | "get_mut" | "contains_key" | "contains" | "remove" | "take", - ) => { - lookup = true; - false - } ("slice", "iter" | "iter_mut") => true, ("IntoIterator", "into_iter") => self_ty.is_some_and(|s| elem(tcx, s).is_some()), _ => false, @@ -214,9 +210,6 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { item => std::iter::once(ty).chain(item).any(holds_bytes_ref), } }; - if lookup { - return refs(sig.output()); - } sig.inputs_and_output.iter().any(refs) || gargs.types().any(refs) } @@ -1547,11 +1540,6 @@ fn owns_bytes(ty: Ty<'_>) -> bool { } } -/// Whether `ty` holds a `&[u8; N]`, as a closure's signature does if it takes one. -fn holds_bytes_ref(ty: Ty<'_>) -> bool { - ty.walk().any(|arg| arg.as_type().is_some_and(crate::lower::bytes_ref)) -} - /// The element type of a slice, array, or `Vec`. fn elem<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option> { match ty.peel_refs().kind() {