diff --git a/quest/m3/README.md b/quest/m3/README.md index 07fe0783e..a5cca2bcd 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -19,7 +19,7 @@ generic std gap is planned here even when moq never calls it. - [f32](/quest/m3/ts-f32.md) - `f32` and `Duration`'s `f32` methods translate as `f64` does - [Trait objects](/quest/m3/ts-dyn.md) - `dyn Trait` and `Box` translate, so a consumer neither rewrites nor binds them - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library -- [An inclusive range drained by a fold](/quest/m3/ts-range-fold.md) - a folded `RangeInclusive` ends in core's state +- [An inclusive range folded through an adaptor](/quest/m3/ts-range-fold.md) - a `RangeInclusive` folded through `map`, `filter`, or `rev` ends in core's state - [bool's combinators from core's MIR](/quest/m3/ts-bool-core.md) - `bool::then` lowers from core, retiring `rt.boolThen` - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies - [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 diff --git a/quest/m3/ts-range-fold.md b/quest/m3/ts-range-fold.md index 643a16a0b..adfe3de8a 100644 --- a/quest/m3/ts-range-fold.md +++ b/quest/m3/ts-range-fold.md @@ -1,14 +1,29 @@ -# [XS] An inclusive range drained by a fold +# [S] An inclusive range folded through an adaptor ## Goal -A `RangeInclusive` drained through `by_ref` by a fold (`any`, `find`, `for_each`) ends in -core's state, `exhausted` with `start == end`, so `start()`, `end_bound`, and `Debug` -read what std's do, proven by a differential test against native Rust. +A `RangeInclusive` borrowed by `by_ref` and drained by a fold through an adaptor (`map`, +`filter`, `rev`, `chain`, `peekable`, `take`, `skip`, ...) ends in core's state, `exhausted` +with `start == end`, where std's adaptor folds the range, proven by a differential test +against native Rust. ## Plan -Found by [#124](https://github.com/kixelated/rust2/pull/124) on 2026-10-27: the runtime -folds through `next` and leaves `start` past `end`. +[#179](https://github.com/kixelated/rust2/pull/179) gave `rt.Iter` a `folded()`, which each +consumer std folds reads through: the iterator itself, but a stepper of core's `try_fold` +for a `RangeInclusive`, and its iterator's for a `&mut` borrow. A per-item method instead +cost up to 58% on `just ts bench`. [#172](https://github.com/kixelated/rust2/pull/172)'s +per-kind `fold`, which consumers take where the iterator drops, forwards through each +adaptor to the default `fold`, which reads through `folded()`. What remains is each +adaptor where a consumer reads it by `next`: + +- One whose std `try_fold` folds its iterator overrides `folded()` to read its iterator's + `folded()`, sharing its own state (`take`'s count, `peekable`'s peeked item) with it. +- `filter`, `filter_map`, and `skip_while` fold it in `next` too, through `find`, as the + `collect` shunts do through `try_for_each`. +- `rev` needs a back step, as core's `try_rfold` takes it. +- `skip` reads `nth(n - 1)` before it folds, where its `next` reads `nth(n)`. +- `zip` and `step_by` read by `next`, as std's do. +- `collect` and `extend` fold only a `TrustedLen` iterator and read others by `next`. Public API: the translator's output. diff --git a/ts/fixtures/lang/expected/wrappers.Tally.ts b/ts/fixtures/lang/expected/wrappers.Tally.ts new file mode 100644 index 000000000..26bb94fa8 --- /dev/null +++ b/ts/fixtures/lang/expected/wrappers.Tally.ts @@ -0,0 +1,21 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** A sum that drops, as a fold's accumulator may. */ +export class Tally { + _0: rt.u64; + + constructor(_0: rt.u64) { + this._0 = _0; + } + + [Symbol.dispose](): void { + drop$Drop(this); + } +} + +export function drop$Drop(_self: Tally): void { +} + +//# sourceMappingURL=wrappers.Tally.ts.map diff --git a/ts/fixtures/lang/expected/wrappers.ts b/ts/fixtures/lang/expected/wrappers.ts index a641472f0..0b1dc953a 100644 --- a/ts/fixtures/lang/expected/wrappers.ts +++ b/ts/fixtures/lang/expected/wrappers.ts @@ -1,9 +1,10 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_u8 } from "./core/option.ts"; +import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_u8, mapOr$Option_usize_u8_consumers_closure8 } from "./core/option.ts"; import { put } from "./fmt.ts"; import * as Scale from "./wrappers.Scale.ts"; +import * as Tally from "./wrappers.Tally.ts"; export function putU64(out: rt.ByteVec, v: rt.u64): void { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(v))); @@ -244,6 +245,7 @@ export function boundOf(kind: rt.u8, a: rt.u64): rt.RangeBound { } export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let _154!: Tally.Tally; let scale!: Scale.Scale; let name!: string; const _5: (a0: rt.u8) => rt.u64 = (b3: rt.u8): rt.u64 => { @@ -259,183 +261,280 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void } return rt.u64Mul(b3 as rt.u64, 7); }; - const at: (a0: rt.usize) => rt.u8 = (i3: rt.usize): rt.u8 => { - return unwrapOr$Option_u8(rt.byteSliceGet(data, i3), 0); - }; - const part = at(0); - const _9: rt.u64 = _5(part); - const part2 = at(1); - const _8: [rt.u64, rt.u64] = [_9, _5(part2)]; - const a: rt.u64 = _9; - const b: rt.u64 = _8[1]; - const _21: rt.u8 = rt.u8Rem(kind, 5); - if (_21 === 0) { - bounds$Range_u64(out, new rt.Range(a, b, rt.u64Step)); - bounds$RangeInclusive_u64(out, new rt.RangeInclusive(a, b, rt.u64Step)); - const stepped: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Rem(a, 4), rt.u64Rem(b, 4), rt.u64Step); - while (rt.iterToOption(stepped.next()) !== undefined) { - } - bounds$RangeInclusive_u64(out, stepped); - const top: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Sub({ hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Rem(a, 2)), { hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Step); - while (rt.iterToOption(top.next()) !== undefined) { - } - bounds$RangeInclusive_u64(out, top); - const down: rt.RangeInclusive = new rt.RangeInclusive(0, rt.u64Rem(b, 3), rt.u64Step); - while (rt.iterToOption(down.nextBack()) !== undefined) { - } - bounds$RangeInclusive_u64(out, down); - bounds$RangeTo_u64(out, { end: b }); - bounds$RangeToInclusive_u64(out, { end: b, inclusive: true }); - bounds$RangeFrom_u64(out, { start: a }); - bounds$RangeFull(out, undefined); - bounds$Bound_u64_Bound_u64(out, [boundOf(at(2), a), boundOf(at(3), b)] as [rt.RangeBound, rt.RangeBound]); - const held: [rt.RangeBound, rt.RangeBound] = [boundOf(at(3), b), { tag: "Unbounded" } as rt.RangeBound]; - bounds$Bound_u64_Bound_u64(out, held); - putBound(out, held[0]); - const _87: [{ end: rt.usize }, { start: rt.usize }] = [{ end: rt.usizeRem(at(2) as rt.usize, rt.usizeAdd(data.length, 1)) }, { start: rt.usizeRem(at(3) as rt.usize, rt.usizeAdd(data.length, 1)) }]; - const to: { end: rt.usize } = _87[0]; - const from: { start: rt.usize } = _87[1]; - putU64(out, rt.usizeAdd(to.end, from.start) as rt.u64); - putU64(out, b); - rt.byteVecPush(out, rt.byteSliceRange(data, 0, to.end).length & 0xff); - rt.byteVecPush(out, rt.byteSliceRange(data, from.start).length & 0xff); - } else if (_21 === 1) { - const map: rt.BTreeMap = rt.btreeNew(rt.cmp) as rt.BTreeMap; - const iter: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))); - while (true) { - const _132: [rt.usize, rt.u8] | rt.Done = iter.next(); - if (_132 === rt.DONE) { - break; + let unwind: number = 0; + try { + const at: (a0: rt.usize) => rt.u8 = (i3: rt.usize): rt.u8 => { + return unwrapOr$Option_u8(rt.byteSliceGet(data, i3), 0); + }; + const part = at(0); + const _9: rt.u64 = _5(part); + const part2 = at(1); + const _8: [rt.u64, rt.u64] = [_9, _5(part2)]; + const a: rt.u64 = _9; + const b: rt.u64 = _8[1]; + const _21: rt.u8 = rt.u8Rem(kind, 5); + if (_21 === 0) { + bounds$Range_u64(out, new rt.Range(a, b, rt.u64Step)); + bounds$RangeInclusive_u64(out, new rt.RangeInclusive(a, b, rt.u64Step)); + const stepped: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Rem(a, 4), rt.u64Rem(b, 4), rt.u64Step); + while (rt.iterToOption(stepped.next()) !== undefined) { } - rt.btreeInsert(map, _132[1] as rt.u64, _132[0] as rt.u64); - } - const _143: [rt.u64, rt.u64] = [at(0) as rt.u64, at(1) as rt.u64]; - const a2: rt.u64 = _143[0]; - const b2: rt.u64 = _143[1]; - const iter2: rt.Iter<[rt.u64, rt.u64]> = rt.btreeRange(map, { end: a2, inclusive: true }); - while (true) { - const _157: [rt.u64, rt.u64] | rt.Done = iter2.next(); - if (_157 === rt.DONE) { - break; + bounds$RangeInclusive_u64(out, stepped); + const top: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Sub({ hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Rem(a, 2)), { hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Step); + while (rt.iterToOption(top.next()) !== undefined) { } - putU64(out, _157[0]); - putU64(out, _157[1]); - } - rt.byteVecPush(out, 254); - const _167: [rt.u64, rt.u64] | undefined = rt.iterToOption(rt.btreeRange(map, { end: b2, inclusive: true }).nextBack()); - if (_167 !== undefined) { - putU64(out, _167[0]); - } - rt.byteVecPush(out, rt.iterCount(rt.btreeRange(map, { end: a2 })) & 0xff); - const top2: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Sub({ hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Rem(a2, 2)), { hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Step); - while (rt.iterToOption(top2.next()) !== undefined) { - } - const bottom: rt.RangeInclusive = new rt.RangeInclusive(0, rt.u64Rem(b2, 2), rt.u64Step); - while (rt.iterToOption(bottom.nextBack()) !== undefined) { - } - rt.byteVecPush(out, rt.iterCount(rt.btreeRange(map, top2)) & 0xff); - rt.byteVecPush(out, rt.iterCount(rt.btreeRange(map, bottom)) & 0xff); - const above: rt.BTreeMap = rt.btreeSplitOff(map, b2); - const iter3: rt.Iter<[rt.u64, rt.u64]> = rt.iterChain(rt.btreeIter(map), rt.btreeIter(above)); - while (true) { - const _220: [rt.u64, rt.u64] | rt.Done = iter3.next(); - if (_220 === rt.DONE) { - break; + bounds$RangeInclusive_u64(out, top); + const down: rt.RangeInclusive = new rt.RangeInclusive(0, rt.u64Rem(b, 3), rt.u64Step); + while (rt.iterToOption(down.nextBack()) !== undefined) { } - putU64(out, _220[0]); - putU64(out, _220[1]); - } - rt.byteVecPush(out, rt.btreeLen(map) & 0xff); - rt.byteVecPush(out, rt.btreeLen(above) & 0xff); - const _238: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); - const set: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _238, true); - const _243: rt.u8 = at(2); - const high: rt.BTreeSet = rt.btreeSplitOff(set, _243); - rt.iterExtendBytes(out, rt.iterChain(rt.btreeKeys(set), rt.btreeKeys(high))); - rt.byteVecPush(out, rt.btreeLen(high) & 0xff); - } else if (_21 === 2) { - const _258: [rt.u8, rt.u8] = [at(0), at(1)]; - const x2: rt.u8 = _258[0]; - const y: rt.u8 = _258[1]; - rt.byteVecPush(out, rt.cmpReverse(rt.cmp)(x2, y) & 0xff); - rt.byteVecPush(out, rt.cmpReverse(rt.cmp)(x2, y) === -1 ? 1 : 0); - rt.byteVecPush(out, rt.cmpReverse(rt.cmpOption(rt.cmp))(x2, undefined) === 1 ? 1 : 0); - rt.byteVecPush(out, rt.ordMax(x2, y, rt.cmpLt(rt.cmpReverse(rt.cmp)))); - const order = rt.cmpTuple([rt.cmpReverse(rt.cmp), rt.cmp])([x2, y] as [rt.u8, rt.u8], [y, x2] as [rt.u8, rt.u8]); - rt.byteVecPush(out, order !== undefined && order <= 0 ? 1 : 0); - const _302: rt.Iter<[rt.u8, rt.usize]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_2: [rt.usize, rt.u8]): [rt.u8, rt.usize] => { - return [_2[1], _2[0]] as [rt.u8, rt.usize]; - }); - const iter4: rt.Iter<[rt.u8, rt.usize]> = rt.btreeIter(rt.btreeExtend(rt.btreeNew(rt.cmpReverse(rt.cmp)) as rt.BTreeMap, _302, true)); - while (true) { - const _308: [rt.u8, rt.usize] | rt.Done = iter4.next(); - if (_308 === rt.DONE) { - break; + bounds$RangeInclusive_u64(out, down); + const _60: [rt.u64, rt.u64] = [rt.u64Rem(a, 4), rt.u64Rem(b, 4)]; + const lo: rt.u64 = _60[0]; + const hi: rt.u64 = _60[1]; + const found: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + rt.byteVecPush(out, rt.iterAny(found, (x3: rt.u64): boolean => { + return rt.u64Eq(x3, hi); + }) ? 1 : 0); + bounds$RangeInclusive_u64(out, found); + const rejected: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + rt.byteVecPush(out, rt.iterAll(rejected, (x4: rt.u64): boolean => { + return rt.u64Lt(x4, hi); + }) ? 1 : 0); + bounds$RangeInclusive_u64(out, rt.rangeInclusiveClone(rejected)); + rt.byteVecPush(out, rt.iterFind(rejected, (_27: rt.u64): boolean => { + return true; + }) !== undefined ? 1 : 0); + const placed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + rt.byteVecPush(out, mapOr$Option_usize_u8_consumers_closure8(rt.iterPosition(placed, (x5: rt.u64): boolean => { + return rt.u64Eq(x5, rt.u64Add(lo, 1)); + }), 238, (i7: rt.usize): rt.u8 => { + return i7 & 0xff; + })); + bounds$RangeInclusive_u64(out, placed); + const each: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + rt.iterForEach(rt.iterBorrow(each), (x6: rt.u64): void => { + rt.byteVecPush(out, rt.u64Low(x6) & 0xff); + }); + bounds$RangeInclusive_u64(out, each); + const summed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + putU64(out, rt.iterSum(rt.iterBorrow(summed), rt.u64Add, 0)); + bounds$RangeInclusive_u64(out, summed); + const top2: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Sub({ hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Rem(a, 3)), { hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Step); + putU64(out, rt.iterCount(rt.iterBorrow(top2)) as rt.u64); + bounds$RangeInclusive_u64(out, top2); + const last: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + putU64(out, unwrapOr$Option_NonZero_u64(rt.iterLast(rt.iterBorrow(last)), 238)); + bounds$RangeInclusive_u64(out, last); + const most: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + putU64(out, unwrapOr$Option_NonZero_u64(rt.iterMax(rt.iterBorrow(most)), 238)); + bounds$RangeInclusive_u64(out, most); + const collected: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + rt.byteVecPush(out, rt.iterToArray(rt.iterBorrow(collected)).length & 0xff); + bounds$RangeInclusive_u64(out, collected); + const gathered: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + _154 = rt.iterFold(rt.iterBorrow(gathered), new Tally.Tally(0), (t: Tally.Tally, x7: rt.u64): Tally.Tally => { + let unwind2: number = 0; + try { + unwind2 = 4; + const _66: rt.u64 = rt.u64Add(t._0, x7); + unwind2 = 0; + const _010: Tally.Tally = new Tally.Tally(_66); + rt.drop(t); + return _010; + } catch (e2) { + if (unwind2 === 0) { + throw e2; + } + rt.drop(t); + throw e2; + } + }, undefined, rt.drop); + unwind = 329; + putU64(out, _154._0); + unwind = 0; + rt.drop(_154); + bounds$RangeInclusive_u64(out, gathered); + const keyed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + const _166: rt.RangeInclusive = keyed; + rt.byteVecPush(out, rt.btreeLen(rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterBorrow(_166), true)) & 0xff); + bounds$RangeInclusive_u64(out, keyed); + const queued: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + const _176: rt.RangeInclusive = queued; + rt.byteVecPush(out, rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterBorrow(_176)).n & 0xff); + bounds$RangeInclusive_u64(out, queued); + const hashed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + const _186: rt.RangeInclusive = hashed; + rt.byteVecPush(out, rt.hashLen(rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashInt, rt.u64Eq)) as rt.HashSet, rt.iterBorrow(_186))) & 0xff); + bounds$RangeInclusive_u64(out, hashed); + const extended: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + const held: rt.u64[] = []; + rt.iterExtend(held, rt.iterBorrow(extended)); + rt.byteVecPush(out, held.length & 0xff); + bounds$RangeInclusive_u64(out, extended); + const narrow: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Low(lo) & 0xff, rt.u64Low(hi) & 0xff, rt.u8Step); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(rt.iterToByteVec(rt.iterBorrow(narrow))))); + rt.byteVecPush(out, rt.rangeInclusiveStart(narrow)); + rt.byteVecPush(out, rt.rangeInclusiveIsEmpty(narrow) ? 1 : 0); + bounds$RangeTo_u64(out, { end: b }); + bounds$RangeToInclusive_u64(out, { end: b, inclusive: true }); + bounds$RangeFrom_u64(out, { start: a }); + bounds$RangeFull(out, undefined); + bounds$Bound_u64_Bound_u64(out, [boundOf(at(2), a), boundOf(at(3), b)] as [rt.RangeBound, rt.RangeBound]); + const held2: [rt.RangeBound, rt.RangeBound] = [boundOf(at(3), b), { tag: "Unbounded" } as rt.RangeBound]; + bounds$Bound_u64_Bound_u64(out, held2); + putBound(out, held2[0]); + const _246: [{ end: rt.usize }, { start: rt.usize }] = [{ end: rt.usizeRem(at(2) as rt.usize, rt.usizeAdd(data.length, 1)) }, { start: rt.usizeRem(at(3) as rt.usize, rt.usizeAdd(data.length, 1)) }]; + const to: { end: rt.usize } = _246[0]; + const from: { start: rt.usize } = _246[1]; + putU64(out, rt.usizeAdd(to.end, from.start) as rt.u64); + putU64(out, b); + rt.byteVecPush(out, rt.byteSliceRange(data, 0, to.end).length & 0xff); + rt.byteVecPush(out, rt.byteSliceRange(data, from.start).length & 0xff); + } else if (_21 === 1) { + const map: rt.BTreeMap = rt.btreeNew(rt.cmp) as rt.BTreeMap; + const iter: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))); + while (true) { + const _291: [rt.usize, rt.u8] | rt.Done = iter.next(); + if (_291 === rt.DONE) { + break; + } + rt.btreeInsert(map, _291[1] as rt.u64, _291[0] as rt.u64); } - rt.byteVecPush(out, _308[0]); - rt.byteVecPush(out, _308[1] & 0xff); - } - const ranked: [rt.u8, rt.usize][] = rt.iterToArray(rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_23: [rt.usize, rt.u8]): [rt.u8, rt.usize] => { - return [_23[1], _23[0]] as [rt.u8, rt.usize]; - })); - rt.sliceSortByKey(ranked, (_25: [rt.u8, rt.usize]): [rt.u8, rt.usize] => { - return [rt.u8Rem(_25[0], 4), _25[1]] as [rt.u8, rt.usize]; - }, rt.cmpLt(rt.cmpTuple([rt.cmpReverse(rt.cmp), rt.cmp]))); - for (const _328 of ranked) { - const d: rt.u8 = _328[0]; - const i2: rt.usize = _328[1]; - rt.byteVecPush(out, d); - rt.byteVecPush(out, i2 & 0xff); - } - put(out, "held"); - } else if (_21 === 3) { - const n: rt.u64 | undefined = rt.nonZero(a); - if (n === undefined) { - rt.byteVecPush(out, 0); - } else { - const n2: rt.u64 = n; - putU64(out, n2); - putU64(out, n2); - const _360: rt.u64 = unwrapOr$Option_NonZero_u64(rt.nonZero(b), n2); - const _357: boolean = rt.u64Eq(n2, _360); - const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u64Display(n2, f), (f: rt.Formatter) => rt.u64Debug(n2, f), (f: rt.Formatter) => rt.fmtBool(_357, f)]; - put(out, rt.fmtFormat([args2[0], " ", args2[1], " ", args2[2]])); - rt.byteVecPush(out, rt.u64Gt(n2, { _0: 1 }._0) ? 1 : 0); - rt.byteVecPush(out, rt.cmp(n2, { _0: 1000 }._0) & 0xff); - } - rt.byteVecPush(out, rt.nonZero(rt.u64And(a, 1)) !== undefined ? 1 : 0); - } else { - const _387: rt.u64 | undefined = rt.nonZero(a); - b1: { - if (_387 !== undefined) { - if (rt.u8Rem(at(2), 2) === 0) { - scale = { _0: _387 }; - break b1; + const _302: [rt.u64, rt.u64] = [at(0) as rt.u64, at(1) as rt.u64]; + const a2: rt.u64 = _302[0]; + const b2: rt.u64 = _302[1]; + const iter2: rt.Iter<[rt.u64, rt.u64]> = rt.btreeRange(map, { end: a2, inclusive: true }); + while (true) { + const _316: [rt.u64, rt.u64] | rt.Done = iter2.next(); + if (_316 === rt.DONE) { + break; } + putU64(out, _316[0]); + putU64(out, _316[1]); } - const _398: rt.usize = rt.usizeRem(at(3) as rt.usize, 3); - if (!(_398 < 3)) { - rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _398); + rt.byteVecPush(out, 254); + const _326: [rt.u64, rt.u64] | undefined = rt.iterToOption(rt.btreeRange(map, { end: b2, inclusive: true }).nextBack()); + if (_326 !== undefined) { + putU64(out, _326[0]); } - scale = [{ _0: 1 }, { _0: 1000 }, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }][_398]; - } - if (!rt.u64Eq(1, scale._0)) { - if (!rt.u64Eq(1000, scale._0)) { - if (!rt.u64Eq({ hi: 0xffffffff, lo: 0xffffffff } as rt.u64, scale._0)) { - name = "other"; - } else { - name = "wide"; + rt.byteVecPush(out, rt.iterCount(rt.btreeRange(map, { end: a2 })) & 0xff); + const top3: rt.RangeInclusive = new rt.RangeInclusive(rt.u64Sub({ hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Rem(a2, 2)), { hi: 0xffffffff, lo: 0xffffffff } as rt.u64, rt.u64Step); + while (rt.iterToOption(top3.next()) !== undefined) { + } + const bottom: rt.RangeInclusive = new rt.RangeInclusive(0, rt.u64Rem(b2, 2), rt.u64Step); + while (rt.iterToOption(bottom.nextBack()) !== undefined) { + } + rt.byteVecPush(out, rt.iterCount(rt.btreeRange(map, top3)) & 0xff); + rt.byteVecPush(out, rt.iterCount(rt.btreeRange(map, bottom)) & 0xff); + const above: rt.BTreeMap = rt.btreeSplitOff(map, b2); + const iter3: rt.Iter<[rt.u64, rt.u64]> = rt.iterChain(rt.btreeIter(map), rt.btreeIter(above)); + while (true) { + const _379: [rt.u64, rt.u64] | rt.Done = iter3.next(); + if (_379 === rt.DONE) { + break; } + putU64(out, _379[0]); + putU64(out, _379[1]); + } + rt.byteVecPush(out, rt.btreeLen(map) & 0xff); + rt.byteVecPush(out, rt.btreeLen(above) & 0xff); + const _397: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); + const set: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _397, true); + const _402: rt.u8 = at(2); + const high: rt.BTreeSet = rt.btreeSplitOff(set, _402); + rt.iterExtendBytes(out, rt.iterChain(rt.btreeKeys(set), rt.btreeKeys(high))); + rt.byteVecPush(out, rt.btreeLen(high) & 0xff); + } else if (_21 === 2) { + const _417: [rt.u8, rt.u8] = [at(0), at(1)]; + const x2: rt.u8 = _417[0]; + const y: rt.u8 = _417[1]; + rt.byteVecPush(out, rt.cmpReverse(rt.cmp)(x2, y) & 0xff); + rt.byteVecPush(out, rt.cmpReverse(rt.cmp)(x2, y) === -1 ? 1 : 0); + rt.byteVecPush(out, rt.cmpReverse(rt.cmpOption(rt.cmp))(x2, undefined) === 1 ? 1 : 0); + rt.byteVecPush(out, rt.ordMax(x2, y, rt.cmpLt(rt.cmpReverse(rt.cmp)))); + const order = rt.cmpTuple([rt.cmpReverse(rt.cmp), rt.cmp])([x2, y] as [rt.u8, rt.u8], [y, x2] as [rt.u8, rt.u8]); + rt.byteVecPush(out, order !== undefined && order <= 0 ? 1 : 0); + const _461: rt.Iter<[rt.u8, rt.usize]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_2: [rt.usize, rt.u8]): [rt.u8, rt.usize] => { + return [_2[1], _2[0]] as [rt.u8, rt.usize]; + }); + const iter4: rt.Iter<[rt.u8, rt.usize]> = rt.btreeIter(rt.btreeExtend(rt.btreeNew(rt.cmpReverse(rt.cmp)) as rt.BTreeMap, _461, true)); + while (true) { + const _467: [rt.u8, rt.usize] | rt.Done = iter4.next(); + if (_467 === rt.DONE) { + break; + } + rt.byteVecPush(out, _467[0]); + rt.byteVecPush(out, _467[1] & 0xff); + } + const ranked: [rt.u8, rt.usize][] = rt.iterToArray(rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_23: [rt.usize, rt.u8]): [rt.u8, rt.usize] => { + return [_23[1], _23[0]] as [rt.u8, rt.usize]; + })); + rt.sliceSortByKey(ranked, (_25: [rt.u8, rt.usize]): [rt.u8, rt.usize] => { + return [rt.u8Rem(_25[0], 4), _25[1]] as [rt.u8, rt.usize]; + }, rt.cmpLt(rt.cmpTuple([rt.cmpReverse(rt.cmp), rt.cmp]))); + for (const _487 of ranked) { + const d: rt.u8 = _487[0]; + const i2: rt.usize = _487[1]; + rt.byteVecPush(out, d); + rt.byteVecPush(out, i2 & 0xff); + } + put(out, "held"); + } else if (_21 === 3) { + const n: rt.u64 | undefined = rt.nonZero(a); + if (n === undefined) { + rt.byteVecPush(out, 0); } else { - name = "milli"; + const n2: rt.u64 = n; + putU64(out, n2); + putU64(out, n2); + const _519: rt.u64 = unwrapOr$Option_NonZero_u64(rt.nonZero(b), n2); + const _516: boolean = rt.u64Eq(n2, _519); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u64Display(n2, f), (f: rt.Formatter) => rt.u64Debug(n2, f), (f: rt.Formatter) => rt.fmtBool(_516, f)]; + put(out, rt.fmtFormat([args2[0], " ", args2[1], " ", args2[2]])); + rt.byteVecPush(out, rt.u64Gt(n2, { _0: 1 }._0) ? 1 : 0); + rt.byteVecPush(out, rt.cmp(n2, { _0: 1000 }._0) & 0xff); } + rt.byteVecPush(out, rt.nonZero(rt.u64And(a, 1)) !== undefined ? 1 : 0); } else { - name = "one"; + const _546: rt.u64 | undefined = rt.nonZero(a); + b1: { + if (_546 !== undefined) { + if (rt.u8Rem(at(2), 2) === 0) { + scale = { _0: _546 }; + break b1; + } + } + const _557: rt.usize = rt.usizeRem(at(3) as rt.usize, 3); + if (!(_557 < 3)) { + rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _557); + } + scale = [{ _0: 1 }, { _0: 1000 }, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }][_557]; + } + if (!rt.u64Eq(1, scale._0)) { + if (!rt.u64Eq(1000, scale._0)) { + if (!rt.u64Eq({ hi: 0xffffffff, lo: 0xffffffff } as rt.u64, scale._0)) { + name = "other"; + } else { + name = "wide"; + } + } else { + name = "milli"; + } + } else { + name = "one"; + } + unwind = 0; + put(out, name); + rt.byteVecPush(out, Scale.eq$PartialEq(scale, { _0: 1000 }) ? 1 : 0); + rt.byteVecPush(out, Scale.partialCmp$PartialOrd(scale, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }) === -1 ? 1 : 0); + putU64(out, scale._0); + } + } catch (e) { + if (unwind === 0) { + throw e; } - put(out, name); - rt.byteVecPush(out, Scale.eq$PartialEq(scale, { _0: 1000 }) ? 1 : 0); - rt.byteVecPush(out, Scale.partialCmp$PartialOrd(scale, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }) === -1 ? 1 : 0); - putU64(out, scale._0); + rt.drop(_154); + throw e; } } diff --git a/ts/fixtures/lang/src/wrappers.rs b/ts/fixtures/lang/src/wrappers.rs index 4f0d39211..8cb14113a 100644 --- a/ts/fixtures/lang/src/wrappers.rs +++ b/ts/fixtures/lang/src/wrappers.rs @@ -57,6 +57,13 @@ fn bounds(out: &mut Vec, r: impl RangeBounds) { out.push(start.is_some() as u8 + 2 * end.is_some() as u8); } +/// A sum that drops, as a fold's accumulator may. +struct Tally(u64); + +impl Drop for Tally { + fn drop(&mut self) {} +} + /// A bound of `a` picked by `kind`, held by value. fn bound_of(kind: u8, a: u64) -> Bound { match kind % 3 { @@ -91,6 +98,60 @@ pub fn wrappers(kind: u8, data: &[u8], out: &mut Vec) { let mut down = 0..=(b % 3); while down.next_back().is_some() {} bounds(out, down); + // Drained by a fold, as std's `try_fold` takes it, `start` stops at `end`, exhausted. + let (lo, hi) = (a % 4, b % 4); + let mut found = lo..=hi; + out.push(found.any(|x| x == hi) as u8); + bounds(out, found); + let mut rejected = lo..=hi; + out.push(rejected.all(|x| x < hi) as u8); + bounds(out, rejected.clone()); + out.push(rejected.find(|_| true).is_some() as u8); + let mut placed = lo..=hi; + out.push(placed.position(|x| x == lo + 1).map_or(0xee, |i| i as u8)); + bounds(out, placed); + let mut each = lo..=hi; + each.by_ref().for_each(|x| out.push(x as u8)); + bounds(out, each); + let mut summed = lo..=hi; + put_u64(out, summed.by_ref().sum()); + bounds(out, summed); + let mut top = (u64::MAX - a % 3)..=u64::MAX; + put_u64(out, top.by_ref().count() as u64); + bounds(out, top); + let mut last = lo..=hi; + put_u64(out, last.by_ref().last().unwrap_or(0xee)); + bounds(out, last); + // `max` reads its first by `next`, so one alone passes `end`, and folds the rest. + let mut most = lo..=hi; + put_u64(out, most.by_ref().max().unwrap_or(0xee)); + bounds(out, most); + let mut collected = lo..=hi; + out.push(collected.by_ref().collect::>().len() as u8); + bounds(out, collected); + // Of an accumulator that drops, which takes the range's own `fold`. + let mut gathered = lo..=hi; + put_u64(out, gathered.by_ref().fold(Tally(0), |t, x| Tally(t.0 + x)).0); + bounds(out, gathered); + let mut keyed = lo..=hi; + out.push(keyed.by_ref().collect::>().len() as u8); + bounds(out, keyed); + let mut queued = lo..=hi; + out.push(queued.by_ref().collect::>().len() as u8); + bounds(out, queued); + let mut hashed = lo..=hi; + out.push(hashed.by_ref().collect::>().len() as u8); + bounds(out, hashed); + let mut extended = lo..=hi; + let mut held = Vec::new(); + held.extend(extended.by_ref()); + out.push(held.len() as u8); + bounds(out, extended); + // Bytes, which collect into their own `Vec`. + let mut narrow = (lo as u8)..=(hi as u8); + out.extend(narrow.by_ref().collect::>()); + out.push(*narrow.start()); + out.push(narrow.is_empty() as u8); bounds(out, ..b); bounds(out, ..=b); bounds(out, a..); diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index af1524e1c..c8ee6dafb 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -834,13 +834,15 @@ function taken(m: HashMap): HashMap { * `c` by `drop`. */ function extending(c: C, items: Iter, add: (item: T) => void, take?: Take, fresh = false, drop?: (c: C) => void): C { + // By `next` where std's reads by it, and else as its `try_fold` steps. + const read = take === "next" ? items : items.folded(); if (take === undefined && !fresh) { - for (let item = items.next(); item !== DONE; item = items.next()) add(item); + for (let item = read.next(); item !== DONE; item = read.next()) add(item); return c; } const all = () => { if (take === "fold") items.fold(undefined, (_, item) => add(item)); - else for (let item = items.next(); item !== DONE; item = items.next()) add(item); + else for (let item = read.next(); item !== DONE; item = read.next()) add(item); }; try { try { diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index c70c18db5..3134dea20 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -14,9 +14,10 @@ // function is given what drops it, after its own arguments, and so is a consumer that // takes the iterator by value, which drops it as std's does, a panic included. Each kind's // `nth`, `nth_back`, and `advance_by` are its own, as std's are, reading and dropping what -// they skip in its order. Where std's `count` and `last` drop items in an order each kind -// of iterator has its own, the `owned` functions follow it, dispatching on the kind, so a -// bundle that never drops an item carries none of it. +// they skip in its order. A consumer std folds reads what `folded` returns, which steps +// an inclusive range as its `try_fold` does. Where std's `count` and `last` drop items in +// an order each kind of iterator has its own, the `owned` functions follow it, dispatching +// on the kind, so a bundle that never drops an item carries none of it. import { ByteVec, byteVecPush, Err, type None, Ok, type Place, panic, type RangeBound, type Result } from "../rt.ts"; import { AtIndex, BTreeIter, HashIter } from "./collections.ts"; @@ -81,6 +82,11 @@ export abstract class Iter implements Iterable { return this.advanceBackBy(n, drop) === 0 ? this.nextBack() : DONE; } + /** What std's `try_fold` reads it by: itself, but for an inclusive range, which steps its own way. */ + folded(): Iter { + return this; + } + /** * `fold`, as std's of its kind, which takes the iterator by value and drops what it * holds as std's does, a panic included. `release` drops what `f` owns, as std drops @@ -103,15 +109,17 @@ export abstract class Iter implements Iterable { } /** - * std's default `fold`, or `rfold` if `back`: each item by `next`, the accumulator dropped - * by `dropAcc` if reading one panics, and then `release` and `it`. + * std's default `fold`, or `rfold` if `back`: each item as `folded` steps it, or by + * `next_back`, the accumulator dropped by `dropAcc` if reading one panics, and then + * `release` and `it`. */ function foldBy(it: Iter, back: boolean, acc: A, f: (acc: A, item: T) => A, dropAcc?: Drop, release?: () => void): A { + const folded = it.folded(); try { for (;;) { let item: T | Done; try { - item = back ? it.nextBack() : it.next(); + item = back ? it.nextBack() : folded.next(); } catch (e) { dropAcc?.(acc); throw e; @@ -297,7 +305,8 @@ export function iterBorrow(it: Iter): Iter { /** `Extend::extend` of a `Vec`, which takes `items` as `take` says. */ export function iterExtend(v: T[], items: Iter, take?: Take): void { if (take === undefined) { - for (let item = items.next(); item !== DONE; item = items.next()) v.push(item); + const folded = items.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) v.push(item); return; } each(items, (item) => v.push(item), take); @@ -306,7 +315,8 @@ export function iterExtend(v: T[], items: Iter, take?: Take): void { /** `Extend::extend` of a `Vec`. */ export function iterExtendBytes(v: ByteVec, items: Iter, take?: Take): void { if (take === undefined) { - for (let item = items.next(); item !== DONE; item = items.next()) byteVecPush(v, item); + const folded = items.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) byteVecPush(v, item); return; } each(items, (item) => byteVecPush(v, item), take); @@ -455,15 +465,17 @@ function each(it: Iter, f: (item: T) => void, take: Take | undefined): voi /** `Iterator::count` of an iterator that drops nothing. */ export function iterCount(it: Iter): number { + const folded = it.folded(); let n = 0; - while (it.next() !== DONE) n++; + while (folded.next() !== DONE) n++; return n; } /** `Iterator::last` of an iterator that drops nothing. */ export function iterLast(it: Iter): T | undefined { + const folded = it.folded(); let last: T | Done = DONE; - for (let item = it.next(); item !== DONE; item = it.next()) last = item; + for (let item = folded.next(); item !== DONE; item = folded.next()) last = item; return iterToOption(last); } @@ -482,23 +494,26 @@ export function iterNthBack(it: Iter, n: number, drop?: Drop, none?: No /** `Iterator::any`. */ export function iterAny(it: Iter, f: (item: T) => boolean): boolean { - for (let item = it.next(); item !== DONE; item = it.next()) if (f(item)) return true; + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) if (f(item)) return true; return false; } /** `Iterator::all`. */ export function iterAll(it: Iter, f: (item: T) => boolean): boolean { - for (let item = it.next(); item !== DONE; item = it.next()) if (!f(item)) return false; + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) if (!f(item)) return false; return true; } /** `find`, as `next` returns it: what `f` rejects, or panics on, drops by `drop`. */ function findItem(it: Iter, f: (item: T) => boolean, drop?: Drop): T | Done { + const folded = it.folded(); if (drop === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) if (f(item)) return item; + for (let item = folded.next(); item !== DONE; item = folded.next()) if (f(item)) return item; return DONE; } - for (let item = it.next(); item !== DONE; item = it.next()) { + for (let item = folded.next(); item !== DONE; item = folded.next()) { if (test(f, item, drop)) return item; drop(item); } @@ -531,7 +546,8 @@ export function iterFind(it: Iter, f: (item: T) => boolean, drop?: Drop /** `Iterator::find_map`. */ export function iterFindMap(it: Iter, f: (item: T) => U | undefined): U | undefined { - for (let item = it.next(); item !== DONE; item = it.next()) { + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) { const found = f(item); if (found !== undefined) return found; } @@ -540,8 +556,9 @@ export function iterFindMap(it: Iter, f: (item: T) => U | undefined): U /** `Iterator::position`. */ export function iterPosition(it: Iter, f: (item: T) => boolean): number | undefined { + const folded = it.folded(); let i = 0; - for (let item = it.next(); item !== DONE; item = it.next()) { + for (let item = folded.next(); item !== DONE; item = folded.next()) { if (f(item)) return i; i++; } @@ -554,8 +571,9 @@ export function iterPosition(it: Iter, f: (item: T) => boolean): number | */ export function iterFold(it: Iter, init: A, f: (acc: A, item: T) => A, drops = false, dropAcc?: Drop): A { if (!drops && dropAcc === undefined) { + const folded = it.folded(); let acc = init; - for (let item = it.next(); item !== DONE; item = it.next()) acc = f(acc, item); + for (let item = folded.next(); item !== DONE; item = folded.next()) acc = f(acc, item); return acc; } return it.fold(init, f, dropAcc); @@ -564,7 +582,8 @@ export function iterFold(it: Iter, init: A, f: (acc: A, item: T) => A, /** `Iterator::for_each`. */ export function iterForEach(it: Iter, f: (item: T) => void, drops = false): void { if (!drops) { - for (let item = it.next(); item !== DONE; item = it.next()) f(item); + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) f(item); return; } it.fold(undefined, (_, item) => f(item)); @@ -611,7 +630,8 @@ function best(it: Iter, f: (item: T) = let best = it.next(); if (best === DONE) return undefined; let key = f(best); - for (let item = it.next(); item !== DONE; item = it.next()) { + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) { const k = f(item); if (least ? lt(k, key) : !lt(k, key)) [best, key] = [item, k]; } @@ -657,7 +677,8 @@ function best(it: Iter, f: (item: T) = export function iterToArray(it: Iter, take?: Take | "place", drop?: Drop): T[] { const out: T[] = []; if (take === undefined && drop === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) out.push(item); + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) out.push(item); return out; } // Not `dropAll`'s default, which would drop what a collected reference points to. @@ -691,7 +712,8 @@ export function iterToByteVec(it: Iter, take?: Take): ByteVec { const out = new ByteVec(); // Its own loop: through `iterExtendBytes`, JavaScriptCore took 1.9x as long. if (take === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) byteVecPush(out, item); + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) byteVecPush(out, item); return out; } each(it, (item) => byteVecPush(out, item), take); @@ -1177,6 +1199,10 @@ class Borrowed extends Iter { return this.inner.next(); } + override folded(): Iter { + return this.inner.folded(); + } + override nextBack(): T | Done { return this.inner.nextBack(); } @@ -2579,6 +2605,10 @@ export class RangeInclusive extends Iter { return lo; } + override folded(): Iter { + return new RangeInclusiveFold(this); + } + override nextBack(): T | Done { if (rangeInclusiveIsEmpty(this)) return DONE; const hi = this.hi; @@ -2611,6 +2641,25 @@ export class RangeInclusive extends Iter { } } +/** An inclusive range as std's `try_fold` steps it: `start` stops at `end`, which exhausts it, so it never wraps. */ +class RangeInclusiveFold extends Iter { + readonly range: RangeInclusive; + + constructor(range: RangeInclusive) { + super(); + this.range = range; + } + + next(): T | Done { + const r = this.range; + if (rangeInclusiveIsEmpty(r)) return DONE; + const lo = r.lo; + if (lt(lo, r.hi)) r.lo = step(lo, 1, r.int)!; + else r.exhausted = true; + return lo; + } +} + /** `RangeInclusive::start`. */ export function rangeInclusiveStart(r: RangeInclusive): T { return r.lo;