Skip to content

Add .chain_mut_ref() for Option #7931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/libstd/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ impl<T> Option<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() }

Expand Down Expand Up @@ -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> {
Expand All @@ -168,19 +179,19 @@ impl<T> 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 {
Expand Down Expand Up @@ -261,7 +272,7 @@ impl<T> Option<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")
}
}

Expand All @@ -283,7 +294,7 @@ impl<T> Option<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")
}
}

Expand All @@ -307,7 +318,7 @@ impl<T> Option<T> {
*/
match self {
Some(x) => x,
None => fail!("option::unwrap none")
None => fail!("option::unwrap None")
}
}

Expand All @@ -321,7 +332,7 @@ impl<T> Option<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()
}

Expand All @@ -331,7 +342,7 @@ impl<T> Option<T> {
*
* # Failure
*
* Fails if the value equals `none`
* Fails if the value equals `None`
*/
#[inline]
pub fn expect(self, reason: &str) -> T {
Expand Down Expand Up @@ -359,7 +370,7 @@ impl<T> Option<T> {
pub fn get(self) -> T {
match self {
Some(x) => return x,
None => fail!("option::get none")
None => fail!("option::get None")
}
}

Expand All @@ -369,7 +380,7 @@ impl<T> Option<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;
Expand Down