]> git.lizzy.rs Git - rust.git/commitdiff
Don't call size_hint of underlying iterator needlessly
authorShotaro Yamada <sinkuu@sinkuu.xyz>
Sat, 8 Dec 2018 11:16:36 +0000 (20:16 +0900)
committerShotaro Yamada <sinkuu@sinkuu.xyz>
Sat, 8 Dec 2018 15:01:09 +0000 (00:01 +0900)
src/libcore/iter/mod.rs

index bc5bbc2217cd3fd63d4652b14a6f905ba5866a9c..7b273f7862a1f14f730437311ee1714ce1211ea7 100644 (file)
@@ -2109,8 +2109,12 @@ fn next(&mut self) -> Option<I::Item> {
 
     #[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 @@ fn nth(&mut self, n: usize) -> Option<I::Item> {
 
     #[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);