Skip to content

fix(ts): skip through each iterator's own nth, as std's does - #140

Merged
kixelated merged 3 commits into
mainfrom
quest/m2/ts-iter-nth
Oct 28, 2026
Merged

kixelated merged 3 commits into
mainfrom
quest/m2/ts-iter-nth

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Without a dropper, rust2-ts's nth and nth_back used per-class nthItem/nthBackItem methods that only Enumerate, Rev, Take, Range, and by_ref overrode. std's Fuse, Skip, Chain, StepBy, Peekable, Flatten, and Copied forward to what they wrap, so a by_ref source or an inspect closure saw different reads. For example, (0..3).by_ref().take(0).fuse().nth_back(0) leaves the range empty in Rust but not here. The rt.owned* dispatchers from #101 already followed std, but only when an item dropped.

Fixing this turned up three more mismatches with std:

  • std's Fuse has no advance_by or advance_back_by of its own, but the dispatchers forwarded them. So once(x).chain(src.take(0).fuse()).nth_back(1) read past the take's limit.
  • cloned() of JS values was the source itself, which forwarded nth. std's Cloned reads by next.
  • copied() of a struct or byte array was a plain map, which reads by next. std's Copied forwards nth and advance_by.

Approach

nth, nth_back, advance_by, and advance_back_by are now methods on rt.Iter, which carry std's defaults. Each class that std overrides them for overrides them too, with the bodies of #101's dispatchers moved in. A single implementation serves both cases, with or without a dropper.

  • Skip, Take, and StepBy take an optional dropper, and the OwnedSkip/OwnedTake/OwnedStepBy subclasses are gone.
  • Fuse keeps the default advance_by, as std's does.
  • New rt.iterCopied makes a CopiedIter that forwards nth and advance_by. New rt.iterCloned reads by next.
  • Skip::advance_by now follows std's saturating split exactly.
  • count and last keep their owned* dispatchers, which run only where something drops. Their reads already matched std.

Tests: new iters::skips lang differential cases. Each runs nth or nth_back through every adaptor above, over a take of a by_ref source and of an inspected one, and records what was read. Before the fix it diverged from native, and with Fuse's old forwarding put back the chain(.. .fuse()) case fails again.

Impact

  • Translator output: rt.ownedNth, ownedNthBack, ownedSkip, ownedTake, and ownedStepBy become rt.iterNth, iterNthBack, iterSkip, iterTake, and iterStepBy, with the dropper as an optional last argument. .nthBackItem(n) becomes rt.iterNthBack(it, n). copied/cloned may emit rt.iterCopied/rt.iterCloned.
  • Runtime API (breaking, in MIGRATE.md): the owned* skip exports are removed, and rt.Iter's nthItem/nthBackItem are renamed to nth/nthBack. Iter also gains advanceBy and advanceBackBy.
  • Consumer: moq, whose translated code calls these through std's iterators.

Alternatives

  • Call feat(ts): drop what std's iterators own #101's dispatchers without a dropper. This was the first version, commit b380ed5. It was correct, but every step_by item went through a long instanceof chain. Measured in Node: step_by(2) over a map of a slice took 40 ns/item, against 7 ns/item on main. With methods it is back to parity in Node and Bun (6 to 10 ns/item, within noise). Since speed ranks above bundle size, methods won.
  • Bundle-size cost of methods: a class's skip logic now ships wherever that class does, even if nothing skips. The dispatchers avoided that. The codec shake test is unchanged.
  • copied() of JS values stays the source itself. It forwards nth_back where std's Copied uses advance_back_by followed by next_back. For every std adaptor, these read the same items.

Decisions made without asking

  • Fixed Cloned, Fuse's advance_by, and Skip's saturation here, since they are the same problem: skips that read differently from std's.
  • Merged the owned/iter names for nth, nth_back, skip, take, and step_by, because each now has a single implementation.

Follow-ups

  • No just ts bench run. It needs the bench shell and browsers. The per-item check above used Bun and Node only.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 3 commits October 27, 2026 17:13
Quest-Claim: 59cbd1e2fd07e33d
nth, nth_back, and advance_by are methods with std's defaults on rt.Iter,
overridden where std's adaptors override them, so a by_ref source or an
inspect closure sees std's reads with or without a dropper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Iterator skips that read as std's fix(ts): skip through each iterator's own nth, as std's does Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 02:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T02:04:56.434640Z 52bb36a Draft marked ready
🔒 Security Review ✅ Completed 2026-10-28T02:03:51.713761Z 52bb36a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 52bb36a

No actionable bugs found in the changed iterator dispatch, optional dropper plumbing, or copied/cloned lowering. Moving skip semantics onto each iterator removes the duplicated owned/non-owned paths; keeping Fuse's default advance behavior and separating Copied from Cloned are important correctness details. The added by_ref/inspect differential cases exercise observable reads, not just returned values.

Overall direction is sound. Per-class methods are a reasonable alternative to the long dispatcher, with the documented bundle-size tradeoff. Verification was static inspection of the runtime, lowering, migration notes, and fixtures; I did not run builds, differential tests, or benchmarks, and have not independently verified the reported performance measurements.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merging: Codex reviewed head 52bb36a with no findings, CI passes, and quest check --base origin/main is clean (the quest file and its link in quest/m2/README.md are removed). The automated review from the maintainer's account found nothing either but is not counted. No open decisions; the PR body's "Decisions made without asking" stand. The skipped just ts bench run stays a suggested follow-up.

(written by Claude Opus 5.5)

@kixelated
kixelated merged commit 1be3a39 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-iter-nth branch October 28, 2026 02:26
kixelated added a commit that referenced this pull request Oct 28, 2026
…ted-option

Resolve against #140: `nth_back` now maps to `rt.iterNthBack`, which takes the
`rt.None` of an item that can be `undefined` as its last argument, and the
`lang` harness has a case for each of `nested`, `refs`, and `skips`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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