提交 467d381d 编写于 作者: B bors

auto merge of #7931 : blake2-ppc/rust/chain-mut-ref, r=pcwalton

First, clean up the uses of "None" and "Some" to always use consistent title case matching the variant names.

Add .chain_mut_ref() which is a missing method. A use case example for this method is extraction of an optional value from an Option\<Container\> value.
......@@ -118,13 +118,13 @@ pub fn mut_iter<'r>(&'r mut self) -> OptionMutIterator<'r, T> {
}
}
/// Returns true if the option equals `none`
/// Returns true if the option equals `None`
#[inline]
pub fn is_none(&self) -> bool {
match *self { None => true, Some(_) => false }
}
/// Returns true if the option contains some value
/// Returns true if the option contains a `Some` value
#[inline]
pub fn is_some(&self) -> bool { !self.is_none() }
......@@ -158,6 +158,17 @@ pub fn chain_ref<'a, U>(&'a self, f: &fn(x: &'a T) -> Option<U>)
}
}
/// 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> {
......@@ -167,19 +178,19 @@ pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {
}
}
/// Maps a `some` value from one type to another by reference
/// Maps a `Some` value from one type to another by reference
#[inline]
pub fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option<U> {
match *self { Some(ref x) => Some(f(x)), None => None }
}
/// Maps a `some` value from one type to another by a mutable reference
/// Maps a `Some` value from one type to another by a mutable reference
#[inline]
pub fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option<U> {
match *self { Some(ref mut x) => Some(f(x)), None => None }
}
/// Maps a `some` value from one type to another by a mutable reference,
/// Maps a `Some` value from one type to another by a mutable reference,
/// or returns a default value.
#[inline]
pub fn map_mut_default<'a, U>(&'a mut self, def: U, f: &fn(&'a mut T) -> U) -> U {
......@@ -260,7 +271,7 @@ pub fn mutate_default(&mut self, def: T, f: &fn(T) -> T) {
pub fn get_ref<'a>(&'a self) -> &'a T {
match *self {
Some(ref x) => x,
None => fail!("option::get_ref none")
None => fail!("option::get_ref None")
}
}
......@@ -282,7 +293,7 @@ pub fn get_ref<'a>(&'a self) -> &'a T {
pub fn get_mut_ref<'a>(&'a mut self) -> &'a mut T {
match *self {
Some(ref mut x) => x,
None => fail!("option::get_mut_ref none")
None => fail!("option::get_mut_ref None")
}
}
......@@ -306,7 +317,7 @@ pub fn unwrap(self) -> T {
*/
match self {
Some(x) => x,
None => fail!("option::unwrap none")
None => fail!("option::unwrap None")
}
}
......@@ -320,7 +331,7 @@ pub fn unwrap(self) -> T {
*/
#[inline]
pub fn take_unwrap(&mut self) -> T {
if self.is_none() { fail!("option::take_unwrap none") }
if self.is_none() { fail!("option::take_unwrap None") }
self.take().unwrap()
}
......@@ -330,7 +341,7 @@ pub fn take_unwrap(&mut self) -> T {
*
* # Failure
*
* Fails if the value equals `none`
* Fails if the value equals `None`
*/
#[inline]
pub fn expect(self, reason: &str) -> T {
......@@ -358,7 +369,7 @@ pub fn expect(self, reason: &str) -> T {
pub fn get(self) -> T {
match self {
Some(x) => return x,
None => fail!("option::get none")
None => fail!("option::get None")
}
}
......@@ -368,7 +379,7 @@ pub fn get_or_default(self, def: T) -> T {
match self { Some(x) => x, None => def }
}
/// Applies a function zero or more times until the result is none.
/// Applies a function zero or more times until the result is None.
#[inline]
pub fn while_some(self, blk: &fn(v: T) -> Option<T>) {
let mut opt = self;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册