-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-iteratorsArea: IteratorsArea: IteratorsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.
Description
If the wrapped iterator implements ExactSizeIterator
then Fuse
blindly delegates len()
to it. The underlying iterator may know its exact size but return spurious None
results anyway, on which Fuse
will stop iterating even though the source knows that there are elements left to iterate, meaning Fuse
will indicate that there are elements left but never return them.
The behavior was introduced in #37944 but didn't see any discussion.
struct Alternate {
state: i32,
}
impl Iterator for Alternate {
type Item = i32;
fn next(&mut self) -> Option<i32> {
let val = self.state;
self.state = self.state + 1;
if val % 2 == 0 && val <= 4 {
Some(val)
} else {
None
}
}
}
impl ExactSizeIterator for Alternate {
// We can easily calculate the remaining number of iterations.
fn len(&self) -> usize {
std::cmp::max(0, 3 - (self.state + 1) / 2) as usize
}
}
fn main() {
eprintln!("### regular");
let mut iter = Alternate { state: 0 };
dbg!(iter.len(), iter.next());
dbg!(iter.len(), iter.next());
dbg!(iter.len(), iter.next());
dbg!(iter.len(), iter.next());
dbg!(iter.len(), iter.next());
dbg!(iter.len(), iter.next());
eprintln!("### fused");
let mut iter = Alternate { state: 0 }.fuse();
dbg!(iter.len(), iter.next());
dbg!(iter.len(), iter.next());
dbg!(iter.len(), iter.next());
}
Metadata
Metadata
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-iteratorsArea: IteratorsArea: IteratorsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.