Skip to content

Commit

Permalink
Fix FlatMap::size_hint (thanks @bluss)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Oct 18, 2017
1 parent dfdf89c commit e3784ab
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,15 @@ impl<'a, R:?Sized+'a, U, F> Iterator for FlatMap<'a, R, U, F>
}

fn size_hint(&self) -> (usize, Option<usize>) {
// See impl for Map above
self.len.map_or((usize::MAX, None), |len| (len, Some(len)))
if self.len == Some(0) {
// No new iters, so we have frontiter or nothing
self.frontiter.as_ref().map_or((0, Some(0)), |it| it.size_hint())
} else {
// Can't compute an actual bound without producing the sub-iters,
// which we don't want to do. But we may have a lower bound.
let lb = self.frontiter.as_ref().map_or(0, |it| it.size_hint().0);
(lb, None)
}
}
}

Expand Down

2 comments on commit e3784ab

@bluss
Copy link

@bluss bluss commented on e3784ab Oct 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was pinged. The sub iters may be empty, so the lower bound is still zero for the second case unfortunately.

@dhardy
Copy link
Owner Author

@dhardy dhardy commented on e3784ab Oct 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. frontiter is the only available sub-iter; if it gives us a lower-bound, we know at least this many items are available (next doesn't even evaluate self.len until self.frontiter is exhausted).

Please sign in to comment.