From a20013b129c10907aee0bfb5bf1cac387e48eb51 Mon Sep 17 00:00:00 2001 From: Mikhail Zabaluev Date: Sat, 2 Nov 2019 20:13:32 +0200 Subject: [PATCH 1/3] Add Result::unwrap_infallible Implementation of https://github.com/rust-lang/rfcs/pull/2799 --- src/libcore/result.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index fb4dc62d8c176..ed3b37ad2a57b 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -1083,6 +1083,44 @@ impl Result { } } +#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")] +impl> Result { + /// 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 { + /// Ok("this is fine".into()) + /// } + /// + /// let s: String = only_good_news().unwrap_infallible(); + /// println!("{}", s); + /// ``` + #[inline] + pub fn unwrap_infallible(self) -> T { + match self { + Ok(x) => x, + Err(e) => e.into(), + } + } +} + #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] impl Result { /// Converts from `Result` (or `&Result`) to `Result<&T::Target, &E>`. From 9a99a2159bb57ee0f1a52bb3f989a903df2f8cb4 Mon Sep 17 00:00:00 2001 From: Mikhail Zabaluev Date: Sat, 2 Nov 2019 22:12:51 +0200 Subject: [PATCH 2/3] libcore: test Result::unwrap_infallible --- src/libcore/tests/lib.rs | 2 ++ src/libcore/tests/result.rs | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 1f20ebc01e993..4656024241804 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -39,6 +39,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 163f8d0ab3797..8d17790af5620 100644 --- a/src/libcore/tests/result.rs +++ b/src/libcore/tests/result.rs @@ -197,6 +197,28 @@ pub fn test_unwrap_or_default() { assert_eq!(op2().unwrap_or_default(), 0); } +#[test] +pub fn test_unwrap_infallible() { + fn infallible_op() -> Result { + Ok(666) + } + + assert_eq!(infallible_op().unwrap_infallible(), 666); + + enum MyNeverToken {} + impl From for ! { + fn from(never: MyNeverToken) -> ! { + match never {} + } + } + + fn infallible_op2() -> Result { + Ok(667) + } + + assert_eq!(infallible_op2().unwrap_infallible(), 667); +} + #[test] fn test_try() { fn try_result_some() -> Option { From 6f0672c08b7609c7ed77245a3feea3040221b804 Mon Sep 17 00:00:00 2001 From: Mikhail Zabaluev Date: Mon, 4 Nov 2019 14:05:15 +0200 Subject: [PATCH 3/3] Rename Result::unwrap_infallible to into_ok --- src/libcore/result.rs | 4 ++-- src/libcore/tests/result.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index ed3b37ad2a57b..ea2dd77f4efbb 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -1109,11 +1109,11 @@ impl> Result { /// Ok("this is fine".into()) /// } /// - /// let s: String = only_good_news().unwrap_infallible(); + /// let s: String = only_good_news().into_ok(); /// println!("{}", s); /// ``` #[inline] - pub fn unwrap_infallible(self) -> T { + pub fn into_ok(self) -> T { match self { Ok(x) => x, Err(e) => e.into(), diff --git a/src/libcore/tests/result.rs b/src/libcore/tests/result.rs index 8d17790af5620..cac15a6b32414 100644 --- a/src/libcore/tests/result.rs +++ b/src/libcore/tests/result.rs @@ -198,12 +198,12 @@ pub fn test_unwrap_or_default() { } #[test] -pub fn test_unwrap_infallible() { +pub fn test_into_ok() { fn infallible_op() -> Result { Ok(666) } - assert_eq!(infallible_op().unwrap_infallible(), 666); + assert_eq!(infallible_op().into_ok(), 666); enum MyNeverToken {} impl From for ! { @@ -216,7 +216,7 @@ pub fn test_unwrap_infallible() { Ok(667) } - assert_eq!(infallible_op2().unwrap_infallible(), 667); + assert_eq!(infallible_op2().into_ok(), 667); } #[test]