Skip to content

Commit 5728a04

Browse files
committed
Don't call size_hint of underlying iterator needlessly
1 parent e704ce9 commit 5728a04

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/libcore/iter/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,8 +2109,12 @@ impl<I: Iterator, P> Iterator for TakeWhile<I, P>
21092109

21102110
#[inline]
21112111
fn size_hint(&self) -> (usize, Option<usize>) {
2112-
let (_, upper) = self.iter.size_hint();
2113-
(0, upper) // can't know a lower bound, due to the predicate
2112+
if self.flag {
2113+
(0, Some(0))
2114+
} else {
2115+
let (_, upper) = self.iter.size_hint();
2116+
(0, upper) // can't know a lower bound, due to the predicate
2117+
}
21142118
}
21152119

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

23222326
#[inline]
23232327
fn size_hint(&self) -> (usize, Option<usize>) {
2328+
if self.n == 0 {
2329+
return (0, Some(0));
2330+
}
2331+
23242332
let (lower, upper) = self.iter.size_hint();
23252333

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

0 commit comments

Comments
 (0)