core: Inherit the result module

The unwrap()/unwrap_err() methods are temporarily removed, and will be added
back in the next commit.
This commit is contained in:
Alex Crichton 2014-05-01 11:11:28 -07:00
parent f12b51705b
commit a156534a96
3 changed files with 22 additions and 31 deletions

View File

@ -268,14 +268,13 @@
use clone::Clone;
use cmp::Eq;
use std::fmt::Show;
use iter::{Iterator, FromIterator};
use option::{None, Option, Some};
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
///
/// See the [`std::result`](index.html) module documentation for details.
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd, Show, Hash)]
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
#[must_use]
pub enum Result<T, E> {
/// Contains the success value
@ -516,34 +515,6 @@ impl<T, E> Result<T, E> {
}
}
impl<T, E: Show> Result<T, E> {
/// Unwraps a result, yielding the content of an `Ok`.
///
/// Fails if the value is an `Err`.
#[inline]
pub fn unwrap(self) -> T {
match self {
Ok(t) => t,
Err(e) =>
fail!("called `Result::unwrap()` on an `Err` value: {}", e)
}
}
}
impl<T: Show, E> Result<T, E> {
/// 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(t) =>
fail!("called `Result::unwrap_err()` on an `Ok` value: {}", t),
Err(e) => e
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Free functions
/////////////////////////////////////////////////////////////////////////////

View File

@ -1291,6 +1291,15 @@ impl<T: Show> Show for Option<T> {
}
}
impl<T: Show, U: Show> Show for ::result::Result<T, U> {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
Ok(ref t) => write!(f.buf, "Ok({})", *t),
Err(ref t) => write!(f.buf, "Err({})", *t),
}
}
}
impl<'a, T: Show> Show for &'a [T] {
fn fmt(&self, f: &mut Formatter) -> Result {
if f.flags & (1 << (parse::FlagAlternate as uint)) == 0 {

View File

@ -70,8 +70,9 @@ use iter::Iterator;
use option::{Option, Some, None};
use owned::Box;
use rc::Rc;
use str::{Str, StrSlice};
use result::{Result, Ok, Err};
use slice::{Vector, ImmutableVector};
use str::{Str, StrSlice};
use vec::Vec;
/// Reexport the `sip::hash` function as our default hasher.
@ -292,6 +293,16 @@ impl<S: Writer> Hash<S> for TypeId {
}
}
impl<S: Writer, T: Hash<S>, U: Hash<S>> Hash<S> for Result<T, U> {
#[inline]
fn hash(&self, state: &mut S) {
match *self {
Ok(ref t) => { 1u.hash(state); t.hash(state); }
Err(ref t) => { 2u.hash(state); t.hash(state); }
}
}
}
//////////////////////////////////////////////////////////////////////////////
#[cfg(test)]