Skip to content

Implement nth_back for Zip iterator#152652

Open
veeceey wants to merge 2 commits intorust-lang:mainfrom
veeceey:feat/issue-54054-zip-nth-back
Open

Implement nth_back for Zip iterator#152652
veeceey wants to merge 2 commits intorust-lang:mainfrom
veeceey:feat/issue-54054-zip-nth-back

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 15, 2026

Resolves the remaining Zip items from the tracking list in #54054.

The Zip iterator was missing a nth_back specialization, so calling .rev().step_by(n) (or any other pattern that ends up calling nth_back) on a zipped iterator would fall through to the default DoubleEndedIterator::nth_back, which just loops over next_back(). For TrustedRandomAccess iterators like slices, this turns what should be O(1) into O(n).

This adds nth_back at all three specialization levels in ZipImpl:

  • Default (zip_impl_general_defaults macro): delegates to a super_nth_back helper that calls next_back() in a loop, same as the trait default but properly routed through the Zip dispatch.
  • TrustedRandomAccessNoCoerce: inherits the default.
  • TrustedRandomAccess: O(1) via direct __iterator_get_unchecked indexing, with proper length-equalization on first backward call, side-effect execution for skipped elements (as required by the MAY_HAVE_SIDE_EFFECT contract), and panic-safe self.len updates before each unchecked access.

The implementation mirrors the existing nth specialization and next_back backward-trimming logic.

Test plan

  • Added test_zip_nth_back covering basic nth_back, exhaustion, and unequal-length iterators
  • Added test_zip_nth_back_after_next covering interleaved forward/backward iteration
  • Added test_zip_rev_nth_uses_nth_back covering the motivating use case (zip(..).rev().step_by(..))
  • All existing zip tests pass (including the side-effect and panic-safety tests)

The `Zip` iterator was missing a specialized `nth_back` implementation,
causing it to fall back to the default `DoubleEndedIterator::nth_back`
which calls `next_back()` in a loop. This is O(n) even for iterators
that support efficient random access.

This adds `nth_back` to all three `ZipImpl` specialization levels:

- Default: delegates to `super_nth_back` (calls `next_back` in a loop,
  same as the trait default but going through the Zip dispatch)
- TrustedRandomAccessNoCoerce: uses the default
- TrustedRandomAccess: uses direct index-based access with proper
  side-effect handling and panic safety

This completes the last three unchecked items from the tracking list
in rust-lang#54054 (Zip, Zip in ZipImpl default fn, Zip in ZipImpl where
TrustedRandomAccess).

The performance impact is most visible when using patterns like
`iter_a.zip(iter_b).rev().step_by(n)`, since `step_by` calls `nth`
on the underlying iterator, and `Rev::nth` delegates to `nth_back`.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 15, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 15, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, jhpratt, joboet, scottmcm

@rustbot
Copy link
Collaborator

rustbot commented Feb 15, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@rust-log-analyzer

This comment has been minimized.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants