@@ -1084,6 +1084,44 @@ impl<T: Default, E> Result<T, E> {
1084
1084
}
1085
1085
}
1086
1086
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
+
1087
1125
#[ unstable( feature = "inner_deref" , reason = "newly added" , issue = "50264" ) ]
1088
1126
impl < T : Deref , E > Result < T , E > {
1089
1127
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
0 commit comments