Don't call size_hint of underlying iterator needlessly
This commit is contained in:
parent
e704ce9e8a
commit
5728a043e8
|
@ -2109,9 +2109,13 @@ impl<I: Iterator, P> Iterator for TakeWhile<I, P>
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
if self.flag {
|
||||||
|
(0, Some(0))
|
||||||
|
} else {
|
||||||
let (_, upper) = self.iter.size_hint();
|
let (_, upper) = self.iter.size_hint();
|
||||||
(0, upper) // can't know a lower bound, due to the predicate
|
(0, upper) // can't know a lower bound, due to the predicate
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
|
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
|
||||||
|
@ -2321,6 +2325,10 @@ impl<I> Iterator for Take<I> where I: Iterator{
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
if self.n == 0 {
|
||||||
|
return (0, Some(0));
|
||||||
|
}
|
||||||
|
|
||||||
let (lower, upper) = self.iter.size_hint();
|
let (lower, upper) = self.iter.size_hint();
|
||||||
|
|
||||||
let lower = cmp::min(lower, self.n);
|
let lower = cmp::min(lower, self.n);
|
||||||
|
|
Loading…
Reference in New Issue