about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAndrew Paseltiner <apaseltiner@gmail.com>2013-07-08 18:31:26 -0400
committerDaniel Micay <danielmicay@gmail.com>2013-07-09 16:35:56 -0400
commitf2bd4416fa3549f962c8ffdd6474cf4bf0d21eca (patch)
tree5b579eb6574470f847c7167d56c996446cdaef27 /src
parenta4af0960bd4bef820cdd10b014d1f9858ec9fa14 (diff)
downloadrust-f2bd4416fa3549f962c8ffdd6474cf4bf0d21eca.tar.gz
rust-f2bd4416fa3549f962c8ffdd6474cf4bf0d21eca.zip
std: Implement `Iterator::size_hint` method for `Option` iterators
Diffstat (limited to 'src')
-rw-r--r--src/libstd/option.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs
index fb9962f8a44..222952a6dc1 100644
--- a/src/libstd/option.rs
+++ b/src/libstd/option.rs
@@ -379,6 +379,13 @@ impl<'self, A> Iterator<&'self A> for OptionIterator<'self, A> {
     fn next(&mut self) -> Option<&'self A> {
         util::replace(&mut self.opt, None)
     }
+
+    fn size_hint(&self) -> (uint, Option<uint>) {
+        match self.opt {
+            Some(_) => (1, Some(1)),
+            None => (0, Some(0)),
+        }
+    }
 }
 
 /// Mutable iterator over an `Option<A>`
@@ -390,6 +397,13 @@ impl<'self, A> Iterator<&'self mut A> for OptionMutIterator<'self, A> {
     fn next(&mut self) -> Option<&'self mut A> {
         util::replace(&mut self.opt, None)
     }
+
+    fn size_hint(&self) -> (uint, Option<uint>) {
+        match self.opt {
+            Some(_) => (1, Some(1)),
+            None => (0, Some(0)),
+        }
+    }
 }
 
 #[test]
@@ -487,3 +501,39 @@ fn test_filtered() {
     assert_eq!(some_stuff.get(), 42);
     assert!(modified_stuff.is_none());
 }
+
+#[test]
+fn test_iter() {
+    let val = 5;
+
+    let x = Some(val);
+    let mut it = x.iter();
+
+    assert_eq!(it.size_hint(), (1, Some(1)));
+    assert_eq!(it.next(), Some(&val));
+    assert_eq!(it.size_hint(), (0, Some(0)));
+    assert!(it.next().is_none());
+}
+
+#[test]
+fn test_mut_iter() {
+    let val = 5;
+    let new_val = 11;
+
+    let mut x = Some(val);
+    let mut it = x.mut_iter();
+
+    assert_eq!(it.size_hint(), (1, Some(1)));
+
+    match it.next() {
+        Some(interior) => {
+            assert_eq!(*interior, val);
+            *interior = new_val;
+            assert_eq!(x, Some(new_val));
+        }
+        None => assert!(false),
+    }
+
+    assert_eq!(it.size_hint(), (0, Some(0)));
+    assert!(it.next().is_none());
+}