Skip to content

Commit cf5afc8

Browse files
committed
Auto merge of #66045 - mzabaluev:unwrap-infallible, r=sfackler
Add method Result::into_ok Implementation of rust-lang/rfcs#2799 Tracking issue #61695
2 parents 975e83a + 6f6848f commit cf5afc8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/libcore/result.rs

+38
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,44 @@ impl<T: Default, E> Result<T, E> {
10841084
}
10851085
}
10861086

1087+
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
1088+
impl<T, E: Into<!>> Result<T, E> {
1089+
/// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`].
1090+
///
1091+
/// Unlike [`unwrap`], this method is known to never panic on the
1092+
/// result types it is implemented for. Therefore, it can be used
1093+
/// instead of `unwrap` as a maintainability safeguard that will fail
1094+
/// to compile if the error type of the `Result` is later changed
1095+
/// to an error that can actually occur.
1096+
///
1097+
/// [`Ok`]: enum.Result.html#variant.Ok
1098+
/// [`Err`]: enum.Result.html#variant.Err
1099+
/// [`unwrap`]: enum.Result.html#method.unwrap
1100+
///
1101+
/// # Examples
1102+
///
1103+
/// Basic usage:
1104+
///
1105+
/// ```
1106+
/// # #![feature(never_type)]
1107+
/// # #![feature(unwrap_infallible)]
1108+
///
1109+
/// fn only_good_news() -> Result<String, !> {
1110+
/// Ok("this is fine".into())
1111+
/// }
1112+
///
1113+
/// let s: String = only_good_news().into_ok();
1114+
/// println!("{}", s);
1115+
/// ```
1116+
#[inline]
1117+
pub fn into_ok(self) -> T {
1118+
match self {
1119+
Ok(x) => x,
1120+
Err(e) => e.into(),
1121+
}
1122+
}
1123+
}
1124+
10871125
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
10881126
impl<T: Deref, E> Result<T, E> {
10891127
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.

src/libcore/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#![feature(iter_is_partitioned)]
3737
#![feature(iter_order_by)]
3838
#![feature(cmp_min_max_by)]
39+
#![feature(never_type)]
40+
#![feature(unwrap_infallible)]
3941

4042
extern crate test;
4143

src/libcore/tests/result.rs

+22
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,28 @@ pub fn test_unwrap_or_default() {
197197
assert_eq!(op2().unwrap_or_default(), 0);
198198
}
199199

200+
#[test]
201+
pub fn test_into_ok() {
202+
fn infallible_op() -> Result<isize, !> {
203+
Ok(666)
204+
}
205+
206+
assert_eq!(infallible_op().into_ok(), 666);
207+
208+
enum MyNeverToken {}
209+
impl From<MyNeverToken> for ! {
210+
fn from(never: MyNeverToken) -> ! {
211+
match never {}
212+
}
213+
}
214+
215+
fn infallible_op2() -> Result<isize, MyNeverToken> {
216+
Ok(667)
217+
}
218+
219+
assert_eq!(infallible_op2().into_ok(), 667);
220+
}
221+
200222
#[test]
201223
fn test_try() {
202224
fn try_result_some() -> Option<u8> {

0 commit comments

Comments
 (0)