Change core::iter::Fuse
's Default
impl to do what its docs say it does
#140985
+1
−1
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 docs on
impl<I: Default> Default for core::iter::Fuse<I>
say (as theI: Default
bound implies) thatFuse::<I>::default
"Creates aFuse
iterator from the default value ofI
". However, the implementation creates aFuse
withFuse { iter: Default::default() }
, and since theiter
field is anOption<I>
, this is actuallyFuse { iter: None }
, notFuse { iter: Some(I::default()) }
, soFuse::<I>::default()
always returns an empty iterator, even ifI::default()
would not be empty.This PR changes
Fuse
'sDefault
implementation to match the documentation. This will be a behavior change for anyone currently usingFuse::<I>::default()
whereI::default()
is not an empty iterator1, asFuse::<I>::default()
will now also not be an empty iterator.(Alternately, the docs could be updated to reflect what the current implementation actually does, i.e. returns an always-exhausted iterator that never yields any items (even if
I::default()
would have yielded items). With this option, theI: Default
bound could also be removed to reflect that noI
is ever created.)Current behavior example (maybe an example like this should be added to the docs either way?)
This PR changes publicly observable behavior, so I think requires at least a T-libs-api FCP?
r? libs-api
cc #140961
impl<I: Default> Default for Fuse<I>
was added in 1.70.0 (#99929), and it's docs and behavior do not appear to have changed since (Fuse
'siter
field has been anOption
since before the impl was added).Footnotes
IIUC it is a "de facto" guideline for the stdlib that an iterator type's
default()
should be empty (and for iterators where that would not make sense, they should not implementDefault
): cc https://github.com/rust-lang/libs-team/issues/77#issuecomment-1194681709 , so for stdlib iterators, I don't think this would change anything. However, if a user has a customIterator
typeI
, and they are usingFuse<I>
, and they callFuse::<I>::default()
, this may change the behavior of their code. ↩