diff options
| author | Steven Fackler <sfackler@gmail.com> | 2014-04-11 20:59:18 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2014-04-13 23:47:53 -0700 |
| commit | eb0473df93b49d6a2ff50f82e435dbd2e19467c7 (patch) | |
| tree | a34483e0c817fd840d7dd5e38401a5dc7883fbb6 /src/libstd | |
| parent | bb9b2e0ebe63b6853a7936aa8071859e433c6597 (diff) | |
| download | rust-eb0473df93b49d6a2ff50f82e435dbd2e19467c7.tar.gz rust-eb0473df93b49d6a2ff50f82e435dbd2e19467c7.zip | |
Make Result::{unwrap, unwrap_err} require Show
`foo.ok().unwrap()` and `foo.err().unwrap()` are the fallbacks for types that aren't `Show`. Closes #13379
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/comm/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/result.rs | 33 |
2 files changed, 20 insertions, 15 deletions
diff --git a/src/libstd/comm/mod.rs b/src/libstd/comm/mod.rs index 58781c01d66..e2d7a869fbc 100644 --- a/src/libstd/comm/mod.rs +++ b/src/libstd/comm/mod.rs @@ -496,7 +496,7 @@ impl<T: Send> Sender<T> { // This send cannot fail because the task is // asleep (we're looking at it), so the receiver // can't go away. - (*a.get()).send(t).unwrap(); + (*a.get()).send(t).ok().unwrap(); task.wake().map(|t| t.reawaken()); (a, Ok(())) } diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 8bd36127db2..9ca1443ea50 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -12,6 +12,7 @@ use clone::Clone; use cmp::Eq; +use std::fmt::Show; use iter::{Iterator, FromIterator}; use option::{None, Option, Some}; @@ -174,46 +175,50 @@ impl<T, E> Result<T, E> { } } - ///////////////////////////////////////////////////////////////////////// - // Common special cases - ///////////////////////////////////////////////////////////////////////// - /// Unwraps a result, yielding the content of an `Ok`. - /// Fails if the value is an `Err`. + /// Else it returns `optb`. #[inline] - pub fn unwrap(self) -> T { + pub fn unwrap_or(self, optb: T) -> T { match self { Ok(t) => t, - Err(_) => fail!("called `Result::unwrap()` on an `Err` value") + Err(_) => optb } } /// Unwraps a result, yielding the content of an `Ok`. - /// Else it returns `optb`. + /// If the value is an `Err` then it calls `op` with its value. #[inline] - pub fn unwrap_or(self, optb: T) -> T { + pub fn unwrap_or_handle(self, op: |E| -> T) -> T { match self { Ok(t) => t, - Err(_) => optb + Err(e) => op(e) } } +} +impl<T, E: Show> Result<T, E> { /// Unwraps a result, yielding the content of an `Ok`. - /// If the value is an `Err` then it calls `op` with its value. + /// + /// Fails if the value is an `Err`. #[inline] - pub fn unwrap_or_handle(self, op: |E| -> T) -> T { + pub fn unwrap(self) -> T { match self { Ok(t) => t, - Err(e) => op(e) + 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(_) => fail!("called `Result::unwrap_err()` on an `Ok` value"), + Ok(t) => + fail!("called `Result::unwrap_err()` on an `Ok` value: {}", t), Err(e) => e } } |
