diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 2386a779235d..8855ad0e30f5 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -48,6 +48,7 @@ use util; use num::Zero; use old_iter::{BaseIter, MutableIter, ExtendedIter}; use old_iter; +use iterator::Iterator; use str::StrSlice; use clone::DeepClone; @@ -146,7 +147,24 @@ impl ExtendedIter for Option { } impl Option { + #[inline] + pub fn iter<'r>(&'r self) -> OptionIterator<'r, T> { + match *self { + Some(ref x) => OptionIterator{opt: Some(x)}, + None => OptionIterator{opt: None} + } + } + + #[inline] + pub fn mut_iter<'r>(&'r mut self) -> OptionMutIterator<'r, T> { + match *self { + Some(ref mut x) => OptionMutIterator{opt: Some(x)}, + None => OptionMutIterator{opt: None} + } + } + /// Returns true if the option equals `none` + #[inline] pub fn is_none(&const self) -> bool { match *self { None => true, Some(_) => false } } @@ -376,6 +394,26 @@ impl Option { } } +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) + } +} + +pub struct OptionMutIterator<'self, A> { + priv opt: Option<&'self mut A> +} + +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) + } +} + #[test] fn test_unwrap_ptr() { unsafe {