Skip to content

Add Step::forward/backward_overflowing to enable RangeInclusive loop optimizations#155114

Open
pitaj wants to merge 2 commits intorust-lang:mainfrom
pitaj:rangeinclusiveiter-optimize
Open

Add Step::forward/backward_overflowing to enable RangeInclusive loop optimizations#155114
pitaj wants to merge 2 commits intorust-lang:mainfrom
pitaj:rangeinclusiveiter-optimize

Conversation

@pitaj
Copy link
Copy Markdown
Contributor

@pitaj pitaj commented Apr 10, 2026

ACP: rust-lang/libs-team#767

This adds new required methods to the Step trait:

trait Step: ... {
    // ... existing functions

    // New required functions
    fn forward_overflowing(start: Self, count: usize) -> (Self, bool);
    fn backward_overflowing(start: Self, count: usize) -> (Self, bool);
}

It was found that using these to implement RangeInclusive's Iterator impl enabled optimizations previously only applicable to the exclusive-ended Range.

This required changing how "exhaustion" works for RangeInclusive. I've nominated this for libs-api discussion because of one insta-stable change:

PartialEq impl now always returns false if either RangeInclusive is exhausted
I changed this because the new implementations now only set exhausted when overflow occurs, and start is now advanced past end otherwise. I doubt anyone depends on the prior behavior, but it's probably worth a crater run.

The exhaustion changes also affect Debug but my understanding is that debug formatting is never guaranteed stable.

Note: I did not change the nth impls to use the new functions yet. I want buyoff on these changes first. If the changes to legacy ops::RangeInclusive are rejected, I will change this PR to target the new range::RangeInclusive instead.

r? libs

but RangeInclusive loops do not
@pitaj pitaj added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Apr 10, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 10, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

and optimize the RangeInclusive iterator implementation with them

This changes the `exhausted` field to represent an overflow flag
for the bounds, essentially acting as an extra bit for `Idx`.
This was found to enable optimizations previously only applicable
to the exclusive-ended Range type.
@pitaj pitaj force-pushed the rangeinclusiveiter-optimize branch from d0c1f8d to 39e74a3 Compare April 10, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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