Skip to content

Commit 40e3af5

Browse files
authored
Rollup merge of #80572 - thomcc:ok_or_err, r=m-ou-se
Add a `Result::into_ok_or_err` method to extract a `T` from `Result<T, T>` When updating code to handle the semi-recent deprecation of `compare_and_swap` in favor of `compare_exchange`, which returns `Result<T, T>`, I wanted this. I've also wanted it with code using `slice::binary_search` before. The name (and perhaps the documentation) is the hardest part here, but this name seems consistent with the other Result methods, and equivalently memorable.
2 parents db59950 + 404da0b commit 40e3af5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

library/core/src/result.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,41 @@ impl<T, E> Result<Result<T, E>, E> {
12761276
}
12771277
}
12781278

1279+
impl<T> Result<T, T> {
1280+
/// Returns the [`Ok`] value if `self` is `Ok`, and the [`Err`] value if
1281+
/// `self` is `Err`.
1282+
///
1283+
/// In other words, this function returns the value (the `T`) of a
1284+
/// `Result<T, T>`, regardless of whether or not that result is `Ok` or
1285+
/// `Err`.
1286+
///
1287+
/// This can be useful in conjunction with APIs such as
1288+
/// [`Atomic*::compare_exchange`], or [`slice::binary_search`][binary_search], but only in
1289+
/// cases where you don't care if the result was `Ok` or not.
1290+
///
1291+
/// [`Atomic*::compare_exchange`]: crate::sync::atomic::AtomicBool::compare_exchange
1292+
/// [binary_search]: ../../std/primitive.slice.html#method.binary_search
1293+
///
1294+
/// # Examples
1295+
///
1296+
/// ```
1297+
/// #![feature(result_into_ok_or_err)]
1298+
/// let ok: Result<u32, u32> = Ok(3);
1299+
/// let err: Result<u32, u32> = Err(4);
1300+
///
1301+
/// assert_eq!(ok.into_ok_or_err(), 3);
1302+
/// assert_eq!(err.into_ok_or_err(), 4);
1303+
/// ```
1304+
#[inline]
1305+
#[unstable(feature = "result_into_ok_or_err", reason = "newly added", issue = "82223")]
1306+
pub const fn into_ok_or_err(self) -> T {
1307+
match self {
1308+
Ok(v) => v,
1309+
Err(v) => v,
1310+
}
1311+
}
1312+
}
1313+
12791314
// This is a separate function to reduce the code size of the methods
12801315
#[inline(never)]
12811316
#[cold]

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#![feature(never_type)]
6666
#![feature(unwrap_infallible)]
6767
#![feature(option_result_unwrap_unchecked)]
68+
#![feature(result_into_ok_or_err)]
6869
#![feature(option_unwrap_none)]
6970
#![feature(peekable_peek_mut)]
7071
#![feature(once_cell)]

library/core/tests/result.rs

+9
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ fn test_unwrap_or() {
9595
assert_eq!(ok_err.unwrap_or(50), 50);
9696
}
9797

98+
#[test]
99+
fn test_ok_or_err() {
100+
let ok: Result<isize, isize> = Ok(100);
101+
let err: Result<isize, isize> = Err(200);
102+
103+
assert_eq!(ok.into_ok_or_err(), 100);
104+
assert_eq!(err.into_ok_or_err(), 200);
105+
}
106+
98107
#[test]
99108
fn test_unwrap_or_else() {
100109
fn handler(msg: &'static str) -> isize {

0 commit comments

Comments
 (0)