]> git.lizzy.rs Git - rust.git/commitdiff
Change signature of Iterator.size_hint
authorKevin Ballard <kevin@sb.org>
Wed, 3 Jul 2013 01:40:46 +0000 (18:40 -0700)
committerKevin Ballard <kevin@sb.org>
Fri, 5 Jul 2013 08:56:48 +0000 (01:56 -0700)
Remove the Option wrapper around the lower bound. None is semantically
the same as Size(0), so there's no point in having a distinction.

src/librustc/util/enum_set.rs
src/libstd/iterator.rs
src/libstd/vec.rs

index f9bd7a3508edbe09b52665d7ab9c9ff28a66b4e3..3ce645e012b791373349a717afd10f00d88bfe96 100644 (file)
@@ -125,9 +125,9 @@ fn next(&mut self) -> Option<E> {
         Some(elem)
     }
 
-    fn size_hint(&self) -> (Option<uint>, Option<uint>) {
-        let exact = Some(self.bits.population_count());
-        (exact, exact)
+    fn size_hint(&self) -> (uint, Option<uint>) {
+        let exact = self.bits.population_count();
+        (exact, Some(exact))
     }
 }
 
index 77befbf19aa92f9afb5fe3dc5782bb5f37b6d60a..46d449e4dfb862e4d2bf228a68ceb2bb5700d86a 100644 (file)
@@ -43,7 +43,7 @@ pub trait Iterator<A> {
     /// Return a lower bound and upper bound on the remaining length of the iterator.
     ///
     /// The common use case for the estimate is pre-allocating space to store the results.
-    fn size_hint(&self) -> (Option<uint>, Option<uint>) { (None, None) }
+    fn size_hint(&self) -> (uint, Option<uint>) { (0, None) }
 }
 
 /// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -684,16 +684,11 @@ fn next(&mut self) -> Option<A> {
     }
 
     #[inline]
-    fn size_hint(&self) -> (Option<uint>, Option<uint>) {
+    fn size_hint(&self) -> (uint, Option<uint>) {
         let (a_lower, a_upper) = self.a.size_hint();
         let (b_lower, b_upper) = self.b.size_hint();
 
-        let lower = match (a_lower, b_lower) {
-            (Some(x), Some(y)) => Some(x + y),
-            (Some(x), None) => Some(x),
-            (None, Some(y)) => Some(y),
-            (None, None) => None
-        };
+        let lower = a_lower + b_lower;
 
         let upper = match (a_upper, b_upper) {
             (Some(x), Some(y)) => Some(x + y),
@@ -737,7 +732,7 @@ fn next(&mut self) -> Option<B> {
     }
 
     #[inline]
-    fn size_hint(&self) -> (Option<uint>, Option<uint>) {
+    fn size_hint(&self) -> (uint, Option<uint>) {
         self.iter.size_hint()
     }
 }
@@ -762,9 +757,9 @@ fn next(&mut self) -> Option<A> {
     }
 
     #[inline]
-    fn size_hint(&self) -> (Option<uint>, Option<uint>) {
+    fn size_hint(&self) -> (uint, Option<uint>) {
         let (_, upper) = self.iter.size_hint();
-        (None, upper) // can't know a lower bound, due to the predicate
+        (0, upper) // can't know a lower bound, due to the predicate
     }
 }
 
@@ -787,9 +782,9 @@ fn next(&mut self) -> Option<B> {
     }
 
     #[inline]
-    fn size_hint(&self) -> (Option<uint>, Option<uint>) {
+    fn size_hint(&self) -> (uint, Option<uint>) {
         let (_, upper) = self.iter.size_hint();
-        (None, upper) // can't know a lower bound, due to the predicate
+        (0, upper) // can't know a lower bound, due to the predicate
     }
 }
 
index 1014ff48b1d2386c26723b9727d831ee9835f72a..7244a9a7aac54fe4ac0ffa5d49832b07a85abeb1 100644 (file)
@@ -2024,14 +2024,14 @@ fn next(&mut self) -> Option<$elem> {
             }
 
             #[inline]
-            fn size_hint(&self) -> (Option<uint>, Option<uint>) {
+            fn size_hint(&self) -> (uint, Option<uint>) {
                 let diff = if $step > 0 {
                     (self.end as uint) - (self.ptr as uint)
                 } else {
                     (self.ptr as uint) - (self.end as uint)
                 };
-                let exact = Some(diff / size_of::<$elem>());
-                (exact, exact)
+                let exact = diff / size_of::<$elem>();
+                (exact, Some(exact))
             }
         }
     }
@@ -2132,7 +2132,7 @@ pub fn from_iterator(iterator: &mut T) -> ~[A] {
 impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
     pub fn from_iterator(iterator: &mut T) -> ~[A] {
         let (lower, _) = iterator.size_hint();
-        let mut xs = with_capacity(lower.get_or_zero());
+        let mut xs = with_capacity(lower);
         for iterator.advance |x| {
             xs.push(x);
         }
@@ -2968,17 +2968,17 @@ fn test_iterator() {
         use iterator::*;
         let xs = [1, 2, 5, 10, 11];
         let mut it = xs.iter();
-        assert_eq!(it.size_hint(), (Some(5), Some(5)));
+        assert_eq!(it.size_hint(), (5, Some(5)));
         assert_eq!(it.next().unwrap(), &1);
-        assert_eq!(it.size_hint(), (Some(4), Some(4)));
+        assert_eq!(it.size_hint(), (4, Some(4)));
         assert_eq!(it.next().unwrap(), &2);
-        assert_eq!(it.size_hint(), (Some(3), Some(3)));
+        assert_eq!(it.size_hint(), (3, Some(3)));
         assert_eq!(it.next().unwrap(), &5);
-        assert_eq!(it.size_hint(), (Some(2), Some(2)));
+        assert_eq!(it.size_hint(), (2, Some(2)));
         assert_eq!(it.next().unwrap(), &10);
-        assert_eq!(it.size_hint(), (Some(1), Some(1)));
+        assert_eq!(it.size_hint(), (1, Some(1)));
         assert_eq!(it.next().unwrap(), &11);
-        assert_eq!(it.size_hint(), (Some(0), Some(0)));
+        assert_eq!(it.size_hint(), (0, Some(0)));
         assert!(it.next().is_none());
     }
 
@@ -2986,10 +2986,10 @@ fn test_iterator() {
     fn test_iter_size_hints() {
         use iterator::*;
         let mut xs = [1, 2, 5, 10, 11];
-        assert_eq!(xs.iter().size_hint(), (Some(5), Some(5)));
-        assert_eq!(xs.rev_iter().size_hint(), (Some(5), Some(5)));
-        assert_eq!(xs.mut_iter().size_hint(), (Some(5), Some(5)));
-        assert_eq!(xs.mut_rev_iter().size_hint(), (Some(5), Some(5)));
+        assert_eq!(xs.iter().size_hint(), (5, Some(5)));
+        assert_eq!(xs.rev_iter().size_hint(), (5, Some(5)));
+        assert_eq!(xs.mut_iter().size_hint(), (5, Some(5)));
+        assert_eq!(xs.mut_rev_iter().size_hint(), (5, Some(5)));
     }
 
     #[test]