mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-02 17:44:28 +00:00
iter: extend step_by specialization to cover StepBy<RangeIter<{integer}>>
`StepBy<Range<{integer}>>` already has a fast path that converts the range
into a yield-count before iteration, avoiding per-step overflow checks and
enabling better loop structure for the optimizer. `StepBy<RangeIter<{integer}>>`
did not benefit from the same optimization even though `RangeIter<T>` is a thin
newtype wrapper around `legacy::Range<T>`.
## Changes
- `library/core/src/range/iter.rs`: make `RangeIter<A>.0` `pub(crate)` so the
step_by adapter can access the inner legacy range's start/end fields.
- `library/core/src/iter/adapters/step_by.rs`: add `spec_int_ranges_new!` and
`spec_int_ranges_new_r!` macros that mirror the existing `spec_int_ranges!` /
`spec_int_ranges_r!` implementations. Forward specialization covers `u8 u16
u32 u64 usize` (pointer-width-gated for u32/u64); backward specialization is
limited to `u8 u16 usize` since `RangeIter<u32>` / `RangeIter<u64>` do not
implement `ExactSizeIterator`.
- `library/coretests/tests/iter/adapters/step_by.rs`: add
`test_step_by_new_range_iter` covering forward, backward, and interleaved
iteration via the new-range iterator.
## Motivation
Fixes rust-lang/rust#157754. The inner loop for `core::range::Range::from(0..n).into_iter().step_by(2)` currently generates noticeably worse assembly than the equivalent `(0..n).step_by(2)` because specialization did not cover `RangeIter`.
r? libs