From 64c0352eb250e67793de5478af0f580e8f985437 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:05:14 -0700 Subject: [PATCH 1/6] chore(quest): claim quest/m3/ts-range-fold.md Quest-Claim: 50bd7f67f489b632 From 52c85b376198cb9870982b510947db0741b014a0 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:14:16 -0700 Subject: [PATCH 2/6] fix(ts): drain an inclusive range by a fold as std's try_fold does Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/wrappers.ts | 151 +++++++++++++++++--------- ts/fixtures/lang/src/wrappers.rs | 37 +++++++ ts/runtime/rt/collections.ts | 10 +- ts/runtime/rt/iter.ts | 73 ++++++++----- 4 files changed, 189 insertions(+), 82 deletions(-) diff --git a/ts/fixtures/lang/expected/wrappers.ts b/ts/fixtures/lang/expected/wrappers.ts index a641472f0..f9d4c3107 100644 --- a/ts/fixtures/lang/expected/wrappers.ts +++ b/ts/fixtures/lang/expected/wrappers.ts @@ -1,7 +1,7 @@ // 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"; @@ -284,6 +284,57 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void while (rt.iterToOption(down.nextBack()) !== undefined) { } 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 keyed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + const _157: rt.RangeInclusive = keyed; + rt.byteVecPush(out, rt.btreeLen(rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterBorrow(_157), true)) & 0xff); + bounds$RangeInclusive_u64(out, keyed); + const queued: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + const _167: rt.RangeInclusive = queued; + rt.byteVecPush(out, rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterBorrow(_167)).n & 0xff); + bounds$RangeInclusive_u64(out, queued); bounds$RangeTo_u64(out, { end: b }); bounds$RangeToInclusive_u64(out, { end: b, inclusive: true }); bounds$RangeFrom_u64(out, { start: a }); @@ -292,9 +343,9 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void 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]; + const _200: [{ 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 } = _200[0]; + const from: { start: rt.usize } = _200[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); @@ -303,77 +354,77 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void 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) { + const _245: [rt.usize, rt.u8] | rt.Done = iter.next(); + if (_245 === rt.DONE) { break; } - rt.btreeInsert(map, _132[1] as rt.u64, _132[0] as rt.u64); + rt.btreeInsert(map, _245[1] as rt.u64, _245[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 _256: [rt.u64, rt.u64] = [at(0) as rt.u64, at(1) as rt.u64]; + const a2: rt.u64 = _256[0]; + const b2: rt.u64 = _256[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) { + const _270: [rt.u64, rt.u64] | rt.Done = iter2.next(); + if (_270 === rt.DONE) { break; } - putU64(out, _157[0]); - putU64(out, _157[1]); + putU64(out, _270[0]); + putU64(out, _270[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]); + const _280: [rt.u64, rt.u64] | undefined = rt.iterToOption(rt.btreeRange(map, { end: b2, inclusive: true }).nextBack()); + if (_280 !== undefined) { + putU64(out, _280[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 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, top2)) & 0xff); + 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 _220: [rt.u64, rt.u64] | rt.Done = iter3.next(); - if (_220 === rt.DONE) { + const _333: [rt.u64, rt.u64] | rt.Done = iter3.next(); + if (_333 === rt.DONE) { break; } - putU64(out, _220[0]); - putU64(out, _220[1]); + putU64(out, _333[0]); + putU64(out, _333[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); + const _351: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); + const set: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _351, true); + const _356: rt.u8 = at(2); + const high: rt.BTreeSet = rt.btreeSplitOff(set, _356); 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]; + const _371: [rt.u8, rt.u8] = [at(0), at(1)]; + const x2: rt.u8 = _371[0]; + const y: rt.u8 = _371[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] => { + const _415: 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)); + const iter4: rt.Iter<[rt.u8, rt.usize]> = rt.btreeIter(rt.btreeExtend(rt.btreeNew(rt.cmpReverse(rt.cmp)) as rt.BTreeMap, _415, true)); while (true) { - const _308: [rt.u8, rt.usize] | rt.Done = iter4.next(); - if (_308 === rt.DONE) { + const _421: [rt.u8, rt.usize] | rt.Done = iter4.next(); + if (_421 === rt.DONE) { break; } - rt.byteVecPush(out, _308[0]); - rt.byteVecPush(out, _308[1] & 0xff); + rt.byteVecPush(out, _421[0]); + rt.byteVecPush(out, _421[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]; @@ -381,9 +432,9 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void 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]; + for (const _441 of ranked) { + const d: rt.u8 = _441[0]; + const i2: rt.usize = _441[1]; rt.byteVecPush(out, d); rt.byteVecPush(out, i2 & 0xff); } @@ -396,28 +447,28 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void 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)]; + const _473: rt.u64 = unwrapOr$Option_NonZero_u64(rt.nonZero(b), n2); + const _470: boolean = rt.u64Eq(n2, _473); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u64Display(n2, f), (f: rt.Formatter) => rt.u64Debug(n2, f), (f: rt.Formatter) => rt.fmtBool(_470, 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); + const _500: rt.u64 | undefined = rt.nonZero(a); b1: { - if (_387 !== undefined) { + if (_500 !== undefined) { if (rt.u8Rem(at(2), 2) === 0) { - scale = { _0: _387 }; + scale = { _0: _500 }; break b1; } } - 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); + const _511: rt.usize = rt.usizeRem(at(3) as rt.usize, 3); + if (!(_511 < 3)) { + rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _511); } - scale = [{ _0: 1 }, { _0: 1000 }, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }][_398]; + scale = [{ _0: 1 }, { _0: 1000 }, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }][_511]; } if (!rt.u64Eq(1, scale._0)) { if (!rt.u64Eq(1000, scale._0)) { diff --git a/ts/fixtures/lang/src/wrappers.rs b/ts/fixtures/lang/src/wrappers.rs index 4f0d39211..2e8bc9024 100644 --- a/ts/fixtures/lang/src/wrappers.rs +++ b/ts/fixtures/lang/src/wrappers.rs @@ -91,6 +91,43 @@ 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); + 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); bounds(out, ..b); bounds(out, ..=b); bounds(out, a..); diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index be4efa96f..78360993b 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -867,7 +867,7 @@ export function hashExtend( fresh = false, ): HashMap { const f = () => { - for (let item = items.next(); item !== DONE; item = items.next()) { + for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) { const old = hashInsert(m, item[0], item[1], k, v); if (old !== undefined) v?.(old); } @@ -878,7 +878,7 @@ export function hashExtend( /** `extend` of a set with keys, and `collect` into one, as `hashExtend`: a key already there drops. */ export function hashExtendKeys(m: HashSet, keys: Iter, k?: (key: K) => void, dropIt?: (it: Iter) => void, fresh = false): HashSet { const f = () => { - for (let key = keys.next(); key !== DONE; key = keys.next()) hashAdd(m, key, k); + for (let key = keys.foldNext(); key !== DONE; key = keys.foldNext()) hashAdd(m, key, k); }; return extending(m, keys, f, dropIt, fresh, (m) => hashDrop(m, k)); } @@ -1599,7 +1599,7 @@ export function btreeExtend( dropIt?: (it: Iter<[K, V]>) => void, ): BTreeMap { const f = () => { - for (let item = items.next(); item !== DONE; item = items.next()) { + for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) { if (!last) { const old = btreeInsert(m, item[0], item[1], k, v); if (old !== undefined) v?.(old); @@ -1620,7 +1620,7 @@ export function btreeExtend( /** `extend` of a set with keys, and with `last`, `collect` and `from`, as `btreeExtend`. */ export function btreeExtendKeys(m: BTreeSet, keys: Iter, last = false, k?: (key: K) => void, dropIt?: (it: Iter) => void): BTreeSet { const f = () => { - for (let key = keys.next(); key !== DONE; key = keys.next()) { + for (let key = keys.foldNext(); key !== DONE; key = keys.foldNext()) { if (!last) { btreeAdd(m, key, k); continue; @@ -2146,7 +2146,7 @@ export function dequeDrain(q: VecDeque, r: Bounds, drop?: (item: T /** `extend`, and `collect` into a deque through `dequeNew`, which drops `items` by `dropIt` once done. */ export function dequeExtend(q: VecDeque, items: Iter, dropIt?: (it: Iter) => void): VecDeque { const f = () => { - for (let item = items.next(); item !== DONE; item = items.next()) dequePushBack(q, item); + for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) dequePushBack(q, item); }; return extending(q, items, f, dropIt); } diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index e0df4c084..227bdb351 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 by `foldNext`, which an inclusive +// range takes 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; } + /** The next item as std's `try_fold` takes it, which only an inclusive range takes other than by `next`. */ + foldNext(): T | Done { + return this.next(); + } + /** Its items, for a JS `for ... of`. */ *[Symbol.iterator](): Iterator { for (let item = this.next(); item !== DONE; item = this.next()) yield item; @@ -202,11 +208,11 @@ export function iterBorrow(it: Iter): Iter { /** `Extend::extend` of a `Vec`, which drops `items` by `dropIt` once done or a panic. */ export function iterExtend(v: T[], items: Iter, dropIt?: Drop>): void { if (dropIt === undefined) { - for (let item = items.next(); item !== DONE; item = items.next()) v.push(item); + for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) v.push(item); return; } try { - for (let item = items.next(); item !== DONE; item = items.next()) v.push(item); + for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) v.push(item); } finally { dropIt(items); } @@ -215,7 +221,7 @@ export function iterExtend(v: T[], items: Iter, dropIt?: Drop>): v /** `Extend::extend` of a `Vec`. */ export function iterExtendBytes(v: ByteVec, items: Iter, dropIt?: Drop>): void { try { - for (let item = items.next(); item !== DONE; item = items.next()) byteVecPush(v, item); + for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) byteVecPush(v, item); } finally { dropIt?.(items); } @@ -347,14 +353,14 @@ export function iterFlatten(it: Iter, into?: (value: T) => Iter, fus /** `Iterator::count` of an iterator that drops nothing. */ export function iterCount(it: Iter): number { let n = 0; - while (it.next() !== DONE) n++; + while (it.foldNext() !== DONE) n++; return n; } /** `Iterator::last` of an iterator that drops nothing. */ export function iterLast(it: Iter): T | undefined { let last: T | Done = DONE; - for (let item = it.next(); item !== DONE; item = it.next()) last = item; + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) last = item; return iterToOption(last); } @@ -373,23 +379,23 @@ 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; + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) 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; + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) 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 { if (drop === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) if (f(item)) return item; + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) if (f(item)) return item; return DONE; } - for (let item = it.next(); item !== DONE; item = it.next()) { + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) { if (test(f, item, drop)) return item; drop(item); } @@ -422,7 +428,7 @@ 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()) { + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) { const found = f(item); if (found !== undefined) return found; } @@ -432,7 +438,7 @@ export function iterFindMap(it: Iter, f: (item: T) => U | undefined): U /** `Iterator::position`. */ export function iterPosition(it: Iter, f: (item: T) => boolean): number | undefined { let i = 0; - for (let item = it.next(); item !== DONE; item = it.next()) { + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) { if (f(item)) return i; i++; } @@ -446,14 +452,14 @@ export function iterPosition(it: Iter, f: (item: T) => boolean): number | export function iterFold(it: Iter, init: A, f: (acc: A, item: T) => A, dropIt?: Drop>, dropAcc?: Drop): A { let acc = init; if (dropIt === undefined && dropAcc === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) acc = f(acc, item); + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) acc = f(acc, item); return acc; } try { for (;;) { let item: T | Done; try { - item = it.next(); + item = it.foldNext(); } catch (e) { dropAcc?.(acc); throw e; @@ -473,11 +479,11 @@ 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, dropIt?: Drop>): void { if (dropIt === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) f(item); + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) f(item); return; } try { - for (let item = it.next(); item !== DONE; item = it.next()) f(item); + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) f(item); } finally { dropIt(it); } @@ -524,7 +530,7 @@ 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()) { + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) { const k = f(item); if (least ? lt(k, key) : !lt(k, key)) [best, key] = [item, k]; } @@ -533,7 +539,7 @@ function best(it: Iter, f: (item: T) = let best: T | Done = DONE; let key: K | undefined; try { - for (let item = it.next(); item !== DONE; item = it.next()) { + for (let item = it.next(); item !== DONE; item = it.foldNext()) { let k: K; try { k = f(item); @@ -576,13 +582,13 @@ function best(it: Iter, f: (item: T) = export function iterToArray(it: Iter, inPlace = false, dropIt?: Drop>, drop?: Drop): T[] { const out: T[] = []; if (dropIt === undefined && drop === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) out.push(item); + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) out.push(item); return out; } // Not `dropAll`'s default, which would drop what a collected reference points to. const collected = () => drop !== undefined && dropAll(out, drop); try { - for (let item = it.next(); item !== DONE; item = it.next()) out.push(item); + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) out.push(item); } catch (e) { if (inPlace) both(collected, () => dropIt?.(it)); else both(() => dropIt?.(it), collected); @@ -602,11 +608,11 @@ export function iterToByteVec(it: Iter, dropIt?: Drop>): By const out = new ByteVec(); // Its own loop: through `iterExtendBytes`, JavaScriptCore took 1.9x as long. if (dropIt === undefined) { - for (let item = it.next(); item !== DONE; item = it.next()) byteVecPush(out, item); + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) byteVecPush(out, item); return out; } try { - for (let item = it.next(); item !== DONE; item = it.next()) byteVecPush(out, item); + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) byteVecPush(out, item); } finally { dropIt?.(it); } @@ -725,7 +731,7 @@ function count(it: Iter, drop: Drop | undefined): number { return n; } let n = 0; - for (let item = it.next(); item !== DONE; item = it.next()) { + for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) { drop?.(item); n++; } @@ -799,13 +805,13 @@ function last(it: Iter, drop: Drop | undefined): T | Done { } let item: T | Done = DONE; if (drop === undefined) { - for (let next = it.next(); next !== DONE; next = it.next()) item = next; + for (let next = it.foldNext(); next !== DONE; next = it.foldNext()) item = next; return item; } for (;;) { let next: T | Done; try { - next = it.next(); + next = it.foldNext(); } catch (e) { if (item !== DONE) drop(item); throw e; @@ -1110,6 +1116,10 @@ class Borrowed extends Iter { return this.inner.next(); } + override foldNext(): T | Done { + return this.inner.foldNext(); + } + override nextBack(): T | Done { return this.inner.nextBack(); } @@ -2223,6 +2233,15 @@ export class RangeInclusive extends Iter { return lo; } + /** std's `try_fold`'s: `start` stops at `end`, which exhausts it, so it never wraps. */ + override foldNext(): T | Done { + if (rangeInclusiveIsEmpty(this)) return DONE; + const lo = this.lo; + if (lt(lo, this.hi)) this.lo = step(lo, 1, this.int)!; + else this.exhausted = true; + return lo; + } + override nextBack(): T | Done { if (rangeInclusiveIsEmpty(this)) return DONE; const hi = this.hi; From 7f9ef9886210184ade9c3bc3464624514e888f2d Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:18:57 -0700 Subject: [PATCH 3/6] chore(quest): rewrite ts-range-fold to the adaptors left Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 2 +- quest/m3/ts-range-fold.md | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) 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..088bf63ed 100644 --- a/quest/m3/ts-range-fold.md +++ b/quest/m3/ts-range-fold.md @@ -1,14 +1,24 @@ -# [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 `foldNext`, which each +consumer std folds reads by, and which `RangeInclusive` and a `&mut` borrow take as core's +`try_fold` does. What remains is each adaptor: + +- One whose std `try_fold` folds its iterator overrides `foldNext` to read its iterator's. +- `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, `foldNextBack`, 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. From dc8e8ab44dd8d67c3458bfe2fbf9e9652564c5d2 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:25:31 -0700 Subject: [PATCH 4/6] perf(ts): read a fold through folded, so only an inclusive range pays for its own step Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/wrappers.ts | 111 ++++++++++++++------------ ts/fixtures/lang/src/wrappers.rs | 13 +++ ts/runtime/rt/collections.ts | 15 ++-- ts/runtime/rt/iter.ts | 106 +++++++++++++++--------- 4 files changed, 153 insertions(+), 92 deletions(-) diff --git a/ts/fixtures/lang/expected/wrappers.ts b/ts/fixtures/lang/expected/wrappers.ts index f9d4c3107..9c2e68cbf 100644 --- a/ts/fixtures/lang/expected/wrappers.ts +++ b/ts/fixtures/lang/expected/wrappers.ts @@ -335,17 +335,30 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void const _167: rt.RangeInclusive = queued; rt.byteVecPush(out, rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterBorrow(_167)).n & 0xff); bounds$RangeInclusive_u64(out, queued); + const hashed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); + const _177: rt.RangeInclusive = hashed; + rt.byteVecPush(out, rt.hashLen(rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashInt, rt.u64Eq)) as rt.HashSet, rt.iterBorrow(_177))) & 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 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 _200: [{ 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 } = _200[0]; - const from: { start: rt.usize } = _200[1]; + 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 _237: [{ 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 } = _237[0]; + const from: { start: rt.usize } = _237[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); @@ -354,28 +367,28 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void 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 _245: [rt.usize, rt.u8] | rt.Done = iter.next(); - if (_245 === rt.DONE) { + const _282: [rt.usize, rt.u8] | rt.Done = iter.next(); + if (_282 === rt.DONE) { break; } - rt.btreeInsert(map, _245[1] as rt.u64, _245[0] as rt.u64); + rt.btreeInsert(map, _282[1] as rt.u64, _282[0] as rt.u64); } - const _256: [rt.u64, rt.u64] = [at(0) as rt.u64, at(1) as rt.u64]; - const a2: rt.u64 = _256[0]; - const b2: rt.u64 = _256[1]; + const _293: [rt.u64, rt.u64] = [at(0) as rt.u64, at(1) as rt.u64]; + const a2: rt.u64 = _293[0]; + const b2: rt.u64 = _293[1]; const iter2: rt.Iter<[rt.u64, rt.u64]> = rt.btreeRange(map, { end: a2, inclusive: true }); while (true) { - const _270: [rt.u64, rt.u64] | rt.Done = iter2.next(); - if (_270 === rt.DONE) { + const _307: [rt.u64, rt.u64] | rt.Done = iter2.next(); + if (_307 === rt.DONE) { break; } - putU64(out, _270[0]); - putU64(out, _270[1]); + putU64(out, _307[0]); + putU64(out, _307[1]); } rt.byteVecPush(out, 254); - const _280: [rt.u64, rt.u64] | undefined = rt.iterToOption(rt.btreeRange(map, { end: b2, inclusive: true }).nextBack()); - if (_280 !== undefined) { - putU64(out, _280[0]); + const _317: [rt.u64, rt.u64] | undefined = rt.iterToOption(rt.btreeRange(map, { end: b2, inclusive: true }).nextBack()); + if (_317 !== undefined) { + putU64(out, _317[0]); } 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); @@ -389,42 +402,42 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void 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 _333: [rt.u64, rt.u64] | rt.Done = iter3.next(); - if (_333 === rt.DONE) { + const _370: [rt.u64, rt.u64] | rt.Done = iter3.next(); + if (_370 === rt.DONE) { break; } - putU64(out, _333[0]); - putU64(out, _333[1]); + putU64(out, _370[0]); + putU64(out, _370[1]); } rt.byteVecPush(out, rt.btreeLen(map) & 0xff); rt.byteVecPush(out, rt.btreeLen(above) & 0xff); - const _351: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); - const set: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _351, true); - const _356: rt.u8 = at(2); - const high: rt.BTreeSet = rt.btreeSplitOff(set, _356); + const _388: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); + const set: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _388, true); + const _393: rt.u8 = at(2); + const high: rt.BTreeSet = rt.btreeSplitOff(set, _393); rt.iterExtendBytes(out, rt.iterChain(rt.btreeKeys(set), rt.btreeKeys(high))); rt.byteVecPush(out, rt.btreeLen(high) & 0xff); } else if (_21 === 2) { - const _371: [rt.u8, rt.u8] = [at(0), at(1)]; - const x2: rt.u8 = _371[0]; - const y: rt.u8 = _371[1]; + const _408: [rt.u8, rt.u8] = [at(0), at(1)]; + const x2: rt.u8 = _408[0]; + const y: rt.u8 = _408[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 _415: rt.Iter<[rt.u8, rt.usize]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_2: [rt.usize, rt.u8]): [rt.u8, rt.usize] => { + const _452: 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, _415, true)); + const iter4: rt.Iter<[rt.u8, rt.usize]> = rt.btreeIter(rt.btreeExtend(rt.btreeNew(rt.cmpReverse(rt.cmp)) as rt.BTreeMap, _452, true)); while (true) { - const _421: [rt.u8, rt.usize] | rt.Done = iter4.next(); - if (_421 === rt.DONE) { + const _458: [rt.u8, rt.usize] | rt.Done = iter4.next(); + if (_458 === rt.DONE) { break; } - rt.byteVecPush(out, _421[0]); - rt.byteVecPush(out, _421[1] & 0xff); + rt.byteVecPush(out, _458[0]); + rt.byteVecPush(out, _458[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]; @@ -432,9 +445,9 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void 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 _441 of ranked) { - const d: rt.u8 = _441[0]; - const i2: rt.usize = _441[1]; + for (const _478 of ranked) { + const d: rt.u8 = _478[0]; + const i2: rt.usize = _478[1]; rt.byteVecPush(out, d); rt.byteVecPush(out, i2 & 0xff); } @@ -447,28 +460,28 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void const n2: rt.u64 = n; putU64(out, n2); putU64(out, n2); - const _473: rt.u64 = unwrapOr$Option_NonZero_u64(rt.nonZero(b), n2); - const _470: boolean = rt.u64Eq(n2, _473); - const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u64Display(n2, f), (f: rt.Formatter) => rt.u64Debug(n2, f), (f: rt.Formatter) => rt.fmtBool(_470, f)]; + const _510: rt.u64 = unwrapOr$Option_NonZero_u64(rt.nonZero(b), n2); + const _507: boolean = rt.u64Eq(n2, _510); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u64Display(n2, f), (f: rt.Formatter) => rt.u64Debug(n2, f), (f: rt.Formatter) => rt.fmtBool(_507, 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 _500: rt.u64 | undefined = rt.nonZero(a); + const _537: rt.u64 | undefined = rt.nonZero(a); b1: { - if (_500 !== undefined) { + if (_537 !== undefined) { if (rt.u8Rem(at(2), 2) === 0) { - scale = { _0: _500 }; + scale = { _0: _537 }; break b1; } } - const _511: rt.usize = rt.usizeRem(at(3) as rt.usize, 3); - if (!(_511 < 3)) { - rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _511); + const _548: rt.usize = rt.usizeRem(at(3) as rt.usize, 3); + if (!(_548 < 3)) { + rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _548); } - scale = [{ _0: 1 }, { _0: 1000 }, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }][_511]; + scale = [{ _0: 1 }, { _0: 1000 }, { _0: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64 }][_548]; } if (!rt.u64Eq(1, scale._0)) { if (!rt.u64Eq(1000, scale._0)) { diff --git a/ts/fixtures/lang/src/wrappers.rs b/ts/fixtures/lang/src/wrappers.rs index 2e8bc9024..d9dc1b080 100644 --- a/ts/fixtures/lang/src/wrappers.rs +++ b/ts/fixtures/lang/src/wrappers.rs @@ -128,6 +128,19 @@ pub fn wrappers(kind: u8, data: &[u8], out: &mut Vec) { 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 78360993b..525166f6b 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -867,7 +867,8 @@ export function hashExtend( fresh = false, ): HashMap { const f = () => { - for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) { + const folded = items.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) { const old = hashInsert(m, item[0], item[1], k, v); if (old !== undefined) v?.(old); } @@ -878,7 +879,8 @@ export function hashExtend( /** `extend` of a set with keys, and `collect` into one, as `hashExtend`: a key already there drops. */ export function hashExtendKeys(m: HashSet, keys: Iter, k?: (key: K) => void, dropIt?: (it: Iter) => void, fresh = false): HashSet { const f = () => { - for (let key = keys.foldNext(); key !== DONE; key = keys.foldNext()) hashAdd(m, key, k); + const folded = keys.folded(); + for (let key = folded.next(); key !== DONE; key = folded.next()) hashAdd(m, key, k); }; return extending(m, keys, f, dropIt, fresh, (m) => hashDrop(m, k)); } @@ -1599,7 +1601,8 @@ export function btreeExtend( dropIt?: (it: Iter<[K, V]>) => void, ): BTreeMap { const f = () => { - for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) { + const folded = items.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) { if (!last) { const old = btreeInsert(m, item[0], item[1], k, v); if (old !== undefined) v?.(old); @@ -1620,7 +1623,8 @@ export function btreeExtend( /** `extend` of a set with keys, and with `last`, `collect` and `from`, as `btreeExtend`. */ export function btreeExtendKeys(m: BTreeSet, keys: Iter, last = false, k?: (key: K) => void, dropIt?: (it: Iter) => void): BTreeSet { const f = () => { - for (let key = keys.foldNext(); key !== DONE; key = keys.foldNext()) { + const folded = keys.folded(); + for (let key = folded.next(); key !== DONE; key = folded.next()) { if (!last) { btreeAdd(m, key, k); continue; @@ -2146,7 +2150,8 @@ export function dequeDrain(q: VecDeque, r: Bounds, drop?: (item: T /** `extend`, and `collect` into a deque through `dequeNew`, which drops `items` by `dropIt` once done. */ export function dequeExtend(q: VecDeque, items: Iter, dropIt?: (it: Iter) => void): VecDeque { const f = () => { - for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) dequePushBack(q, item); + const folded = items.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) dequePushBack(q, item); }; return extending(q, items, f, dropIt); } diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 227bdb351..e51449f5e 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -14,8 +14,8 @@ // 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. A consumer std folds reads by `foldNext`, which an inclusive -// range takes as its `try_fold` does. Where std's `count` and `last` drop items in an +// 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. @@ -82,9 +82,9 @@ export abstract class Iter implements Iterable { return this.advanceBackBy(n, drop) === 0 ? this.nextBack() : DONE; } - /** The next item as std's `try_fold` takes it, which only an inclusive range takes other than by `next`. */ - foldNext(): T | Done { - return this.next(); + /** What std's `try_fold` reads it by: itself, but for an inclusive range, which steps its own way. */ + folded(): Iter { + return this; } /** Its items, for a JS `for ... of`. */ @@ -207,12 +207,13 @@ export function iterBorrow(it: Iter): Iter { /** `Extend::extend` of a `Vec`, which drops `items` by `dropIt` once done or a panic. */ export function iterExtend(v: T[], items: Iter, dropIt?: Drop>): void { + const folded = items.folded(); if (dropIt === undefined) { - for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) v.push(item); + for (let item = folded.next(); item !== DONE; item = folded.next()) v.push(item); return; } try { - for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) v.push(item); + for (let item = folded.next(); item !== DONE; item = folded.next()) v.push(item); } finally { dropIt(items); } @@ -220,8 +221,9 @@ export function iterExtend(v: T[], items: Iter, dropIt?: Drop>): v /** `Extend::extend` of a `Vec`. */ export function iterExtendBytes(v: ByteVec, items: Iter, dropIt?: Drop>): void { + const folded = items.folded(); try { - for (let item = items.foldNext(); item !== DONE; item = items.foldNext()) byteVecPush(v, item); + for (let item = folded.next(); item !== DONE; item = folded.next()) byteVecPush(v, item); } finally { dropIt?.(items); } @@ -352,15 +354,17 @@ export function iterFlatten(it: Iter, into?: (value: T) => Iter, fus /** `Iterator::count` of an iterator that drops nothing. */ export function iterCount(it: Iter): number { + const folded = it.folded(); let n = 0; - while (it.foldNext() !== 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.foldNext(); item !== DONE; item = it.foldNext()) last = item; + for (let item = folded.next(); item !== DONE; item = folded.next()) last = item; return iterToOption(last); } @@ -379,23 +383,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.foldNext(); item !== DONE; item = it.foldNext()) 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.foldNext(); item !== DONE; item = it.foldNext()) 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.foldNext(); item !== DONE; item = it.foldNext()) 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.foldNext(); item !== DONE; item = it.foldNext()) { + for (let item = folded.next(); item !== DONE; item = folded.next()) { if (test(f, item, drop)) return item; drop(item); } @@ -428,7 +435,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.foldNext(); item !== DONE; item = it.foldNext()) { + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) { const found = f(item); if (found !== undefined) return found; } @@ -437,8 +445,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.foldNext(); item !== DONE; item = it.foldNext()) { + for (let item = folded.next(); item !== DONE; item = folded.next()) { if (f(item)) return i; i++; } @@ -450,16 +459,17 @@ export function iterPosition(it: Iter, f: (item: T) => boolean): number | * `f`, which took it, leaves it to `f`. */ export function iterFold(it: Iter, init: A, f: (acc: A, item: T) => A, dropIt?: Drop>, dropAcc?: Drop): A { + const folded = it.folded(); let acc = init; if (dropIt === undefined && dropAcc === undefined) { - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) acc = f(acc, item); + for (let item = folded.next(); item !== DONE; item = folded.next()) acc = f(acc, item); return acc; } try { for (;;) { let item: T | Done; try { - item = it.foldNext(); + item = folded.next(); } catch (e) { dropAcc?.(acc); throw e; @@ -478,12 +488,13 @@ 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, dropIt?: Drop>): void { + const folded = it.folded(); if (dropIt === undefined) { - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) f(item); + for (let item = folded.next(); item !== DONE; item = folded.next()) f(item); return; } try { - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) f(item); + for (let item = folded.next(); item !== DONE; item = folded.next()) f(item); } finally { dropIt(it); } @@ -526,11 +537,12 @@ export function iterMinByKey(it: Iter, * std's unwinding does a returned value. */ function best(it: Iter, f: (item: T) => K, least: boolean, dropIt?: Drop>, drop?: Drop): T | undefined { + const folded = it.folded(); if (dropIt === undefined && drop === undefined) { let best = it.next(); if (best === DONE) return undefined; let key = f(best); - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) { + 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]; } @@ -539,7 +551,7 @@ function best(it: Iter, f: (item: T) = let best: T | Done = DONE; let key: K | undefined; try { - for (let item = it.next(); item !== DONE; item = it.foldNext()) { + for (let item = it.next(); item !== DONE; item = folded.next()) { let k: K; try { k = f(item); @@ -580,15 +592,16 @@ function best(it: Iter, f: (item: T) = * way around. */ export function iterToArray(it: Iter, inPlace = false, dropIt?: Drop>, drop?: Drop): T[] { + const folded = it.folded(); const out: T[] = []; if (dropIt === undefined && drop === undefined) { - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) out.push(item); + 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. const collected = () => drop !== undefined && dropAll(out, drop); try { - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) out.push(item); + for (let item = folded.next(); item !== DONE; item = folded.next()) out.push(item); } catch (e) { if (inPlace) both(collected, () => dropIt?.(it)); else both(() => dropIt?.(it), collected); @@ -605,14 +618,15 @@ export function iterToArray(it: Iter, inPlace = false, dropIt?: Drop`. */ export function iterToByteVec(it: Iter, dropIt?: Drop>): ByteVec { + const folded = it.folded(); const out = new ByteVec(); // Its own loop: through `iterExtendBytes`, JavaScriptCore took 1.9x as long. if (dropIt === undefined) { - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) byteVecPush(out, item); + for (let item = folded.next(); item !== DONE; item = folded.next()) byteVecPush(out, item); return out; } try { - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) byteVecPush(out, item); + for (let item = folded.next(); item !== DONE; item = folded.next()) byteVecPush(out, item); } finally { dropIt?.(it); } @@ -730,8 +744,9 @@ function count(it: Iter, drop: Drop | undefined): number { dispose(it); return n; } + const folded = it.folded(); let n = 0; - for (let item = it.foldNext(); item !== DONE; item = it.foldNext()) { + for (let item = folded.next(); item !== DONE; item = folded.next()) { drop?.(item); n++; } @@ -803,15 +818,16 @@ function last(it: Iter, drop: Drop | undefined): T | Done { if (peeked !== DONE) drop?.(peeked); return item; } + const folded = it.folded(); let item: T | Done = DONE; if (drop === undefined) { - for (let next = it.foldNext(); next !== DONE; next = it.foldNext()) item = next; + for (let next = folded.next(); next !== DONE; next = folded.next()) item = next; return item; } for (;;) { let next: T | Done; try { - next = it.foldNext(); + next = folded.next(); } catch (e) { if (item !== DONE) drop(item); throw e; @@ -1116,8 +1132,8 @@ class Borrowed extends Iter { return this.inner.next(); } - override foldNext(): T | Done { - return this.inner.foldNext(); + override folded(): Iter { + return this.inner.folded(); } override nextBack(): T | Done { @@ -2233,13 +2249,8 @@ export class RangeInclusive extends Iter { return lo; } - /** std's `try_fold`'s: `start` stops at `end`, which exhausts it, so it never wraps. */ - override foldNext(): T | Done { - if (rangeInclusiveIsEmpty(this)) return DONE; - const lo = this.lo; - if (lt(lo, this.hi)) this.lo = step(lo, 1, this.int)!; - else this.exhausted = true; - return lo; + override folded(): Iter { + return new RangeInclusiveFold(this); } override nextBack(): T | Done { @@ -2274,6 +2285,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; From 6b315ba21fef824549db43239467813f6aa82ba6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:32:31 -0700 Subject: [PATCH 5/6] chore(quest): name folded in ts-range-fold's plan Co-Authored-By: Claude Opus 5.5 --- quest/m3/ts-range-fold.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/quest/m3/ts-range-fold.md b/quest/m3/ts-range-fold.md index 088bf63ed..f27896bf2 100644 --- a/quest/m3/ts-range-fold.md +++ b/quest/m3/ts-range-fold.md @@ -9,14 +9,16 @@ against native Rust. ## Plan -[#179](https://github.com/kixelated/rust2/pull/179) gave `rt.Iter` a `foldNext`, which each -consumer std folds reads by, and which `RangeInclusive` and a `&mut` borrow take as core's -`try_fold` does. What remains is each adaptor: +[#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`. What remains is each adaptor: -- One whose std `try_fold` folds its iterator overrides `foldNext` to read its iterator's. +- 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, `foldNextBack`, as core's `try_rfold` takes it. +- `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`. From 4c9bd7528b24b9c12c862af782200b06d15ff3ec Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 08:04:53 -0700 Subject: [PATCH 6/6] fix(ts): step an inclusive range std's way in the default fold too A fold whose accumulator drops takes the iterator's own fold, whose default read by next. Co-Authored-By: Claude Opus 5.5 --- quest/m3/ts-range-fold.md | 6 +- ts/fixtures/lang/expected/wrappers.Tally.ts | 21 + ts/fixtures/lang/expected/wrappers.ts | 493 +++++++++++--------- ts/fixtures/lang/src/wrappers.rs | 11 + ts/runtime/rt/iter.ts | 8 +- 5 files changed, 304 insertions(+), 235 deletions(-) create mode 100644 ts/fixtures/lang/expected/wrappers.Tally.ts diff --git a/quest/m3/ts-range-fold.md b/quest/m3/ts-range-fold.md index c95b205f5..adfe3de8a 100644 --- a/quest/m3/ts-range-fold.md +++ b/quest/m3/ts-range-fold.md @@ -13,9 +13,9 @@ against native Rust. 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, already forwards through -each adaptor but reaches a range's default `fold`, by `next`: an override there is part of -this. What remains is each adaptor: +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. 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 9c2e68cbf..0b1dc953a 100644 --- a/ts/fixtures/lang/expected/wrappers.ts +++ b/ts/fixtures/lang/expected/wrappers.ts @@ -4,6 +4,7 @@ import * as rt from "./rt.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,247 +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); - 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 keyed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); - const _157: rt.RangeInclusive = keyed; - rt.byteVecPush(out, rt.btreeLen(rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterBorrow(_157), true)) & 0xff); - bounds$RangeInclusive_u64(out, keyed); - const queued: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); - const _167: rt.RangeInclusive = queued; - rt.byteVecPush(out, rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterBorrow(_167)).n & 0xff); - bounds$RangeInclusive_u64(out, queued); - const hashed: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); - const _177: rt.RangeInclusive = hashed; - rt.byteVecPush(out, rt.hashLen(rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashInt, rt.u64Eq)) as rt.HashSet, rt.iterBorrow(_177))) & 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 _237: [{ 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 } = _237[0]; - const from: { start: rt.usize } = _237[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 _282: [rt.usize, rt.u8] | rt.Done = iter.next(); - if (_282 === 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, _282[1] as rt.u64, _282[0] as rt.u64); - } - const _293: [rt.u64, rt.u64] = [at(0) as rt.u64, at(1) as rt.u64]; - const a2: rt.u64 = _293[0]; - const b2: rt.u64 = _293[1]; - const iter2: rt.Iter<[rt.u64, rt.u64]> = rt.btreeRange(map, { end: a2, inclusive: true }); - while (true) { - const _307: [rt.u64, rt.u64] | rt.Done = iter2.next(); - if (_307 === 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, _307[0]); - putU64(out, _307[1]); - } - rt.byteVecPush(out, 254); - const _317: [rt.u64, rt.u64] | undefined = rt.iterToOption(rt.btreeRange(map, { end: b2, inclusive: true }).nextBack()); - if (_317 !== undefined) { - putU64(out, _317[0]); - } - 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 _370: [rt.u64, rt.u64] | rt.Done = iter3.next(); - if (_370 === 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, _370[0]); - putU64(out, _370[1]); - } - rt.byteVecPush(out, rt.btreeLen(map) & 0xff); - rt.byteVecPush(out, rt.btreeLen(above) & 0xff); - const _388: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); - const set: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _388, true); - const _393: rt.u8 = at(2); - const high: rt.BTreeSet = rt.btreeSplitOff(set, _393); - rt.iterExtendBytes(out, rt.iterChain(rt.btreeKeys(set), rt.btreeKeys(high))); - rt.byteVecPush(out, rt.btreeLen(high) & 0xff); - } else if (_21 === 2) { - const _408: [rt.u8, rt.u8] = [at(0), at(1)]; - const x2: rt.u8 = _408[0]; - const y: rt.u8 = _408[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 _452: 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, _452, true)); - while (true) { - const _458: [rt.u8, rt.usize] | rt.Done = iter4.next(); - if (_458 === 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, _458[0]); - rt.byteVecPush(out, _458[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 _478 of ranked) { - const d: rt.u8 = _478[0]; - const i2: rt.usize = _478[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 _510: rt.u64 = unwrapOr$Option_NonZero_u64(rt.nonZero(b), n2); - const _507: boolean = rt.u64Eq(n2, _510); - const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u64Display(n2, f), (f: rt.Formatter) => rt.u64Debug(n2, f), (f: rt.Formatter) => rt.fmtBool(_507, 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 _537: rt.u64 | undefined = rt.nonZero(a); - b1: { - if (_537 !== undefined) { - if (rt.u8Rem(at(2), 2) === 0) { - scale = { _0: _537 }; - 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 _548: rt.usize = rt.usizeRem(at(3) as rt.usize, 3); - if (!(_548 < 3)) { - rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _548); + 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 }][_548]; - } - 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 d9dc1b080..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 { @@ -122,6 +129,10 @@ pub fn wrappers(kind: u8, data: &[u8], out: &mut Vec) { 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); diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index d0d78a7e6..3134dea20 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -109,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;