Repository navigation
feat(ts): clone std's iterators and adaptors - #322
Conversation
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
|
Warning Review limit reachedYou'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. View limit details
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. |
# 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
left a comment
There was a problem hiding this comment.
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>
Closes quest/m3/ts-iter-clone.md.
Problem
Clone::cloneof std's iterators and adaptors refused, for exampleunsupported call to <std::iter::Zip<...> as std::clone::Clone>::cloneatsrc/lib.rs:20:13. Reproduced on a scratch crate: sliceIter,Map,Zip,Chars,Peekable, B-tree mapIter/Keys/Values/Range,btree_set::Iter,hash_set::Iter, B-treeUnion/Intersection/Difference, andFilterall 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>::cloneat5:16.Approach
rt.Iterthat backs a std iterator withClonegetsclone(h?: rt.Clones). The clone reads on from where the original was, independently. It follows std's derived or hand-writtenClone(pinned std source).rt.Clonesis{ 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 initer::clonesand leaves out parts that clone as themselves: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 samefrom(pick)itsremaining()uses.vec/array/option/deque/heapIntoIter,Once) clone each remaining item withv.i/oand their closure withf.Peekablealso clones its peeked item.FlatMap/Flattenclone the front and back inner iterators.FromFn/Successorsclone their state.into_iter, clones through itsClone.const make = (captured...) => rt.cloning(arrow, () => make(clone(captured)...)), sort.closureClone(f)rebuilds the closure from cloned captures.Translator::settlereruns when it reaches a newly cloned closure, because the clone can lower before or after the closure is made.rt.rangeClone/rangeInclusiveCloneare nowRange.clone()/RangeInclusive.clone().remaining()stays. std'sDebugofvec::IntoIter,IterMut,Drain, and the owning hash and B-tree iterators lists items throughas_sliceor a borrowing iter, not a clone, and most of those kinds have noCloneor would clone items. OnlyCharsreads through a clone, so itsremaining()now returnsthis.clone().clones::iterscase inlangruns 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 behindby_ref;Peekablewith a peeked item and a peeked end;Charsandsplit/match_indicesmid-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 reachesclonestill refuses at the call's span. Kinds withoutClone(IterMut,Drain, owning hash and B-tree iterators) are rejected by rustc.No iterator's
next,nextBack, or fold changed, only newclonemethods and constructor parameter types, so there is no benchmark run.Impact
cloneof std's iterators, adaptors, and closures translates instead of being refused. A closure that is cloned and can't share itself is emitted as amakefactory.rt.Iter.clone,rt.Clones,rt.cloning, andrt.closureClone. Removesrt.rangeCloneandrt.rangeInclusiveClone, replaced byRange.clone/RangeInclusive.clone.rt.ChainIter,rt.FuseIter,rt.FlattenIter, and the B-tree set-operation constructors now take their parts already split.Alternatives
remaining()with clones: this only works forChars, as noted above, so it would not be simpler.Follow-ups
Iterator::cycle, which this PR'sclone(h)makes possible.Review
Codex (
quest review), gpt-6.1-sol, medium effort, againstorigin/main52a8900b:b0d6c9c07c05b1d2b6f711bea081a578cf4e82bc: no findings.just cipassed locally onb0d6c9c0.Merge of main at
f2288c08intob0d6c9c0, as4565ab08, thenb5d0eba6by the orchestrator: #320 madestart..anrt.RangeFromiterator while this branch removedrt.rangeClone, so the runtime conflict keepsRangeFromwithoutrangeClone,RangeFromgets its ownclone(),iter::clonesmaps it besideRangeandRangeInclusive, andclones::itersreadsstart..ofu8, ofu64past 2^53, and ofcharin turns with its clone.quest/m3/ts-iter-clone.mdstays deleted, itsstart..now covered. Recorded skip: the added mapping is one pattern beside its siblings and the case follows the existing ones;just cipassed onb5d0eba6.(written by Claude Opus 5.5)
🤖 Generated with Claude Code