Skip to content

Commit

Permalink
Add Option::{ok_or, ok_or_else}
Browse files Browse the repository at this point in the history
These are the inverses of `Result::ok` and help to bridge `Option` and
`Result` based APIs.
  • Loading branch information
sfackler committed Sep 26, 2014
1 parent 3f299ff commit 0c8878d
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/libcore/option.rs
Expand Up @@ -145,10 +145,11 @@

use cmp::{PartialEq, Eq, Ord};
use default::Default;
use slice::Slice;
use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
use mem;
use result::{Result, Ok, Err};
use slice;
use slice::Slice;

// Note that this is not a lang item per se, but it has a hidden dependency on
// `Iterator`, which is one. The compiler assumes that the `next` method of
Expand Down Expand Up @@ -439,6 +440,48 @@ impl<T> Option<T> {
match self { None => def(), Some(t) => f(t) }
}

/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
/// `Ok(v)` and `None` to `Err(err)`.
///
/// # Example
///
/// ```
/// let x = Some("foo");
/// assert_eq!(x.ok_or(0i), Ok("foo"));
///
/// let x: Option<&str> = None;
/// assert_eq!(x.ok_or(0i), Err(0i));
/// ```
#[inline]
#[experimental]
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
match self {
Some(v) => Ok(v),
None => Err(err),
}
}

/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
/// `Ok(v)` and `None` to `Err(err())`.
///
/// # Example
///
/// ```
/// let x = Some("foo");
/// assert_eq!(x.ok_or_else(|| 0i), Ok("foo"));
///
/// let x: Option<&str> = None;
/// assert_eq!(x.ok_or_else(|| 0i), Err(0i));
/// ```
#[inline]
#[experimental]
pub fn ok_or_else<E>(self, err: || -> E) -> Result<T, E> {
match self {
Some(v) => Ok(v),
None => Err(err()),
}
}

/// Deprecated.
///
/// Applies a function to the contained value or does nothing.
Expand Down

13 comments on commit 0c8878d

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from aturon
at sfackler@0c8878d

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sfackler/rust/into-result = 0c8878d into auto

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sfackler/rust/into-result = 0c8878d merged ok, testing candidate = 9827a476

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from aturon
at sfackler@0c8878d

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sfackler/rust/into-result = 0c8878d into auto

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sfackler/rust/into-result = 0c8878d merged ok, testing candidate = 1109b005

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from aturon
at sfackler@0c8878d

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sfackler/rust/into-result = 0c8878d into auto

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sfackler/rust/into-result = 0c8878d merged ok, testing candidate = 606bf11

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 0c8878d Sep 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 606bf11

Please sign in to comment.