Skip to content

Commit 778ca20

Browse files
committed
Move Result::into_ok
1 parent 06ea5eb commit 778ca20

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

library/core/src/result.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,43 @@ impl<T, E> Result<T, E> {
11741174
}
11751175
}
11761176

1177+
/// Returns the contained [`Ok`] value, but never panics.
1178+
///
1179+
/// Unlike [`unwrap`], this method is known to never panic on the
1180+
/// result types it is implemented for. Therefore, it can be used
1181+
/// instead of `unwrap` as a maintainability safeguard that will fail
1182+
/// to compile if the error type of the `Result` is later changed
1183+
/// to an error that can actually occur.
1184+
///
1185+
/// [`unwrap`]: Result::unwrap
1186+
///
1187+
/// # Examples
1188+
///
1189+
/// Basic usage:
1190+
///
1191+
/// ```
1192+
/// # #![feature(never_type)]
1193+
/// # #![feature(unwrap_infallible)]
1194+
///
1195+
/// fn only_good_news() -> Result<String, !> {
1196+
/// Ok("this is fine".into())
1197+
/// }
1198+
///
1199+
/// let s: String = only_good_news().into_ok();
1200+
/// println!("{}", s);
1201+
/// ```
1202+
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
1203+
#[inline]
1204+
pub fn into_ok(self) -> T
1205+
where
1206+
E: Into<!>,
1207+
{
1208+
match self {
1209+
Ok(x) => x,
1210+
Err(e) => e.into(),
1211+
}
1212+
}
1213+
11771214
////////////////////////////////////////////////////////////////////////
11781215
// Boolean operations on the values, eager and lazy
11791216
/////////////////////////////////////////////////////////////////////////
@@ -1499,42 +1536,6 @@ impl<T: Clone, E> Result<&mut T, E> {
14991536
}
15001537
}
15011538

1502-
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
1503-
impl<T, E: Into<!>> Result<T, E> {
1504-
/// Returns the contained [`Ok`] value, but never panics.
1505-
///
1506-
/// Unlike [`unwrap`], this method is known to never panic on the
1507-
/// result types it is implemented for. Therefore, it can be used
1508-
/// instead of `unwrap` as a maintainability safeguard that will fail
1509-
/// to compile if the error type of the `Result` is later changed
1510-
/// to an error that can actually occur.
1511-
///
1512-
/// [`unwrap`]: Result::unwrap
1513-
///
1514-
/// # Examples
1515-
///
1516-
/// Basic usage:
1517-
///
1518-
/// ```
1519-
/// # #![feature(never_type)]
1520-
/// # #![feature(unwrap_infallible)]
1521-
///
1522-
/// fn only_good_news() -> Result<String, !> {
1523-
/// Ok("this is fine".into())
1524-
/// }
1525-
///
1526-
/// let s: String = only_good_news().into_ok();
1527-
/// println!("{}", s);
1528-
/// ```
1529-
#[inline]
1530-
pub fn into_ok(self) -> T {
1531-
match self {
1532-
Ok(x) => x,
1533-
Err(e) => e.into(),
1534-
}
1535-
}
1536-
}
1537-
15381539
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
15391540
impl<T: Into<!>, E> Result<T, E> {
15401541
/// Returns the contained [`Err`] value, but never panics.

0 commit comments

Comments
 (0)