Skip to content

feat(ts): call a crate's own find, rfind, find_map, for_each, reduce, and all where std does - #501

Merged
kixelated merged 7 commits into
mainfrom
quest/m3/ts-iter-own-calls
Nov 3, 2026
Merged

kixelated merged 7 commits into
mainfrom
quest/m3/ts-iter-own-calls

Conversation

@kixelated

@kixelated kixelated commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused any impl Iterator of the crate that overrides a provided method other than the ones rt.iterFrom already called (size_hint, next_back, len, nth, nth_back, fold, rfold, count, last). std's adaptors and consumers call several more of a crate's own methods in place of next:

  • Filter::next and SkipWhile::next call find, Filter::next_back calls rfind, and FilterMap::next calls find_map. Chain, Fuse, and Rev forward find and rfind.
  • Extend of String, hash and B-tree collections, LinkedList, (), and tuples (so unzip) calls for_each.
  • max, min, max_by, and min_by call reduce. is_sorted and is_sorted_by call all.

Overrides std never calls in place of another, such as position or max, were refused too, though only the crate's own calls reach them.

Approach

  • rt.Own gains find, rfind, findMap, forEach, reduce, and all. As with fold, the crate's method is translated once, for its closure as a Box<dyn FnMut>. find_map's found B is a u32 stand-in, and the runtime boxes each value in an array where it crosses, so no value of it is the None. erases checks each of these bodies as it checked fold's, now over all of the method's type parameters.
  • rt.Iter gets find, rfind, and findMap methods. Their defaults read the iterator as try_fold does. FromNext calls its own; Chain, Fuse, and Rev forward as std's do. Filter, SkipWhile, and FilterMap read their iterator by these in next, and by folded in their try_fold/fold path. finds() keeps that path allocation-free unless the crate's own find is reachable.
  • forEach, best (max/min/max_by/min_by), and iterIsSortedBy dispatch to the crate's own for_each, reduce, or all when they are given the crate's iterator itself. Take's "fold", which models std's for_each in every collection extend, now reads by forEach. ()'s extend uses iterForEach.
  • Only what std's Iterator for &mut I forwards (BORROWED) reaches a borrowed iterator's record. iterBox already forwards none of the new methods.
  • Fails loud: an own find/rfind whose predicate would take a &[u8; N] (std lends the array, the crate's body a slice). collect, eq, partial_cmp, and by_ref stay refused, with the message reworded since collect's caller is a FromIterator, not an adaptor.

Tests: lang/ends' Overrides now also overrides find, rfind, find_map, for_each, reduce, all, position, and max, each logged. A new found consumer reads it by value, through &mut, and boxed, plus plain Run, through every adaptor and consumer above, including the try_fold paths that must not call find, a find_map whose None is rt.None, and droppy comparators. Mutation check: disabling the Chain find forwarding, the reduce dispatch, or the forEach dispatch fails 20 inputs. The reject fixture covers the byte-array rfind, a still-refused collect, and a find_map that drops what it finds.

Perf: a Node A/B of filter/filter_map/chain-find loops over the old and new runtime showed no regression (old 31 to 35 ms, new 31 to 32 ms median, noisy host). The full just ts bench wasn't run (it needs the bench shell and browsers).

Impact

  • Translator output: a crate's iterator overriding find, rfind, find_map, for_each, reduce, or all now translates, as does one overriding a method std never calls in place of another (any, position, rposition, max, min, max_by, min_by, max_by_key, min_by_key, sum, product, partition, unzip, cmp, ne, lt, le, gt, ge, is_sorted, is_sorted_by_key).
  • Runtime: rt.Own gains optional keys; rt.Iter gains find, rfind, findMap, and finds; rt.iterForEach returns undefined. All additive, so no MIGRATE.md entry.
  • Consumer: none in moq today. This is m3's all-of-std iterator coverage.

Alternatives

  • Translating collect, eq, and partial_cmp here too. They are generic over another iterator or a collection, so one instance per iterator, as rt.Own holds, doesn't fit. Each needs a call of the crate's instance at the call site std's mapping reaches. That is independently landable, so the quest now holds just those (plus by_ref, which Vec::splice calls).
  • Always allocating a Folded in Filter::folded. Rejected: the shunts behind collect::<Result<_, _>> call folded() per item.

Follow-ups

Review

🤖 Generated with Claude Code

kixelated and others added 4 commits November 2, 2026 22:59
… and all where std does

Filter, SkipWhile, and FilterMap read their iterator by its find, rfind, or
find_map, which Chain, Fuse, and Rev forward; Extend of hash and B-tree
collections, LinkedList, (), and tuples reads it by for_each; max, min, max_by,
and min_by reduce it, and is_sorted and is_sorted_by read it by all. rt.iterFrom
calls the crate's own of each where std's does, but through &mut or a box,
which forward none. Overrides std never calls in place of another, such as
position or max, no longer refuse the impl. collect, eq, partial_cmp, and
by_ref stay refused, and the quest now holds them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title A crate's own find and for_each feat(ts): call a crate's own find, rfind, find_map, for_each, reduce, and all where std does Nov 3, 2026
@kixelated
kixelated marked this pull request as ready for review November 3, 2026 09:26
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 56 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3423390a-02cb-402e-bafa-2ffe126926f5
Commits

Reviewing files that changed from the base of the PR and between fb428b8 and 78793ba.

Files selected for processing: 19
  • quest/m3/README.md
  • quest/m3/ts-iter-own-calls.md
  • ts/README.md
  • ts/fixtures/lang/ends/caller.ts
  • ts/fixtures/lang/ends/expected/core/bool.ts
  • ts/fixtures/lang/ends/expected/core/option.ts
  • ts/fixtures/lang/ends/expected/ends.Guard.ts
  • ts/fixtures/lang/ends/expected/ends.Overrides.ts
  • ts/fixtures/lang/ends/expected/ends.ts
  • ts/fixtures/lang/ends/src/ends.rs
  • ts/fixtures/lang/tuples/expected/tuples.ts
  • ts/fixtures/reject/census.txt
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/reject/src/iters.rs
  • ts/fixtures/reject/ts.toml
  • ts/runtime/rt/collections.ts
  • ts/runtime/rt/iter.ts
  • ts/src/lower/collections.rs
  • ts/src/lower/iter.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@kixelated
kixelated enabled auto-merge November 3, 2026 09:32
@kixelated
kixelated added this pull request to the merge queue Nov 3, 2026
Merged via the queue into main with commit fb21daf Nov 3, 2026
5 checks passed
@kixelated
kixelated deleted the quest/m3/ts-iter-own-calls branch November 3, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant