Skip to content

feat(ts): clone std's iterators and adaptors - #322

Merged
kixelated merged 5 commits into
mainfrom
quest/m3/ts-iter-clone
Oct 31, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m3/ts-iter-clone

Conversation

@kixelated

@kixelated kixelated commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Closes quest/m3/ts-iter-clone.md.

Problem

Clone::clone of std's iterators and adaptors refused, for example unsupported call to <std::iter::Zip<...> as std::clone::Clone>::clone at src/lib.rs:20:13. Reproduced on a scratch crate: slice Iter, Map, Zip, Chars, Peekable, B-tree map Iter/Keys/Values/Range, btree_set::Iter, hash_set::Iter, B-tree Union/Intersection/Difference, and Filter all refused at the .clone() span. Cloning a closure refused too: unsupported call to <{closure@src/lib.rs:3:14: 3:21} as std::clone::Clone>::clone at 5:16.

Approach

  • Each rt.Iter that backs a std iterator with Clone gets clone(h?: rt.Clones). The clone reads on from where the original was, independently. It follows std's derived or hand-written Clone (pinned std source). rt.Clones is { i?, o?, f?, v? }: clones of the inner iterators, the closure, and an item or other held value. The translator builds it per Rust type in iter::clones and leaves out parts that clone as themselves:
    • Borrowing iterators (slice, Chars, Bytes, ranges, deque, heap, B-tree, hash, set operations) share what they read and copy their position. A hash iterator steps a fresh JS iterator past what it took, through the same from(pick) its remaining() uses.
    • Owning ones (vec/array/option/deque/heap IntoIter, Once) clone each remaining item with v.
    • Adaptors clone their inner iterators with i/o and their closure with f. Peekable also clones its peeked item. FlatMap/Flatten clone the front and back inner iterators. FromFn/Successors clone their state.
    • A crate's own iterator, wrapped by into_iter, clones through its Clone.
  • Closures clone as Rust clones their captures. A closure that writes none of its captures, and whose captures each copy or clone as themselves, clones as itself. Any other closure is emitted as a factory over its captures, const make = (captured...) => rt.cloning(arrow, () => make(clone(captured)...)), so rt.closureClone(f) rebuilds the closure from cloned captures. Translator::settle reruns when it reaches a newly cloned closure, because the clone can lower before or after the closure is made.
  • rt.rangeClone/rangeInclusiveClone are now Range.clone()/RangeInclusive.clone().
  • remaining() stays. std's Debug of vec::IntoIter, IterMut, Drain, and the owning hash and B-tree iterators lists items through as_slice or a borrowing iter, not a clone, and most of those kinds have no Clone or would clone items. Only Chars reads through a clone, so its remaining() now returns this.clone().
  • A new clones::iters case in lang runs differentially against native wasm32. It covers: partial reads from both ends, then the original and the clone read in turns; size hints; zip past the shorter side, including a side-effecting map behind by_ref; Peekable with a peeked item and a peeked end; Chars and split/match_indices mid-text; B-tree and hash iterators (hash checked order-independently); set operations on both; deque and heap; byte-array lending iterators; and adaptors over owned items whose closures capture values that drop, logging clone and drop order.

Audit of std's iterators with Clone: every one the translator can make now clones. Ones it can't make yet (cycle, scan, char_indices, lines, and others) refuse when they are created. Any other kind that reaches clone still refuses at the call's span. Kinds without Clone (IterMut, Drain, owning hash and B-tree iterators) are rejected by rustc.

No iterator's next, nextBack, or fold changed, only new clone methods and constructor parameter types, so there is no benchmark run.

Impact

  • Translator output: clone of std's iterators, adaptors, and closures translates instead of being refused. A closure that is cloned and can't share itself is emitted as a make factory.
  • Runtime: adds rt.Iter.clone, rt.Clones, rt.cloning, and rt.closureClone. Removes rt.rangeClone and rt.rangeInclusiveClone, replaced by Range.clone/RangeInclusive.clone. rt.ChainIter, rt.FuseIter, rt.FlattenIter, and the B-tree set-operation constructors now take their parts already split.

Alternatives

  • Replacing remaining() with clones: this only works for Chars, as noted above, so it would not be simpler.
  • Cloning a closure by copying its JS captures in the runtime: JS closures don't expose their captures, so the translator builds the factory instead.

Follow-ups

Review

Codex (quest review), gpt-6.1-sol, medium effort, against origin/main 52a8900b:

  • Round 1 on b0d6c9c07c05b1d2b6f711bea081a578cf4e82bc: no findings.

just ci passed locally on b0d6c9c0.

Merge of main at f2288c08 into b0d6c9c0, as 4565ab08, then b5d0eba6 by the orchestrator: #320 made start.. an rt.RangeFrom iterator while this branch removed rt.rangeClone, so the runtime conflict keeps RangeFrom without rangeClone, RangeFrom gets its own clone(), iter::clones maps it beside Range and RangeInclusive, and clones::iters reads start.. of u8, of u64 past 2^53, and of char in turns with its clone. quest/m3/ts-iter-clone.md stays deleted, its start.. now covered. Recorded skip: the added mapping is one pattern beside its siblings and the case follows the existing ones; just ci passed on b5d0eba6.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 3 commits October 30, 2026 18:39
Quest-Claim: 36120e2558fbb4d9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/core/option.ts
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
@kixelated kixelated changed the title Cloning std's iterators feat(ts): clone std's iterators and adaptors Oct 31, 2026
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 02:36
@coderabbitai

coderabbitai Bot commented Oct 31, 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 16 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: 88c833aa-1537-49b5-a626-684cb3aa4616
Commits

Reviewing files that changed from the base of the PR and between 52a8900 and b0d6c9c.

Files selected for processing: 28
  • quest/m3/README.md
  • quest/m3/ts-iter-clone.md
  • quest/m3/ts-iter-cycle.md
  • ts/README.md
  • ts/fixtures/lang/expected/clones.Countdown.ts
  • ts/fixtures/lang/expected/clones.Logged.ts
  • ts/fixtures/lang/expected/clones.Plan.ts
  • ts/fixtures/lang/expected/clones.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/iters.ts
  • ts/fixtures/lang/expected/lib.ts
  • ts/fixtures/lang/expected/slices.ts
  • ts/fixtures/lang/expected/wrappers.ts
  • ts/fixtures/lang/src/clones.rs
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/lib.rs
  • ts/runtime/rt/collections.ts
  • ts/runtime/rt/iter.ts
  • ts/runtime/rt/str.ts
  • ts/runtime/rt/task.ts
  • ts/src/lower/collections.rs
  • ts/src/lower/iter.rs
  • ts/src/lower/library.rs
  • ts/src/lower/mir/mod.rs
  • ts/src/lower/mir/place.rs
  • ts/src/lower/mir/rvalue.rs
  • ts/src/lower/mod.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

# Conflicts:
#	quest/m3/ts-iter-clone.md
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/runtime/rt/iter.ts

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: b0d6c9c

No actionable bugs found in the changed iterator-cloning runtime, clone lowering, and closure-capture handling. The direction is coherent: explicit per-adaptor state copies and type-directed clone hooks preserve independent progress without eagerly consuming lazy iterators. The closure factory and fixed-point discovery add complexity, but address the distinct need to clone captured state rather than share a JavaScript function.

The new differential fixture meaningfully covers partial front/back consumption, peeked items, owned values with observable Clone/Drop behavior, and stateful/nested closures. Verification was source inspection only; I did not run the fixture, tests, or CI, and this review does not establish that they pass.

(Written by OpenAI)

#320 made start.. an rt.RangeFrom iterator while this branch removed
rt.rangeClone, so RangeFrom gets its own clone() and the iterator
clone mapping takes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated merged commit 2c6fbfa into main Oct 31, 2026
@kixelated
kixelated deleted the quest/m3/ts-iter-clone branch October 31, 2026 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant