about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-04-13 19:36:50 -0700
committerbors <bors@rust-lang.org>2014-04-13 19:36:50 -0700
commitbb9b2e0ebe63b6853a7936aa8071859e433c6597 (patch)
tree6cbfd229e01cf6ae98a2e657fddb978323aab87e /src/libstd
parent5d284a0daa39f6b87028f97b5bfa2bb92f658f83 (diff)
parenta16eae6ffd923df5b349f55b75caf5af2aa5378d (diff)
downloadrust-bb9b2e0ebe63b6853a7936aa8071859e433c6597.tar.gz
rust-bb9b2e0ebe63b6853a7936aa8071859e433c6597.zip
auto merge of #13475 : Ryman/rust/result_unwrap_or_else, r=brson
It might make more sense to mirror `Option`'s `unwrap_or_else` but I've left it as `handle` as it feels more explicit about the signature difference.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/result.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/libstd/result.rs b/src/libstd/result.rs
index fa16b6f6eeb..8bd36127db2 100644
--- a/src/libstd/result.rs
+++ b/src/libstd/result.rs
@@ -188,6 +188,26 @@ impl<T, E> Result<T, E> {
         }
     }
 
+    /// Unwraps a result, yielding the content of an `Ok`.
+    /// Else it returns `optb`.
+    #[inline]
+    pub fn unwrap_or(self, optb: T) -> T {
+        match self {
+            Ok(t) => t,
+            Err(_) => optb
+        }
+    }
+
+    /// Unwraps a result, yielding the content of an `Ok`.
+    /// If the value is an `Err` then it calls `op` with its value.
+    #[inline]
+    pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
+        match self {
+            Ok(t) => t,
+            Err(e) => op(e)
+        }
+    }
+
     /// Unwraps a result, yielding the content of an `Err`.
     /// Fails if the value is an `Ok`.
     #[inline]
@@ -389,4 +409,45 @@ mod tests {
         assert_eq!(format!("{}", ok), ~"Ok(100)");
         assert_eq!(format!("{}", err), ~"Err(Err)");
     }
+
+    #[test]
+    pub fn test_unwrap_or() {
+        let ok: Result<int, ~str> = Ok(100);
+        let ok_err: Result<int, ~str> = Err(~"Err");
+
+        assert_eq!(ok.unwrap_or(50), 100);
+        assert_eq!(ok_err.unwrap_or(50), 50);
+    }
+
+    #[test]
+    pub fn test_unwrap_or_else() {
+        fn handler(msg: ~str) -> int {
+            if msg == ~"I got this." {
+                50
+            } else {
+                fail!("BadBad")
+            }
+        }
+
+        let ok: Result<int, ~str> = Ok(100);
+        let ok_err: Result<int, ~str> = Err(~"I got this.");
+
+        assert_eq!(ok.unwrap_or_handle(handler), 100);
+        assert_eq!(ok_err.unwrap_or_handle(handler), 50);
+    }
+
+    #[test]
+    #[should_fail]
+    pub fn test_unwrap_or_else_failure() {
+        fn handler(msg: ~str) -> int {
+            if msg == ~"I got this." {
+                50
+            } else {
+                fail!("BadBad")
+            }
+        }
+
+        let bad_err: Result<int, ~str> = Err(~"Unrecoverable mess.");
+        let _ : int = bad_err.unwrap_or_handle(handler);
+    }
 }