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

Always implement ExactSizeIterator for std::iter::Take #50057

Closed
pitdicker opened this issue Apr 18, 2018 · 3 comments
Closed

Always implement ExactSizeIterator for std::iter::Take #50057

pitdicker opened this issue Apr 18, 2018 · 3 comments

Comments

@pitdicker
Copy link
Contributor

Currently std::iter::Take only implements ExactSizeIterator if its parent iterator implements that. But Take knows how many elements it will return at most, so I think it makes sense if it would always implement ExactSizeIterator.

It would not be correct if the parent iterator produces less items, but this trait is only a best effort to help optimizations, right?

@sfackler
Copy link
Member

It would not be correct if the parent iterator produces less items, but this trait is only a best effort to help optimizations, right?

No, it states that you know the exact size of the iterator. If you don't you shouldn't implement it.

@Enet4
Copy link
Contributor

Enet4 commented Apr 18, 2018

Take already takes advantage of the given upper bound in size_hint. However, implementing ExactSizeIterator requires the adaptor to know exactly which elements it contains, and not just an estimation.

@pitdicker
Copy link
Contributor Author

Both thanks for the replies, I now understand the role of ExactSizeIterator a bit better!

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

No branches or pull requests

3 participants