@@ -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" ) ]
10961134impl < T : Deref , E > Result < T , E > {
10971135 /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
0 commit comments