-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Chain has an incorrect implementation of DoubleEndedIterator #26316
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
Comments
|
Specifically for any Iterator, you can call Nothing else is guaranteed. Chain is incorrectly implemented as:
It should use the same strategy as |
Just using Of course, putting the flags inside the chain struct itself is an optimization (packs the members better). |
This is true (spoil-sport! :P) |
The iterator protocol specifies that the iteration ends with the return value `None` from `.next()` (or `.next_back()`) and it is unspecified what further calls return. The chain adaptor must account for this in its DoubleEndedIterator implementation. It uses three states: - Both `a` and `b` are valid - Only the Front iterator (`a`) is valid - Only the Back iterator (`b`) is valid The fourth state (neither iterator is valid) only occurs after Chain has returned None once, so we don't need to store this state. Fixes rust-lang#26316
Correct iterator adaptor Chain The iterator protocol specifies that the iteration ends with the return value `None` from `.next()` (or `.next_back()`) and it is unspecified what further calls return. The chain adaptor must account for this in its DoubleEndedIterator implementation. It uses three states: - Both `a` and `b` are valid - Only the Front iterator (`a`) is valid - Only the Back iterator (`b`) is valid The fourth state (neither iterator is valid) only occurs after Chain has returned None once, so we don't need to store this state. Fixes #26316
Testcase:
gives:
I'm pretty sure CrazyIterator isn't a legitimate implementation of DoubleEndedIterator, but the documentation doesn't say that explicitly, and it explicitly states that this is a legitimate implementation of Iterator. (If this is supposed to be allowed, std::iter::Chain has a bug.)
The text was updated successfully, but these errors were encountered: