Skip to content
2 changes: 1 addition & 1 deletion quest/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Future>` 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
27 changes: 21 additions & 6 deletions quest/m3/ts-range-fold.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions ts/fixtures/lang/expected/wrappers.Tally.ts
Original file line number Diff line number Diff line change
@@ -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
Loading