option: Add .chain_mut_ref()

.chain_mut_ref() is the mutable alternative to .chain_ref().

A use case example for this method is extraction of an optional value
from an Option<Container> value.
This commit is contained in:
blake2-ppc 2013-07-20 19:19:13 +02:00
parent 5991c60750
commit 625ca7afe4

View file

@ -159,6 +159,17 @@ impl<T> Option<T> {
}
}
/// Update an optional value by optionally running its content by mut reference
/// through a function that returns an option.
#[inline]
pub fn chain_mut_ref<'a, U>(&'a mut self, f: &fn(x: &'a mut T) -> Option<U>)
-> Option<U> {
match *self {
Some(ref mut x) => f(x),
None => None
}
}
/// Filters an optional value using given function.
#[inline(always)]
pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {