[new RangeInclusive] Convert to half-open in into_iter if possible#159200
Open
scottmcm wants to merge 1 commit into
Open
[new RangeInclusive] Convert to half-open in into_iter if possible#159200scottmcm wants to merge 1 commit into
into_iter if possible#159200scottmcm wants to merge 1 commit into
Conversation
78f5932 to
a058e3b
Compare
This comment has been minimized.
This comment has been minimized.
a058e3b to
7e9ef25
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The core goal here is to attempt to remove the penalty between
and
by changing how
range::RangeInclusiveIterworks.In
legacy::RangeInclusive, there's no opportunity to run a fixup before the loop, and trying in every iteration of the loop wasn't worth it.But now that we have the
range::RangeInclusivevsrange::RangeInclusiveItersplit, we can!The approach here is to, in
into_iter, attempt to convert fromstart..=lasttostart..(last+1). Regardless whether that worked, we always delegate to a normalRangefor iteration, just with a cold check for "actually we need to return one more element".Basically you can think of this as having
start..=lastdo either(start..(last+1)).chain(None)or(start..last).chain(Some(last)), though stored more efficiently than literally usingChain.That way, for example, for a slice of non-ZST if you run a
for i in 0..=slice.len()loop, it runs exactly the same as if you'd writtenfor i in 0..(slice.len() + 1)(while also still working for slize-of-ZST whereslice.len() + 1might overflow).r? libs