diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-09-12 18:54:02 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-09-12 18:54:28 -0700 |
| commit | 7f9c5aae9e9d94b88cb7507633f449534d32c109 (patch) | |
| tree | 334e59116e3229de8114acc1cc6c3c0cd1873b1f /src/libstd | |
| parent | 7c08abb0cee1a1e679b654d2dd17f38317b58621 (diff) | |
| download | rust-7f9c5aae9e9d94b88cb7507633f449534d32c109.tar.gz rust-7f9c5aae9e9d94b88cb7507633f449534d32c109.zip | |
std: Restore Option::chain{,_mut}_ref as and_then{,_mut}_ref
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/iter.rs | 14 | ||||
| -rw-r--r-- | src/libstd/option.rs | 28 |
2 files changed, 26 insertions, 16 deletions
diff --git a/src/libstd/iter.rs b/src/libstd/iter.rs index f36fb2d98bd..ec3c02a31f2 100644 --- a/src/libstd/iter.rs +++ b/src/libstd/iter.rs @@ -1504,12 +1504,7 @@ impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for FlatMap<'self, } } match self.iter.next().map_move(|x| (self.f)(x)) { - None => { - return match self.backiter { - Some(ref mut it) => it.next(), - None => None, - }; - } + None => return self.backiter.and_then_mut_ref(|it| it.next()), next => self.frontiter = next, } } @@ -1541,12 +1536,7 @@ impl<'self, } } match self.iter.next_back().map_move(|x| (self.f)(x)) { - None => { - return match self.frontiter { - Some(ref mut it) => it.next_back(), - None => None, - }; - } + None => return self.frontiter.and_then_mut_ref(|it| it.next_back()), next => self.backiter = next, } } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 968330a18a8..ce725257dff 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -138,8 +138,8 @@ impl<T> Option<T> { } } - /// Returns `None` if the option is `None`, otherwise calls and returns the - /// value of `f`. + /// Returns `None` if the option is `None`, otherwise calls `f` with the + /// wrapped value and returns the result. #[inline] pub fn and_then<U>(self, f: &fn(T) -> Option<U>) -> Option<U> { match self { @@ -148,6 +148,26 @@ impl<T> Option<T> { } } + /// Returns `None` if the option is `None`, otherwise calls `f` with a + /// reference to the wrapped value and returns the result. + #[inline] + pub fn and_then_ref<'a, U>(&'a self, f: &fn(&'a T) -> Option<U>) -> Option<U> { + match *self { + Some(ref x) => f(x), + None => None + } + } + + /// Returns `None` if the option is `None`, otherwise calls `f` with a + /// mutable reference to the wrapped value and returns the result. + #[inline] + pub fn and_then_mut_ref<'a, U>(&'a mut self, f: &fn(&'a mut T) -> Option<U>) -> Option<U> { + match *self { + Some(ref mut x) => f(x), + None => None + } + } + /// Returns the option if it contains a value, otherwise returns `optb`. #[inline] pub fn or(self, optb: Option<T>) -> Option<T> { @@ -157,8 +177,8 @@ impl<T> Option<T> { } } - /// Returns the option if it contains a value, otherwise calls and returns the - /// value of `f`. + /// Returns the option if it contains a value, otherwise calls `f` and + /// returns the result. #[inline] pub fn or_else(self, f: &fn() -> Option<T>) -> Option<T> { match self { |
