Skip to content

feat(ts): translate Iterator::cycle - #327

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

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

Conversation

@kixelated

@kixelated kixelated commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Iterator::cycle was refused (unsupported call to <std::slice::Iter<'_, u8> as std::iter::Iterator>::cycle at the reject fixture's cycled, fixtures/reject/src/lib.rs:124:11). #322 gave each rt.Iter a clone(h), which is everything Cycle needs.

Approach

  • rt.iterCycle(it, h) builds a CycleIter that follows std's Cycle at the pinned nightly-2026-10-15 (core/src/iter/adapters/cycle.rs):
    • new (:22): orig is it.clone(h), made when cycle() is called, and iter is it.
    • next (:35): reads iter. When that ends, orig is cloned and only then is the old iter dropped, as self.iter = self.orig.clone() does.
    • size_hint (:46): (0, Some(0)) passes through, any other (0, _) gives (0, None), and everything else gives (usize::MAX, None).
    • try_fold (:56), modeled as folded(): reads the rest of iter, then one fresh clone, stopping if that clone was empty, then keeps cloning with no further empty check, exactly as std does. fold is std's default, by next (folded(true) returns the cycle itself).
    • advance_by (:87): advances iter, then fresh clones, and stops at a clone that advances no further. nth stays the default (advance_by then next).
    • Clone (derived): clones orig and then iter. Drop: orig and then iter, in field order.
  • The translator maps cycle with the inner iterator's rt.Clones. clones now covers Cycle, and also iter::Empty, which it lacked (feat(ts): clone std's iterators and adaptors #322 cloned that one only through the zero-sized path).
  • std's Take::count advances its iterator by advance_by (take.rs:60), not try_fold. For a Cycle these differ: advance_by stops at an empty clone, while try_fold reads past it. So a count of an iterator that holds a Cycle now goes through rt.ownedCount, which already advances a Take.
  • Removed the reject fixture's cycled root and cycle from the README's refusal list. Other unsupported call entries still cover that refusal.

Tests: a new cycles::cycles differential case, against native wasm32, covering:

  • empty originals, including an exhausted one, an iter::empty, an empty Vec<Logged>, and a filter that rejects every item;
  • originals partly read before cycle;
  • take(n) across several wraps, through count, fold, any, collect, zip, and skip/step_by;
  • nth across wraps, with owned Logged items cloned and dropped;
  • find and position exiting early;
  • cycle().clone(), including of owned items mid-pass, and a cycle of a cycle;
  • adaptors whose closures capture a value that logs when cloned and when called, which shows when each clone is made and dropped;
  • a closure that counts its calls;
  • an inclusive range;
  • the crate's own iterator;
  • a pausing source whose clones share state, where next, Take::count, and a fold each behave differently.

I checked the test with mutations of the translated runtime. Each of these makes the differential fail: try_fold by next, dropping before cloning, a wrong size hint, the clone order, the drop order, no advance_by override, and iterCount in place of ownedCount.

Impact

  • Translator output: cycle translates to rt.iterCycle, and count of an iterator holding a Cycle emits rt.ownedCount.
  • Runtime: new export iterCycle. No breaking change.

Alternatives

  • Route every count of a Take through ownedCount. I didn't: only Cycle makes Take::count differ from a fold, and the narrower check keeps the owned dispatch out of bundles that don't need it.

Follow-ups

None.

Review

Codex (quest review), gpt-6.1-sol, medium effort:

  • Round 1 on 296939e: no findings.
  • Round 2 on a9e572f (merge of origin/main): no findings.
  • Round 3 on 1bf0ace (merge of origin/main 7eb1474, snapshots regenerated): no findings. This is the reviewed head.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 5 commits October 30, 2026 20:38
Quest-Claim: ec5ea6db4c23475c
rt.iterCycle holds the iterator as std's Cycle does, with a clone of it made
by the rt.Clones the translation gives, and clones that again each time the
one it reads ends. Its next, size_hint, try_fold, and advance_by follow std's
at the pinned toolchain, and a count of one that holds a Cycle goes through
rt.ownedCount, which advances a Take as std's Take::count does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
@kixelated kixelated changed the title Cycling std's iterators feat(ts): translate Iterator::cycle Oct 31, 2026
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 04:41
@kixelated
kixelated merged commit 5813260 into main Oct 31, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-iter-cycle branch October 31, 2026 04:42
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