提交 a156534a 编写于 作者: A Alex Crichton

core: Inherit the result module

The unwrap()/unwrap_err() methods are temporarily removed, and will be added
back in the next commit.
上级 f12b5170
...@@ -268,14 +268,13 @@ ...@@ -268,14 +268,13 @@
use clone::Clone; use clone::Clone;
use cmp::Eq; use cmp::Eq;
use std::fmt::Show;
use iter::{Iterator, FromIterator}; use iter::{Iterator, FromIterator};
use option::{None, Option, Some}; use option::{None, Option, Some};
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`). /// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
/// ///
/// See the [`std::result`](index.html) module documentation for details. /// 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] #[must_use]
pub enum Result<T, E> { pub enum Result<T, E> {
/// Contains the success value /// Contains the success value
...@@ -516,34 +515,6 @@ pub fn unwrap_or_handle(self, op: |E| -> T) -> T { ...@@ -516,34 +515,6 @@ pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
} }
} }
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 // Free functions
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
......
...@@ -1291,6 +1291,15 @@ fn fmt(&self, f: &mut Formatter) -> Result { ...@@ -1291,6 +1291,15 @@ fn fmt(&self, f: &mut Formatter) -> Result {
} }
} }
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] { impl<'a, T: Show> Show for &'a [T] {
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
if f.flags & (1 << (parse::FlagAlternate as uint)) == 0 { if f.flags & (1 << (parse::FlagAlternate as uint)) == 0 {
......
...@@ -70,8 +70,9 @@ ...@@ -70,8 +70,9 @@
use option::{Option, Some, None}; use option::{Option, Some, None};
use owned::Box; use owned::Box;
use rc::Rc; use rc::Rc;
use str::{Str, StrSlice}; use result::{Result, Ok, Err};
use slice::{Vector, ImmutableVector}; use slice::{Vector, ImmutableVector};
use str::{Str, StrSlice};
use vec::Vec; use vec::Vec;
/// Reexport the `sip::hash` function as our default hasher. /// Reexport the `sip::hash` function as our default hasher.
...@@ -292,6 +293,16 @@ fn hash(&self, state: &mut S) { ...@@ -292,6 +293,16 @@ fn hash(&self, state: &mut S) {
} }
} }
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)] #[cfg(test)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册