Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix std::ops::Range size_hint and ExactSizeIterator impls #22065

Merged
merged 1 commit into from
Feb 10, 2015

Conversation

bluss
Copy link
Member

@bluss bluss commented Feb 7, 2015

When self.start > self.end, these iterators simply return None,
so we adjust the size_hint to just return zero in this case.

Certain optimizations can be implemented in and outside libstd if we
know we can trust the size_hint for all inputs to for example
Range.

This corrects the ExactSizeIterator implementations, which IMO were
unsound and incorrect previously, since they allowed a range like (2..1)
to return a size_hint of -1us in when debug assertions are turned off.

When self.start > self.end, these iterators simply return None,
so we adjust the size_hint to just return zero in this case.

Certain optimizations can be implemented in and outside libstd if we
know we can trust the size_hint for all inputs to for example
Range<usize>.

This corrects the ExactSizeIterator implementations, which IMO were
unsound and incorrect previously, since they allowed a range like (2..1)
to return a size_hint of -1us in when debug assertions are turned off.
@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@bluss
Copy link
Member Author

bluss commented Feb 7, 2015

Having correct .size_hint implementations is important. See ZipTrusted in itertools which implements fast and safe lockstep iteration for certain trusted iterators. ZipTrusted's iteration matches the best case of iterating two or more slices based on a precomputed common lower bound (benchmark numbers). It is a prototype for something that could be added to libstd later.

@Gankra
Copy link
Contributor

Gankra commented Feb 7, 2015

@bors r+ 4f61e rollup

@Gankra
Copy link
Contributor

Gankra commented Feb 7, 2015

Thanks!

@bluss
Copy link
Member Author

bluss commented Feb 7, 2015

Thanks for the review!

kmcallister added a commit to kmcallister/rust that referenced this pull request Feb 8, 2015
When self.start > self.end, these iterators simply return None,
so we adjust the size_hint to just return zero in this case.

Certain optimizations can be implemented in and outside libstd if we
know we can trust the size_hint for all inputs to for example
Range<usize>.

This corrects the ExactSizeIterator implementations, which IMO were
unsound and incorrect previously, since they allowed a range like (2..1)
to return a size_hint of -1us in when debug assertions are turned off.
@bors
Copy link
Collaborator

bors commented Feb 9, 2015

⌛ Testing commit 4f61e16 with merge 45c3731...

@bors
Copy link
Collaborator

bors commented Feb 9, 2015

💔 Test failed - auto-win-64-nopt-t

@alexcrichton
Copy link
Member

@bors: retry

Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 10, 2015
When self.start > self.end, these iterators simply return None,
so we adjust the size_hint to just return zero in this case.

Certain optimizations can be implemented in and outside libstd if we
know we can trust the size_hint for all inputs to for example
Range<usize>.

This corrects the ExactSizeIterator implementations, which IMO were
unsound and incorrect previously, since they allowed a range like (2..1)
to return a size_hint of -1us in when debug assertions are turned off.
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Feb 10, 2015
When self.start > self.end, these iterators simply return None,
so we adjust the size_hint to just return zero in this case.

Certain optimizations can be implemented in and outside libstd if we
know we can trust the size_hint for all inputs to for example
Range<usize>.

This corrects the ExactSizeIterator implementations, which IMO were
unsound and incorrect previously, since they allowed a range like (2..1)
to return a size_hint of -1us in when debug assertions are turned off.
@bors bors merged commit 4f61e16 into rust-lang:master Feb 10, 2015
@bluss bluss deleted the range-size-hint branch February 10, 2015 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants