Skip to content

Commit b2df61f

Browse files
committed
Move Result::into_err
1 parent 778ca20 commit b2df61f

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
@@ -1211,6 +1211,43 @@ impl<T, E> Result<T, E> {
12111211
}
12121212
}
12131213

1214+
/// Returns the contained [`Err`] value, but never panics.
1215+
///
1216+
/// Unlike [`unwrap_err`], this method is known to never panic on the
1217+
/// result types it is implemented for. Therefore, it can be used
1218+
/// instead of `unwrap_err` as a maintainability safeguard that will fail
1219+
/// to compile if the ok type of the `Result` is later changed
1220+
/// to a type that can actually occur.
1221+
///
1222+
/// [`unwrap_err`]: Result::unwrap_err
1223+
///
1224+
/// # Examples
1225+
///
1226+
/// Basic usage:
1227+
///
1228+
/// ```
1229+
/// # #![feature(never_type)]
1230+
/// # #![feature(unwrap_infallible)]
1231+
///
1232+
/// fn only_bad_news() -> Result<!, String> {
1233+
/// Err("Oops, it failed".into())
1234+
/// }
1235+
///
1236+
/// let error: String = only_bad_news().into_err();
1237+
/// println!("{}", error);
1238+
/// ```
1239+
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
1240+
#[inline]
1241+
pub fn into_err(self) -> E
1242+
where
1243+
T: Into<!>,
1244+
{
1245+
match self {
1246+
Ok(x) => x.into(),
1247+
Err(e) => e,
1248+
}
1249+
}
1250+
12141251
////////////////////////////////////////////////////////////////////////
12151252
// Boolean operations on the values, eager and lazy
12161253
/////////////////////////////////////////////////////////////////////////
@@ -1536,42 +1573,6 @@ impl<T: Clone, E> Result<&mut T, E> {
15361573
}
15371574
}
15381575

1539-
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
1540-
impl<T: Into<!>, E> Result<T, E> {
1541-
/// Returns the contained [`Err`] value, but never panics.
1542-
///
1543-
/// Unlike [`unwrap_err`], this method is known to never panic on the
1544-
/// result types it is implemented for. Therefore, it can be used
1545-
/// instead of `unwrap_err` as a maintainability safeguard that will fail
1546-
/// to compile if the ok type of the `Result` is later changed
1547-
/// to a type that can actually occur.
1548-
///
1549-
/// [`unwrap_err`]: Result::unwrap_err
1550-
///
1551-
/// # Examples
1552-
///
1553-
/// Basic usage:
1554-
///
1555-
/// ```
1556-
/// # #![feature(never_type)]
1557-
/// # #![feature(unwrap_infallible)]
1558-
///
1559-
/// fn only_bad_news() -> Result<!, String> {
1560-
/// Err("Oops, it failed".into())
1561-
/// }
1562-
///
1563-
/// let error: String = only_bad_news().into_err();
1564-
/// println!("{}", error);
1565-
/// ```
1566-
#[inline]
1567-
pub fn into_err(self) -> E {
1568-
match self {
1569-
Ok(x) => x.into(),
1570-
Err(e) => e,
1571-
}
1572-
}
1573-
}
1574-
15751576
impl<T, E> Result<Option<T>, E> {
15761577
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
15771578
///

0 commit comments

Comments
 (0)