Skip to content

Commit

Permalink
Don't call size_hint of underlying iterator needlessly
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkuu committed Dec 8, 2018
1 parent e704ce9 commit 5728a04
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2109,8 +2109,12 @@ impl<I: Iterator, P> Iterator for TakeWhile<I, P>

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
if self.flag {
(0, Some(0))
} else {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}
}

#[inline]
Expand Down Expand Up @@ -2321,6 +2325,10 @@ impl<I> Iterator for Take<I> where I: Iterator{

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.n == 0 {
return (0, Some(0));
}

let (lower, upper) = self.iter.size_hint();

let lower = cmp::min(lower, self.n);
Expand Down

0 comments on commit 5728a04

Please sign in to comment.