std: Use method name Option::consume

With Option as the simplest container, `consume` is the way to turn it
into a by-value iterator.
This commit is contained in:
blake2-ppc 2013-08-03 21:34:00 +02:00
parent 78effe7626
commit 520f292e48
2 changed files with 7 additions and 5 deletions

View File

@ -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)),

View File

@ -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`.