Skip to content

Commit 520f292

Browse files
author
blake2-ppc
committed
std: Use method name Option::consume
With Option as the simplest container, `consume` is the way to turn it into a by-value iterator.
1 parent 78effe7 commit 520f292

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/libstd/option.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<T> Option<T> {
134134

135135
/// Return a consuming iterator over the possibly contained value
136136
#[inline]
137-
pub fn consume_iter(self) -> OptionIterator<T> {
137+
pub fn consume(self) -> OptionIterator<T> {
138138
OptionIterator{opt: self}
139139
}
140140

@@ -410,16 +410,18 @@ impl<T> Zero for Option<T> {
410410
fn is_zero(&self) -> bool { self.is_none() }
411411
}
412412

413-
/// Immutable iterator over an Option
413+
/// An iterator that yields either one or zero elements
414414
pub struct OptionIterator<A> {
415415
priv opt: Option<A>
416416
}
417417

418418
impl<A> Iterator<A> for OptionIterator<A> {
419+
#[inline]
419420
fn next(&mut self) -> Option<A> {
420-
util::replace(&mut self.opt, None)
421+
self.opt.take()
421422
}
422423

424+
#[inline]
423425
fn size_hint(&self) -> (uint, Option<uint>) {
424426
match self.opt {
425427
Some(_) => (1, Some(1)),

src/libstd/result.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<T, E: ToStr> Result<T, E> {
9494
match *self {
9595
Ok(ref t) => Some(t),
9696
Err(*) => None,
97-
}.consume_iter()
97+
}.consume()
9898
}
9999

100100
/// Call a method based on a previous result
@@ -108,7 +108,7 @@ impl<T, E: ToStr> Result<T, E> {
108108
match *self {
109109
Ok(*) => None,
110110
Err(ref t) => Some(t),
111-
}.consume_iter()
111+
}.consume()
112112
}
113113

114114
/// Unwraps a result, yielding the content of an `Ok`.

0 commit comments

Comments
 (0)