Skip to content

Add Result::expect() for consistency with Option::expect(). #11140

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 1 commit 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
17 changes: 16 additions & 1 deletion src/libstd/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

//! A type representing either success or failure

use any::Any;
use clone::Clone;
use cmp::Eq;
use fmt;
use iter::Iterator;
use kinds::Send;
use option::{None, Option, Some};
use str::OwnedStr;
use to_str::ToStr;
use vec::OwnedVector;
use vec;
use vec::OwnedVector;

/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
#[deriving(Clone, DeepClone, Eq, Ord, TotalEq, TotalOrd, ToStr)]
Expand Down Expand Up @@ -201,6 +203,19 @@ impl<T, E> Result<T, E> {
Err(e) => e
}
}

}

impl<T, E: Send> Result<T, E> {
/// Unwraps a result, yielding the content of a `Ok`
/// Fails if the value is an `Err` with a custom failure message provided by `msg`.
#[inline]
pub fn expect<M: Any + Send>(self, msg: M) -> T {
match self {
Ok(t) => t,
Err(_) => fail!(msg)
}
}
}

/////////////////////////////////////////////////////////////////////////////
Expand Down