about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorblake2-ppc <blake2-ppc>2013-08-03 19:40:20 +0200
committerblake2-ppc <blake2-ppc>2013-08-06 03:59:56 +0200
commitce682cb45ff6f899d478358d7517fa83b215ac3c (patch)
tree6bdf0ea09f1a02231d0eec6f2c237373faa255b9 /src/libstd
parentbbda3fa9383dba653b20bd064102caceef91897a (diff)
downloadrust-ce682cb45ff6f899d478358d7517fa83b215ac3c.tar.gz
rust-ce682cb45ff6f899d478358d7517fa83b215ac3c.zip
std: Add .consume_iter() for Option, to make it reusable
Let Option be a base for a widely useful one- or zero- item iterator.
Refactor OptionIterator to support any generic element type, so the same
iterator impl can be used for both &T, &mut T and T iterators.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/option.rs42
1 files changed, 15 insertions, 27 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs
index 3a4a9220ee1..40a3944b7e0 100644
--- a/src/libstd/option.rs
+++ b/src/libstd/option.rs
@@ -116,7 +116,7 @@ impl<T: ToStr> ToStr for Option<T> {
 impl<T> Option<T> {
     /// Return an iterator over the possibly contained value
     #[inline]
-    pub fn iter<'r>(&'r self) -> OptionIterator<'r, T> {
+    pub fn iter<'r>(&'r self) -> OptionIterator<&'r T> {
         match *self {
             Some(ref x) => OptionIterator{opt: Some(x)},
             None => OptionIterator{opt: None}
@@ -125,13 +125,19 @@ impl<T> Option<T> {
 
     /// Return a mutable iterator over the possibly contained value
     #[inline]
-    pub fn mut_iter<'r>(&'r mut self) -> OptionMutIterator<'r, T> {
+    pub fn mut_iter<'r>(&'r mut self) -> OptionIterator<&'r mut T> {
         match *self {
-            Some(ref mut x) => OptionMutIterator{opt: Some(x)},
-            None => OptionMutIterator{opt: None}
+            Some(ref mut x) => OptionIterator{opt: Some(x)},
+            None => OptionIterator{opt: None}
         }
     }
 
+    /// Return a consuming iterator over the possibly contained value
+    #[inline]
+    pub fn consume_iter(self) -> OptionIterator<T> {
+        OptionIterator{opt: self}
+    }
+
     /// Returns true if the option equals `None`
     #[inline]
     pub fn is_none(&self) -> bool {
@@ -404,31 +410,13 @@ impl<T> Zero for Option<T> {
     fn is_zero(&self) -> bool { self.is_none() }
 }
 
-/// Immutable iterator over an `Option<A>`
-pub struct OptionIterator<'self, A> {
-    priv opt: Option<&'self A>
-}
-
-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>`
-pub struct OptionMutIterator<'self, A> {
-    priv opt: Option<&'self mut A>
+/// Immutable iterator over an Option
+pub struct OptionIterator<A> {
+    priv opt: Option<A>
 }
 
-impl<'self, A> Iterator<&'self mut A> for OptionMutIterator<'self, A> {
-    fn next(&mut self) -> Option<&'self mut A> {
+impl<A> Iterator<A> for OptionIterator<A> {
+    fn next(&mut self) -> Option<A> {
         util::replace(&mut self.opt, None)
     }