about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-01-10 20:08:58 +0000
committerbors <bors@rust-lang.org>2020-01-10 20:08:58 +0000
commit175631311716d7dfeceec40d2587cde7142ffa8c (patch)
treec77e6f2bb4e682bb6239b5e6ab31d674057ee31a /src/libcore
parentac6eb0db01a781d4f4940b34bd4c1cbe7e75263a (diff)
parentbcfb3806340939127d61f15ce101933bce6805f1 (diff)
downloadrust-175631311716d7dfeceec40d2587cde7142ffa8c.tar.gz
rust-175631311716d7dfeceec40d2587cde7142ffa8c.zip
Auto merge of #68101 - JohnTitor:rollup-mvmjukr, r=JohnTitor
Rollup of 8 pull requests

Successful merges:

 - #66045 (Add method Result::into_ok)
 - #67258 (Introduce `X..`, `..X`, and `..=X` range patterns)
 - #68014 (Unify output of "variant not found" errors)
 - #68019 (Build compiletest with in-tree libtest)
 - #68039 (remove explicit strip-hidden pass from compiler doc generation)
 - #68050 (Canonicalize rustc_error imports)
 - #68059 (Allow specifying LLVM args in target specifications)
 - #68075 (rustbuild: Cleanup book generation)

Failed merges:

 - #68089 (Unstabilize `Vec::remove_item`)

r? @ghost
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/result.rs38
-rw-r--r--src/libcore/tests/lib.rs2
-rw-r--r--src/libcore/tests/result.rs22
3 files changed, 62 insertions, 0 deletions
diff --git a/src/libcore/result.rs b/src/libcore/result.rs
index c6062493b86..aa57c7788c6 100644
--- a/src/libcore/result.rs
+++ b/src/libcore/result.rs
@@ -1092,6 +1092,44 @@ impl<T: Default, E> Result<T, E> {
     }
 }
 
+#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
+impl<T, E: Into<!>> Result<T, E> {
+    /// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`].
+    ///
+    /// Unlike [`unwrap`], this method is known to never panic on the
+    /// result types it is implemented for. Therefore, it can be used
+    /// instead of `unwrap` as a maintainability safeguard that will fail
+    /// to compile if the error type of the `Result` is later changed
+    /// to an error that can actually occur.
+    ///
+    /// [`Ok`]: enum.Result.html#variant.Ok
+    /// [`Err`]: enum.Result.html#variant.Err
+    /// [`unwrap`]: enum.Result.html#method.unwrap
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// # #![feature(never_type)]
+    /// # #![feature(unwrap_infallible)]
+    ///
+    /// fn only_good_news() -> Result<String, !> {
+    ///     Ok("this is fine".into())
+    /// }
+    ///
+    /// let s: String = only_good_news().into_ok();
+    /// println!("{}", s);
+    /// ```
+    #[inline]
+    pub fn into_ok(self) -> T {
+        match self {
+            Ok(x) => x,
+            Err(e) => e.into(),
+        }
+    }
+}
+
 #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
 impl<T: Deref, E> Result<T, E> {
     /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs
index 39f6133f2a6..09b54857f7d 100644
--- a/src/libcore/tests/lib.rs
+++ b/src/libcore/tests/lib.rs
@@ -40,6 +40,8 @@
 #![feature(slice_from_raw_parts)]
 #![feature(const_slice_from_raw_parts)]
 #![feature(const_raw_ptr_deref)]
+#![feature(never_type)]
+#![feature(unwrap_infallible)]
 
 extern crate test;
 
diff --git a/src/libcore/tests/result.rs b/src/libcore/tests/result.rs
index 499a5613bd2..254d4539eac 100644
--- a/src/libcore/tests/result.rs
+++ b/src/libcore/tests/result.rs
@@ -184,6 +184,28 @@ pub fn test_unwrap_or_default() {
 }
 
 #[test]
+pub fn test_into_ok() {
+    fn infallible_op() -> Result<isize, !> {
+        Ok(666)
+    }
+
+    assert_eq!(infallible_op().into_ok(), 666);
+
+    enum MyNeverToken {}
+    impl From<MyNeverToken> for ! {
+        fn from(never: MyNeverToken) -> ! {
+            match never {}
+        }
+    }
+
+    fn infallible_op2() -> Result<isize, MyNeverToken> {
+        Ok(667)
+    }
+
+    assert_eq!(infallible_op2().into_ok(), 667);
+}
+
+#[test]
 fn test_try() {
     fn try_result_some() -> Option<u8> {
         let val = Ok(1)?;