Skip to content

Commit 2dbcf08

Browse files
authored
Rollup merge of rust-lang#66045 - mzabaluev:unwrap-infallible, r=dtolnay
Add method Result::into_ok Implementation of rust-lang/rfcs#2799 Tracking issue rust-lang#61695
2 parents f795e8a + 6f0672c commit 2dbcf08

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
@@ -1092,6 +1092,44 @@ impl<T: Default, E> Result<T, E> {
10921092
}
10931093
}
10941094

1095+
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
1096+
impl<T, E: Into<!>> Result<T, E> {
1097+
/// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`].
1098+
///
1099+
/// Unlike [`unwrap`], this method is known to never panic on the
1100+
/// result types it is implemented for. Therefore, it can be used
1101+
/// instead of `unwrap` as a maintainability safeguard that will fail
1102+
/// to compile if the error type of the `Result` is later changed
1103+
/// to an error that can actually occur.
1104+
///
1105+
/// [`Ok`]: enum.Result.html#variant.Ok
1106+
/// [`Err`]: enum.Result.html#variant.Err
1107+
/// [`unwrap`]: enum.Result.html#method.unwrap
1108+
///
1109+
/// # Examples
1110+
///
1111+
/// Basic usage:
1112+
///
1113+
/// ```
1114+
/// # #![feature(never_type)]
1115+
/// # #![feature(unwrap_infallible)]
1116+
///
1117+
/// fn only_good_news() -> Result<String, !> {
1118+
/// Ok("this is fine".into())
1119+
/// }
1120+
///
1121+
/// let s: String = only_good_news().into_ok();
1122+
/// println!("{}", s);
1123+
/// ```
1124+
#[inline]
1125+
pub fn into_ok(self) -> T {
1126+
match self {
1127+
Ok(x) => x,
1128+
Err(e) => e.into(),
1129+
}
1130+
}
1131+
}
1132+
10951133
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
10961134
impl<T: Deref, E> Result<T, E> {
10971135
/// 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
@@ -40,6 +40,8 @@
4040
#![feature(slice_from_raw_parts)]
4141
#![feature(const_slice_from_raw_parts)]
4242
#![feature(const_raw_ptr_deref)]
43+
#![feature(never_type)]
44+
#![feature(unwrap_infallible)]
4345

4446
extern crate test;
4547

src/libcore/tests/result.rs

+22
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ pub fn test_unwrap_or_default() {
183183
assert_eq!(op2().unwrap_or_default(), 0);
184184
}
185185

186+
#[test]
187+
pub fn test_into_ok() {
188+
fn infallible_op() -> Result<isize, !> {
189+
Ok(666)
190+
}
191+
192+
assert_eq!(infallible_op().into_ok(), 666);
193+
194+
enum MyNeverToken {}
195+
impl From<MyNeverToken> for ! {
196+
fn from(never: MyNeverToken) -> ! {
197+
match never {}
198+
}
199+
}
200+
201+
fn infallible_op2() -> Result<isize, MyNeverToken> {
202+
Ok(667)
203+
}
204+
205+
assert_eq!(infallible_op2().into_ok(), 667);
206+
}
207+
186208
#[test]
187209
fn test_try() {
188210
fn try_result_some() -> Option<u8> {

0 commit comments

Comments
 (0)