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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quest/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ generic std gap is planned here even when moq never calls it.
- [std's char methods that have no mapping](/quest/m3/ts-char-methods.md) - `is_alphabetic`, `to_lowercase`, `len_utf8`, the escapes, and the rest of `char` translate, called or as values, by std's Unicode version
- [Waiting: a slice object for a mutable part of other values](/quest/m3/ts-slice-object.md) - waits on a moq profile showing the `Proxy` view's traps matter, then decides on an `rt.ByteSlice`-like form
- [Port the workflow guard to kixelated/quest](/quest/m3/quest-workflow-guard.md) - quest's environment guard parses YAML as this repository's does
- [A crate's own find and for_each](/quest/m3/ts-iter-own-calls.md) - a crate's iterator that overrides `find`, `rfind`, `find_map`, or `for_each` translates, each called where std's adaptors and consumers call it
- [A crate's own collect, eq, partial_cmp, and by_ref](/quest/m3/ts-iter-own-calls.md) - a crate's iterator that overrides `collect`, `eq`, `partial_cmp`, or `by_ref` translates, each called where std calls it
- [A vector's exact capacity as it shrinks](/quest/m3/ts-vec-exact-capacity.md) - a deque made from a `Vec` splits its slices where std's does
- [A B-tree's searches call Ord as std's do](/quest/m3/ts-btree-search-order.md) - a B-tree's lookups and inserts call a key's own `Ord` in std's order and number
- [A Vec from a slice, and a slice's chunks as arrays](/quest/m3/ts-slice-conversions.md) - `Vec::from(&[T])` and `as_chunks` translate
Expand Down
40 changes: 17 additions & 23 deletions quest/m3/ts-iter-own-calls.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
# [M] A crate's own find and for_each
# [S] A crate's own collect, eq, partial_cmp, and by_ref

## Goal

[rust2-ts](/ts/README.md) translates a crate's own iterator that overrides a provided
method std's adaptors and consumers call on it other than those `rt.iterFrom` already
calls (`size_hint`, `next_back`, `len`, `nth`, `nth_back`, `fold`, `rfold`, `count`, and
`last`), such as `find`, `rfind`, `find_map`, or `for_each`. Each runs where native's does,
as many times and in the same order, proven by differential tests against native Rust.
[rust2-ts](/ts/README.md) translates a crate's own iterator that overrides `collect`, `eq`,
`partial_cmp`, or `by_ref`, which std calls of the iterator in place of another method. Each
runs where native's does, as many times and in the same order, proven by differential tests
against native Rust.

## Plan

Left by the PR that finished folds from the back. Each is refused today as "overriding
`_`, which std's adaptors call in place of `next` or beside it":
Left by the PR that translated a crate's own `find`, `rfind`, `find_map`, `for_each`,
`reduce`, and `all`. Each is refused today as "overriding `_`, which std calls in place of
another method":

- `Filter::next` and `SkipWhile::next` call their iterator's `find`, `FilterMap::next` its
`find_map`, and `Fuse`, `Chain`, and `Rev` forward `find` and `rfind`. The runtime's
adaptors read their iterator by `next` there, which is the same for std's own iterators
but skips a crate's override.
- `for_each` is what std's `Extend` impls for hash maps, `String`, and others call of the
iterator they are given.
- Each takes a closure, as `fold` does: the crate's `fold` translates for one
`Box<dyn FnMut>` accumulator-agnostic instance, which these could share.
- Overrides std never calls in place of another, such as `max` or `position`, only matter
where the crate calls them directly. Check which std calls each before lifting its
refusal.
- `try_fold`, `try_rfold`, `advance_by`, and `advance_back_by` can be overridden only on
nightly, through unstable traits and features.
- `lang`'s `ends` fixture's `Overrides` iterator logs each method it is called through, and
can extend to these.
- std's `FromIterator` of `Box<[T]>`, `Rc<[T]>`, `Arc<[T]>`, `BTreeMap`, `BTreeSet`, and
`BinaryHeap` calls the iterator's `collect` for a `Vec` first.
- `ne` calls the iterator's `eq`, and `lt`, `le`, `gt`, and `ge` its `partial_cmp`.
- `Vec::splice` calls its replacement's `by_ref` as the splice drops.
- Unlike the overrides taking a closure, these are generic over another iterator or a
collection, so one instance per iterator, as `rt.iterFrom`'s `rt.Own` record holds, doesn't
fit. A call of the crate's instance for that type where std's mapping reaches it may.
- Adaptor constructors such as `map`, and nightly-only overrides such as `try_fold`, stay
refused: an override of either can't return without diverging or a feature.

Public API: the translator's output.
24 changes: 16 additions & 8 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,12 @@ An iterator over a slice, array, or `Vec`, or a small range, that only its `next
moves read steps in place, as two variables: the array and an index into it, or the
range's bounds, with no `rt.Iter` made. The crate's own `impl Iterator` returns `rt.DONE` from its `next` too, and joins
std's adaptors through `rt.iterFrom`, which calls its own `size_hint`, `next_back`, `len`, `nth`, and `nth_back`,
and of one it owns its own `fold`, `rfold`, `count`, and `last`, where std's adaptors
call them, and `fuse` reads on past the end of one that is a `FusedIterator`, as std's does. A caller calls `next()` for one item, or iterates
and of one it owns, not borrows by `&mut` or boxes, its own `fold`, `rfold`, `count`, `last`,
`find`, `rfind`, `find_map`, `for_each`, `reduce`, and `all`, where std's adaptors and consumers
call them: a filter's `next` calls `find`, an `extend` into a hash or B-tree collection, a
`String`, or a tuple calls `for_each`, `max` calls `reduce`, and `is_sorted` calls `all`. Its
own `position`, `max`, and the others std never calls in place of another run only where the
crate calls them, and `fuse` reads on past the end of one that is a `FusedIterator`, as std's does. A caller calls `next()` for one item, or iterates
an `rt.Iter` with `for ... of`. Each `rt.Iter`'s `sizeHint` is std's `size_hint`, its upper
bound `undefined` for `None`, and a `collect` or `extend` into a `Vec`, `VecDeque`,
`BinaryHeap`, B-tree, or hash collection reserves by it where std's does, panicking with
Expand Down Expand Up @@ -464,7 +468,9 @@ map's `drain` does, drops those it still holds by `[Symbol.dispose]`, an adaptor
what it skips or rejects, and a consumer that takes an iterator by value drops it:
through each `rt.Iter`'s own `fold` where std's folds, which drops each adaptor's closure
as its fold ends, before what it wraps, as std's does. The crate's own `fold` is given that
closure as a `Box<dyn FnMut>`, which drops it where its body does. `try_fold`, `try_rfold`,
closure as a `Box<dyn FnMut>`, which drops it where its body does, and so are its own
`find`, `find_map`, `for_each`, `reduce`, and `all`, whose `find_map` is given what it finds
boxed. `try_fold`, `try_rfold`,
and `try_for_each` drop their closure as they return, leaving what the iterator holds to drop
with it. `scan` hands its closure its state, or the state's place where JS copies it, and
drops the state after the closure, as std's fields drop. A `Vec`'s `drain` leaves the vector
Expand Down Expand Up @@ -929,11 +935,13 @@ another type it borrows as through the crate's own `Borrow`, a `&mut` to a value
copies that std makes, other than a map's, or hands a closure, as `iter_mut` and
`and_modify` do, one into a `Result`'s payload passed on or kept, the adaptors
without a mapping in `src/lower/iter.rs` (such as nightly's `intersperse`), an
own `impl Iterator` or `DoubleEndedIterator` that overrides a provided method other than
those, such as `find` or `for_each`, or whose own `fold` or `rfold` tells its accumulator or
closure apart from another, by an `Option` or a `Poll` of it, a call or constant generic over it
other than calling or dropping the closure or passing them to std's iterators outside the crate's
own types, or a drop of the accumulator,
own `impl Iterator` or `DoubleEndedIterator` that overrides a provided method std calls in place
of another other than those, such as `collect`, `eq`, `partial_cmp`, or `by_ref`, or whose own
method taking a closure tells it, or its accumulator or what it finds, apart from another, by an
`Option` or a `Poll` of the accumulator, a call or constant generic over them other than calling
or dropping the closure or passing them to std's iterators outside the crate's own types, or a
drop of the accumulator or what it finds, or whose own `find` or `rfind` gives its predicate a
reference to a `[u8; N]`,
a pointer's address (`{:p}`), `write!` with a placeholder into
the crate's own `fmt::Write`, a `str` pattern other than a
`char`, a `&str`, a predicate behind any references and boxes, or chars, a mutable `str` slice, a copy of a closure that changes
Expand Down
9 changes: 5 additions & 4 deletions ts/fixtures/lang/ends/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { op } from "./lang.ts";
export { ops, run } from "./lang.ts";

/**
* `ends::ends`: each kind of iterator, and boxes of the crate's own, through every consumer
* and fold and either end's bits, of runs empty, short, and long, whose hints are exact,
* loose, and lying, after none and a few steps through each of its methods.
* `ends::ends`: each kind of iterator, and boxes and borrows of the crate's own, through
* every consumer, fold, and find and either end's bits, of runs empty, short, and long,
* whose hints are exact, loose, and lying, after none and a few steps through each of its
* methods.
*/
const endsCases = [0x00, 0x03, 0x0f, 0x15, 0x27, 0x36, 0x2f].flatMap((shape) =>
[[], [0x01, 0x00], [0x0a, 0x0d, 0x03], [0x05, 0x06, 0x04, 0x0f]].flatMap((steps) =>
Array.from({ length: 14 * 256 }, (_, i) => Uint8Array.of(...op("ends::ends"), i % 14, shape, Math.floor(i / 14), ...steps)),
Array.from({ length: 18 * 256 }, (_, i) => Uint8Array.of(...op("ends::ends"), i % 18, shape, Math.floor(i / 18), ...steps)),
),
);

Expand Down
7 changes: 7 additions & 0 deletions ts/fixtures/lang/ends/expected/core/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

export function thenSome$185ufyt(self: boolean, t: rt.u8 | undefined): rt.u8 | undefined | rt.None {
if (!self) {
return rt.None;
}
return t;
}

export function thenSome$0d5am30(self: boolean, t: rt.u8): rt.u8 | undefined {
if (!self) {
return undefined;
Expand Down
7 changes: 7 additions & 0 deletions ts/fixtures/lang/ends/expected/core/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

export function isNoneOr$1s1o3ss(self: rt.u8 | undefined, f: (a0: rt.u8) => boolean): boolean {
if (self === undefined) {
return true;
}
return f(self);
}

export function unwrapOr$0r2bmpr(self: rt.u8 | undefined, default_: rt.u8): rt.u8 {
if (self === undefined) {
return default_;
Expand Down
24 changes: 24 additions & 0 deletions ts/fixtures/lang/ends/expected/ends.Guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Generated by rust2-ts. Do not edit.

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

/** Logs `G` as it drops. */
export class Guard {
_0: rt.RefCell<rt.ByteVec>;

constructor(_0: rt.RefCell<rt.ByteVec>) {
this._0 = _0;
}

[Symbol.dispose](): void {
drop$ops_Drop(this);
}
}

export function drop$ops_Drop(self: Guard): void {
const _5: rt.RefCell<rt.ByteVec> = rt.refCellBorrowMut(self._0);
rt.byteVecPush(_5.value, 71);
rt.refMutRelease(_5);
}

//# sourceMappingURL=ends.Guard.ts.map
Loading