take_while_inclusive: tighten FusedIterator to require I: FusedIterator#1101
Open
c-tonneslan wants to merge 1 commit into
Open
take_while_inclusive: tighten FusedIterator to require I: FusedIterator#1101c-tonneslan wants to merge 1 commit into
c-tonneslan wants to merge 1 commit into
Conversation
The current impl marks TakeWhileInclusive as FusedIterator regardless of the underlying iterator, so wrapping a non-fused iterator and then calling .fuse() is unsound: TakeWhileInclusive forwards next() to the inner iterator after it returns None once, and the .fuse() layer takes the FusedIterator marker at face value and short-circuits to None. Calling next() a second time can then yield Some after the first None, breaking the FusedIterator contract. Mirror std::iter::TakeWhile and only implement FusedIterator when the inner iterator is fused. Closes rust-itertools#1088 Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.
Closes #1088.
TakeWhileInclusivecurrently unconditionally implementsFusedIterator, but itsnext()keeps calling the inner iterator after that inner iterator returnedNone(it only setsdonewhen the predicate fails, not when the source runs dry). So if you wrap a non-fused iterator and then.fuse()the result,Fusetrusts the marker, short-circuits toNoneon its first observedNone, and the underlying iterator's subsequentSomeis dropped. The other direction is just as broken: without.fuse(), the marker is a lie sinceSomecan followNone.std::iter::TakeWhilealready does the right thing here, only implementingFusedIteratorwhen the underlying iterator is fused. Doing the same forTakeWhileInclusivekeeps the marker honest without changing the runtime behavior for any caller whose source iterator was already fused.