about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-06-15 05:11:53 +0000
committerbors <bors@rust-lang.org>2015-06-15 05:11:53 +0000
commita54a80921971791b1148b7633cb43029f7d2ef9b (patch)
tree9f48fc8af5402cf352a9c474f22286fe67d35ff4
parent7d046230338e117c8a7c85bb4d542f2787d5ef9d (diff)
parent0937c10f3ca21585f0cb00c1a112e4ff4a742456 (diff)
downloadrust-a54a80921971791b1148b7633cb43029f7d2ef9b.tar.gz
rust-a54a80921971791b1148b7633cb43029f7d2ef9b.zip
Auto merge of #25359 - thepowersgang:result-expect-2, r=alexcrichton
As it says in the title. I've added an `expect` method to `Result` that allows printing both an error message (e.g. what operation was attempted), and the error value. This is separate from the `unwrap` and `ok().expect("message")` behaviours.
-rw-r--r--src/libcore/result.rs20
-rw-r--r--src/libcoretest/lib.rs1
-rw-r--r--src/libcoretest/result.rs13
3 files changed, 34 insertions, 0 deletions
diff --git a/src/libcore/result.rs b/src/libcore/result.rs
index 003c4b2b78c..772831b1a58 100644
--- a/src/libcore/result.rs
+++ b/src/libcore/result.rs
@@ -731,6 +731,26 @@ impl<T, E: fmt::Debug> Result<T, E> {
                 panic!("called `Result::unwrap()` on an `Err` value: {:?}", e)
         }
     }
+
+    /// Unwraps a result, yielding the content of an `Ok`.
+    ///
+    /// Panics if the value is an `Err`, with a panic message including the
+    /// passed message, and the content of the `Err`.
+    ///
+    /// # Examples
+    /// ```{.should_panic}
+    /// #![feature(result_expect)]
+    /// let x: Result<u32, &str> = Err("emergency failure");
+    /// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
+    /// ```
+    #[inline]
+    #[unstable(feature = "result_expect", reason = "newly introduced")]
+    pub fn expect(self, msg: &str) -> T {
+        match self {
+            Ok(t) => t,
+            Err(e) => panic!("{}: {:?}", msg, e),
+        }
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs
index deef26022f7..64e34883ee7 100644
--- a/src/libcoretest/lib.rs
+++ b/src/libcoretest/lib.rs
@@ -28,6 +28,7 @@
 #![feature(cell_extras)]
 #![feature(iter_empty)]
 #![feature(iter_once)]
+#![feature(result_expect)]
 
 extern crate core;
 extern crate test;
diff --git a/src/libcoretest/result.rs b/src/libcoretest/result.rs
index 3fdb1028753..02ea6b10e6e 100644
--- a/src/libcoretest/result.rs
+++ b/src/libcoretest/result.rs
@@ -137,3 +137,16 @@ pub fn test_unwrap_or_else_panic() {
     let bad_err: Result<isize, &'static str> = Err("Unrecoverable mess.");
     let _ : isize = bad_err.unwrap_or_else(handler);
 }
+
+
+#[test]
+pub fn test_expect_ok() {
+    let ok: Result<isize, &'static str> = Ok(100);
+    assert_eq!(ok.expect("Unexpected error"), 100);
+}
+#[test]
+#[should_panic(expected="Got expected error: \"All good\"")]
+pub fn test_expect_err() {
+    let err: Result<isize, &'static str> = Err("All good");
+    err.expect("Got expected error");
+}