diff options
| author | Marvin Löbel <loebel.marvin@gmail.com> | 2013-12-06 22:23:23 +0100 |
|---|---|---|
| committer | Marvin Löbel <loebel.marvin@gmail.com> | 2013-12-06 22:29:02 +0100 |
| commit | 142eb685f913e15494bfab9ec142e40f6bf92cc4 (patch) | |
| tree | c68d74020eccd2bdd120ac48cb79898ec79bf3d0 /src/libstd | |
| parent | aa4455e4c76598fcf6de84de14f050a700a2a14e (diff) | |
| download | rust-142eb685f913e15494bfab9ec142e40f6bf92cc4.tar.gz rust-142eb685f913e15494bfab9ec142e40f6bf92cc4.zip | |
Made Results API more composable
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/any.rs | 1 | ||||
| -rw-r--r-- | src/libstd/result.rs | 167 | ||||
| -rw-r--r-- | src/libstd/rt/local_ptr.rs | 1 |
3 files changed, 44 insertions, 125 deletions
diff --git a/src/libstd/any.rs b/src/libstd/any.rs index 965b9ffb95c..6a3e6933001 100644 --- a/src/libstd/any.rs +++ b/src/libstd/any.rs @@ -151,7 +151,6 @@ mod tests { use super::*; use super::AnyRefExt; use option::{Some, None}; - use hash::Hash; #[deriving(Eq)] struct Test; diff --git a/src/libstd/result.rs b/src/libstd/result.rs index afcf092b4f6..690b4e06d49 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -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,71 +55,48 @@ impl<T, E: ToStr> Result<T, E> { !self.is_ok() } - ///////////////////////////////////////////////////////////////////////// - // Adapter for working with references - ///////////////////////////////////////////////////////////////////////// - - /// Convert from `Result<T, E>` to `Result<&T, &E>` - #[inline] - pub fn as_ref<'r>(&'r self) -> Result<&'r T, &'r E> { - match *self { - Ok(ref x) => Ok(x), - Err(ref x) => Err(x), - } - } - - /// Convert from `Result<T, E>` to `Result<&mut T, &mut E>` - #[inline] - pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> { - match *self { - Ok(ref mut x) => Ok(x), - Err(ref mut x) => Err(x), - } - } ///////////////////////////////////////////////////////////////////////// - // Getting to contained values + // Adapter for each variant ///////////////////////////////////////////////////////////////////////// - /// Unwraps a result, yielding the content of an `Ok`. - /// Fails if the value is a `Err` with a custom failure message provided by `msg`. + /// Convert from `Result<T, E>` to `Option<T>` #[inline] - pub fn expect<M: Any + Send>(self, msg: M) -> T { + pub fn ok(self) -> Option<T> { match self { - Ok(t) => t, - Err(_) => fail!(msg), + Ok(x) => Some(x), + Err(_) => None, } } - /// Unwraps a result, yielding the content of an `Err`. - /// Fails if the value is a `Ok` with a custom failure message provided by `msg`. + /// Convert from `Result<T, E>` to `Option<E>` #[inline] - pub fn expect_err<M: Any + Send>(self, msg: M) -> E { + pub fn err(self) -> Option<E> { match self { - Err(e) => e, - Ok(_) => fail!(msg), + Ok(_) => None, + Err(x) => Some(x), } } - /// 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. + ///////////////////////////////////////////////////////////////////////// + // Adapter for working with references + ///////////////////////////////////////////////////////////////////////// + + /// Convert from `Result<T, E>` to `Result<&T, &E>` #[inline] - pub fn unwrap(self) -> T { - match self { - Ok(t) => t, - Err(e) => fail!("called `Result::unwrap()` on `Err` value '{}'", - e.to_str()), + pub fn as_ref<'r>(&'r self) -> Result<&'r T, &'r E> { + match *self { + Ok(ref x) => Ok(x), + Err(ref x) => Err(x), } } - /// Unwraps a result, yielding the content of an `Err`. - /// Fails if the value is a `Ok`. + /// Convert from `Result<T, E>` to `Result<&mut T, &mut E>` #[inline] - pub fn unwrap_err(self) -> E { - match self { - Ok(_) => fail!("called `Result::unwrap_err()` on an `Ok` value"), - Err(e) => e + pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> { + match *self { + Ok(ref mut x) => Ok(x), + Err(ref mut x) => Err(x), } } @@ -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 } } } @@ -459,31 +409,6 @@ mod tests { } #[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")); assert_eq!(Err::<~str, ~str>(~"a").map(|x| x + "b"), Err(~"a")); @@ -496,12 +421,6 @@ mod tests { } #[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) .map(|_| Ok::<int, ()>(0))), diff --git a/src/libstd/rt/local_ptr.rs b/src/libstd/rt/local_ptr.rs index 803938589af..bff9390ee3b 100644 --- a/src/libstd/rt/local_ptr.rs +++ b/src/libstd/rt/local_ptr.rs @@ -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}; |
