about summary refs log tree commit diff
diff options
context:
space:
mode:
authorShotaro Yamada <sinkuu@sinkuu.xyz>2018-12-08 20:16:36 +0900
committerShotaro Yamada <sinkuu@sinkuu.xyz>2018-12-09 00:01:09 +0900
commit5728a043e8280e96cbae784a731d43f2c7a50137 (patch)
tree42d8631232cf67f7823e6f08148c8432f0d7f0f6
parente704ce9e8af1713eb938a9acd7f27bf96f88664e (diff)
downloadrust-5728a043e8280e96cbae784a731d43f2c7a50137.tar.gz
rust-5728a043e8280e96cbae784a731d43f2c7a50137.zip
Don't call size_hint of underlying iterator needlessly
-rw-r--r--src/libcore/iter/mod.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs
index bc5bbc2217c..7b273f7862a 100644
--- a/src/libcore/iter/mod.rs
+++ b/src/libcore/iter/mod.rs
@@ -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]
@@ -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);