From f688bee4ece7b16e8e77b2f4217a8345da742733 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 31 Dec 2020 18:17:25 -0800 Subject: [PATCH 1/4] Add a `Result::ok_or_err` method to extract a `T` from `Result` --- library/core/src/result.rs | 35 +++++++++++++++++++++++++++++++++++ library/core/tests/lib.rs | 1 + library/core/tests/result.rs | 9 +++++++++ 3 files changed, 45 insertions(+) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index a43ba5882edcd..32074c41b8b12 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1276,6 +1276,41 @@ impl Result, E> { } } +impl Result { + /// Returns the [`Ok`] value if `self` is `Ok`, and the [`Err`] value if + /// `self` is `Err`. + /// + /// In other words, this function returns the value (the `T`) of a + /// `Result`, regardless of whether or not that result is `Ok` or + /// `Err`. + /// + /// This can be useful in conjunction with APIs such as + /// [`Atomic*::compare_exchange`], or [`slice::binary_search`][binary_search], but only in + /// cases where you don't care if the result was `Ok` or not. + /// + /// [`Atomic*::compare_exchange`]: crate::sync::atomic::AtomicBool::compare_exchange + /// [binary_search]: ../primitive.slice.html#method.binary_search + /// + /// # Examples + /// + /// ``` + /// #![feature(ok_or_err)] + /// let ok: Result = Ok(3); + /// let err: Result = Err(4); + /// + /// assert_eq!(ok.ok_or_err(), 3); + /// assert_eq!(err.ok_or_err(), 4); + /// ``` + #[inline] + #[unstable(feature = "ok_or_err", reason = "newly added", issue = "none")] + pub const fn ok_or_err(self) -> T { + match self { + Ok(v) => v, + Err(v) => v, + } + } +} + // This is a separate function to reduce the code size of the methods #[inline(never)] #[cold] diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 40dc6473b7d40..5cd6e9efd9eed 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -65,6 +65,7 @@ #![feature(never_type)] #![feature(unwrap_infallible)] #![feature(option_result_unwrap_unchecked)] +#![feature(ok_or_err)] #![feature(option_unwrap_none)] #![feature(peekable_peek_mut)] #![feature(once_cell)] diff --git a/library/core/tests/result.rs b/library/core/tests/result.rs index 7aa44c6e593b3..53087eec499df 100644 --- a/library/core/tests/result.rs +++ b/library/core/tests/result.rs @@ -95,6 +95,15 @@ fn test_unwrap_or() { assert_eq!(ok_err.unwrap_or(50), 50); } +#[test] +fn test_ok_or_err() { + let ok: Result = Ok(100); + let err: Result = Err(200); + + assert_eq!(ok.ok_or_err(), 100); + assert_eq!(err.ok_or_err(), 200); +} + #[test] fn test_unwrap_or_else() { fn handler(msg: &'static str) -> isize { From 7d303661cdfbf96768b341f500acde83dc052342 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 31 Dec 2020 19:50:24 -0800 Subject: [PATCH 2/4] Fix doc link for slice::binary_search --- library/core/src/result.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 32074c41b8b12..bbbf1b412a332 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1289,7 +1289,7 @@ impl Result { /// cases where you don't care if the result was `Ok` or not. /// /// [`Atomic*::compare_exchange`]: crate::sync::atomic::AtomicBool::compare_exchange - /// [binary_search]: ../primitive.slice.html#method.binary_search + /// [binary_search]: ../../std/primitive.slice.html#method.binary_search /// /// # Examples /// From 2711b011e6b2c06c6f289a8a2362c3af31dbc32c Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Wed, 17 Feb 2021 08:54:52 -0800 Subject: [PATCH 3/4] Rename Result::ok_or_err to Result::into_ok_or_err --- library/core/src/result.rs | 10 +++++----- library/core/tests/lib.rs | 2 +- library/core/tests/result.rs | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index bbbf1b412a332..9cfa7b6211a19 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1294,16 +1294,16 @@ impl Result { /// # Examples /// /// ``` - /// #![feature(ok_or_err)] + /// #![feature(result_into_ok_or_err)] /// let ok: Result = Ok(3); /// let err: Result = Err(4); /// - /// assert_eq!(ok.ok_or_err(), 3); - /// assert_eq!(err.ok_or_err(), 4); + /// assert_eq!(ok.into_ok_or_err(), 3); + /// assert_eq!(err.into_ok_or_err(), 4); /// ``` #[inline] - #[unstable(feature = "ok_or_err", reason = "newly added", issue = "none")] - pub const fn ok_or_err(self) -> T { + #[unstable(feature = "result_into_ok_or_err", reason = "newly added", issue = "none")] + pub const fn into_ok_or_err(self) -> T { match self { Ok(v) => v, Err(v) => v, diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 5cd6e9efd9eed..34e05760db259 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -65,7 +65,7 @@ #![feature(never_type)] #![feature(unwrap_infallible)] #![feature(option_result_unwrap_unchecked)] -#![feature(ok_or_err)] +#![feature(result_into_ok_or_err)] #![feature(option_unwrap_none)] #![feature(peekable_peek_mut)] #![feature(once_cell)] diff --git a/library/core/tests/result.rs b/library/core/tests/result.rs index 53087eec499df..5fcd7b4d3a327 100644 --- a/library/core/tests/result.rs +++ b/library/core/tests/result.rs @@ -100,8 +100,8 @@ fn test_ok_or_err() { let ok: Result = Ok(100); let err: Result = Err(200); - assert_eq!(ok.ok_or_err(), 100); - assert_eq!(err.ok_or_err(), 200); + assert_eq!(ok.into_ok_or_err(), 100); + assert_eq!(err.into_ok_or_err(), 200); } #[test] From 404da0bc901b92c2bf74a3c84fb0bd52cbf7f934 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Wed, 17 Feb 2021 09:04:03 -0800 Subject: [PATCH 4/4] Add link to tracking issue #82223 --- library/core/src/result.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 9cfa7b6211a19..d8747f8b8d6dc 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1302,7 +1302,7 @@ impl Result { /// assert_eq!(err.into_ok_or_err(), 4); /// ``` #[inline] - #[unstable(feature = "result_into_ok_or_err", reason = "newly added", issue = "none")] + #[unstable(feature = "result_into_ok_or_err", reason = "newly added", issue = "82223")] pub const fn into_ok_or_err(self) -> T { match self { Ok(v) => v,