From a156534a96a6c401b14c80618c247eaadf876eb7 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 1 May 2014 11:11:28 -0700 Subject: [PATCH] core: Inherit the result module The unwrap()/unwrap_err() methods are temporarily removed, and will be added back in the next commit. --- src/{libstd => libcore}/result.rs | 31 +------------------------------ src/libstd/fmt/mod.rs | 9 +++++++++ src/libstd/hash/mod.rs | 13 ++++++++++++- 3 files changed, 22 insertions(+), 31 deletions(-) rename src/{libstd => libcore}/result.rs (96%) diff --git a/src/libstd/result.rs b/src/libcore/result.rs similarity index 96% rename from src/libstd/result.rs rename to src/libcore/result.rs index 922d2cf3d32..13f59d0875d 100644 --- a/src/libstd/result.rs +++ b/src/libcore/result.rs @@ -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 { /// Contains the success value @@ -516,34 +515,6 @@ impl Result { } } -impl Result { - /// 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 Result { - /// 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 ///////////////////////////////////////////////////////////////////////////// diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index 9f683e45678..8cfc0ae54c3 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -1291,6 +1291,15 @@ impl Show for Option { } } +impl Show for ::result::Result { + 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 { diff --git a/src/libstd/hash/mod.rs b/src/libstd/hash/mod.rs index 010ddbaa942..c8207a4c37c 100644 --- a/src/libstd/hash/mod.rs +++ b/src/libstd/hash/mod.rs @@ -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 Hash for TypeId { } } +impl, U: Hash> Hash for Result { + #[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)]