Made Results API more composable
This commit is contained in:
parent
aa4455e4c7
commit
142eb685f9
@ -1822,7 +1822,7 @@ fn test_linker_build() {
|
||||
let workspace = workspace.path();
|
||||
let matches = getopts([], optgroups());
|
||||
let options = build_session_options(@"rustpkg",
|
||||
matches.get_ref(),
|
||||
matches.as_ref().unwrap(),
|
||||
@diagnostic::DefaultEmitter as
|
||||
@diagnostic::Emitter);
|
||||
let sess = build_session(options,
|
||||
|
@ -151,7 +151,6 @@ mod tests {
|
||||
use super::*;
|
||||
use super::AnyRefExt;
|
||||
use option::{Some, None};
|
||||
use hash::Hash;
|
||||
|
||||
#[deriving(Eq)]
|
||||
struct Test;
|
||||
|
@ -10,13 +10,11 @@
|
||||
|
||||
//! 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, OptionIterator};
|
||||
use option::{None, Option, Some};
|
||||
use option::{ToOption, IntoOption, AsOption};
|
||||
use str::OwnedStr;
|
||||
use to_str::ToStr;
|
||||
@ -24,14 +22,11 @@ use vec::OwnedVector;
|
||||
use vec;
|
||||
|
||||
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
|
||||
///
|
||||
/// In order to provide informative error messages, `E` is required to implement `ToStr`.
|
||||
/// It is further recommended for `E` to be a descriptive error type, eg a `enum` for
|
||||
/// all possible errors cases.
|
||||
#[deriving(Clone, DeepClone, Eq, Ord, TotalEq, TotalOrd, ToStr)]
|
||||
pub enum Result<T, E> {
|
||||
/// Contains the successful result value
|
||||
/// Contains the success value
|
||||
Ok(T),
|
||||
|
||||
/// Contains the error value
|
||||
Err(E)
|
||||
}
|
||||
@ -40,7 +35,7 @@ pub enum Result<T, E> {
|
||||
// Type implementation
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T, E: ToStr> Result<T, E> {
|
||||
impl<T, E> Result<T, E> {
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Querying the contained values
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
@ -60,6 +55,29 @@ impl<T, E: ToStr> Result<T, E> {
|
||||
!self.is_ok()
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Adapter for each variant
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Convert from `Result<T, E>` to `Option<T>`
|
||||
#[inline]
|
||||
pub fn ok(self) -> Option<T> {
|
||||
match self {
|
||||
Ok(x) => Some(x),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from `Result<T, E>` to `Option<E>`
|
||||
#[inline]
|
||||
pub fn err(self) -> Option<E> {
|
||||
match self {
|
||||
Ok(_) => None,
|
||||
Err(x) => Some(x),
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Adapter for working with references
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
@ -82,52 +100,6 @@ impl<T, E: ToStr> Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Getting to contained values
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Unwraps a result, yielding the content of an `Ok`.
|
||||
/// Fails if the value is a `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),
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwraps a result, yielding the content of an `Err`.
|
||||
/// Fails if the value is a `Ok` with a custom failure message provided by `msg`.
|
||||
#[inline]
|
||||
pub fn expect_err<M: Any + Send>(self, msg: M) -> E {
|
||||
match self {
|
||||
Err(e) => e,
|
||||
Ok(_) => fail!(msg),
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwraps a result, yielding the content of an `Ok`.
|
||||
/// Fails if the value is a `Err` with an error message derived
|
||||
/// from `E`'s `ToStr` implementation.
|
||||
#[inline]
|
||||
pub fn unwrap(self) -> T {
|
||||
match self {
|
||||
Ok(t) => t,
|
||||
Err(e) => fail!("called `Result::unwrap()` on `Err` value '{}'",
|
||||
e.to_str()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwraps a result, yielding the content of an `Err`.
|
||||
/// Fails if the value is a `Ok`.
|
||||
#[inline]
|
||||
pub fn unwrap_err(self) -> E {
|
||||
match self {
|
||||
Ok(_) => fail!("called `Result::unwrap_err()` on an `Ok` value"),
|
||||
Err(e) => e
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Transforming contained values
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
@ -163,34 +135,6 @@ impl<T, E: ToStr> Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Iterator constructors
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Returns an `Iterator` over one or zero references to the value of an `Ok`
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// for buf in read_file(file) {
|
||||
/// print_buf(buf)
|
||||
/// }
|
||||
#[inline]
|
||||
pub fn iter<'r>(&'r self) -> OptionIterator<&'r T> {
|
||||
match *self {
|
||||
Ok(ref t) => Some(t),
|
||||
Err(..) => None,
|
||||
}.move_iter()
|
||||
}
|
||||
|
||||
/// Returns an `Iterator` over one or zero references to the value of an `Err`
|
||||
#[inline]
|
||||
pub fn iter_err<'r>(&'r self) -> OptionIterator<&'r E> {
|
||||
match *self {
|
||||
Ok(..) => None,
|
||||
Err(ref t) => Some(t),
|
||||
}.move_iter()
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Boolean operations on the values, eager and lazy
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
@ -239,17 +183,23 @@ impl<T, E: ToStr> Result<T, E> {
|
||||
// Common special cases
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Get a reference to the value out of a successful result
|
||||
///
|
||||
/// # Failure
|
||||
///
|
||||
/// If the result is an error
|
||||
/// Unwraps a result, yielding the content of an `Ok`.
|
||||
/// Fails if the value is an `Err`.
|
||||
#[inline]
|
||||
pub fn get_ref<'a>(&'a self) -> &'a T {
|
||||
match *self {
|
||||
Ok(ref t) => t,
|
||||
Err(ref e) => fail!("called `Result::get_ref()` on `Err` value '{}'",
|
||||
e.to_str()),
|
||||
pub fn unwrap(self) -> T {
|
||||
match self {
|
||||
Ok(t) => t,
|
||||
Err(_) => fail!("called `Result::unwrap()` on an `Err` value")
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwraps a result, yielding the content of an `Err`.
|
||||
/// Fails if the value is an `Ok`.
|
||||
#[inline]
|
||||
pub fn unwrap_err(self) -> E {
|
||||
match self {
|
||||
Ok(_) => fail!("called `Result::unwrap_err()` on an `Ok` value"),
|
||||
Err(e) => e
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -458,31 +408,6 @@ mod tests {
|
||||
assert_eq!(op2().or_else(|e| Err::<int, ~str>(e + "!")).unwrap_err(), ~"sadface!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_impl_iter() {
|
||||
let mut valid = false;
|
||||
let okval = Ok::<~str, ~str>(~"a");
|
||||
okval.iter().next().map(|_| { valid = true; });
|
||||
assert!(valid);
|
||||
|
||||
let errval = Err::<~str, ~str>(~"b");
|
||||
errval.iter().next().map(|_| { valid = false; });
|
||||
assert!(valid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_impl_iter_err() {
|
||||
let mut valid = true;
|
||||
let okval = Ok::<~str, ~str>(~"a");
|
||||
okval.iter_err().next().map(|_| { valid = false });
|
||||
assert!(valid);
|
||||
|
||||
valid = false;
|
||||
let errval = Err::<~str, ~str>(~"b");
|
||||
errval.iter_err().next().map(|_| { valid = true });
|
||||
assert!(valid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_impl_map() {
|
||||
assert_eq!(Ok::<~str, ~str>(~"a").map(|x| x + "b"), Ok(~"ab"));
|
||||
@ -495,12 +420,6 @@ mod tests {
|
||||
assert_eq!(Err::<~str, ~str>(~"a").map_err(|x| x + "b"), Err(~"ab"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_get_ref_method() {
|
||||
let foo: Result<int, ()> = Ok(100);
|
||||
assert_eq!(*foo.get_ref(), 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_collect() {
|
||||
assert_eq!(collect(range(0, 0)
|
||||
|
@ -48,6 +48,7 @@ pub unsafe fn borrow<T>(f: |&mut T|) {
|
||||
/// it wherever possible.
|
||||
#[cfg(not(windows), not(target_os = "android"))]
|
||||
pub mod compiled {
|
||||
#[cfg(not(test))]
|
||||
use libc::c_void;
|
||||
use cast;
|
||||
use option::{Option, Some, None};
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// error-pattern:called `Result::unwrap()` on `Err` value 'kitty'
|
||||
// error-pattern:called `Result::unwrap()` on an `Err` value
|
||||
|
||||
use std::result;
|
||||
|
||||
|
@ -64,7 +64,7 @@ fn test_rm_tempdir() {
|
||||
let f: proc() -> TempDir = proc() {
|
||||
TempDir::new("test_rm_tempdir").unwrap()
|
||||
};
|
||||
let tmp = task::try(f).expect("test_rm_tmdir");
|
||||
let tmp = task::try(f).ok().expect("test_rm_tmdir");
|
||||
path = tmp.path().clone();
|
||||
assert!(path.exists());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user