diff options
| author | blake2-ppc <blake2-ppc> | 2013-08-03 21:34:00 +0200 |
|---|---|---|
| committer | blake2-ppc <blake2-ppc> | 2013-08-06 04:05:07 +0200 |
| commit | 520f292e48a61538e681ce61d46a58f965c800a1 (patch) | |
| tree | e804eff9a1b0ff82d22cee2037ecf61fb95bc021 /src/libstd | |
| parent | 78effe762666f64de28c890ea1a15672c712f390 (diff) | |
| download | rust-520f292e48a61538e681ce61d46a58f965c800a1.tar.gz rust-520f292e48a61538e681ce61d46a58f965c800a1.zip | |
std: Use method name Option::consume
With Option as the simplest container, `consume` is the way to turn it into a by-value iterator.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/option.rs | 8 | ||||
| -rw-r--r-- | src/libstd/result.rs | 4 |
2 files changed, 7 insertions, 5 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 40a3944b7e0..ea1bddcdb4b 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -134,7 +134,7 @@ impl<T> Option<T> { /// Return a consuming iterator over the possibly contained value #[inline] - pub fn consume_iter(self) -> OptionIterator<T> { + pub fn consume(self) -> OptionIterator<T> { OptionIterator{opt: self} } @@ -410,16 +410,18 @@ impl<T> Zero for Option<T> { fn is_zero(&self) -> bool { self.is_none() } } -/// Immutable iterator over an Option +/// An iterator that yields either one or zero elements pub struct OptionIterator<A> { priv opt: Option<A> } impl<A> Iterator<A> for OptionIterator<A> { + #[inline] fn next(&mut self) -> Option<A> { - util::replace(&mut self.opt, None) + self.opt.take() } + #[inline] fn size_hint(&self) -> (uint, Option<uint>) { match self.opt { Some(_) => (1, Some(1)), diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 5a6021d32a5..91f42edf0ae 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -94,7 +94,7 @@ impl<T, E: ToStr> Result<T, E> { match *self { Ok(ref t) => Some(t), Err(*) => None, - }.consume_iter() + }.consume() } /// Call a method based on a previous result @@ -108,7 +108,7 @@ impl<T, E: ToStr> Result<T, E> { match *self { Ok(*) => None, Err(ref t) => Some(t), - }.consume_iter() + }.consume() } /// Unwraps a result, yielding the content of an `Ok`. |
