@@ -1083,6 +1083,44 @@ impl<T: Default, E> Result<T, E> {
10831083 }
10841084}
10851085
1086+ #[ unstable( feature = "unwrap_infallible" , reason = "newly added" , issue = "61695" ) ]
1087+ impl < T , E : Into < !> > Result < T , E > {
1088+ /// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`].
1089+ ///
1090+ /// Unlike [`unwrap`], this method is known to never panic on the
1091+ /// result types it is implemented for. Therefore, it can be used
1092+ /// instead of `unwrap` as a maintainability safeguard that will fail
1093+ /// to compile if the error type of the `Result` is later changed
1094+ /// to an error that can actually occur.
1095+ ///
1096+ /// [`Ok`]: enum.Result.html#variant.Ok
1097+ /// [`Err`]: enum.Result.html#variant.Err
1098+ /// [`unwrap`]: enum.Result.html#method.unwrap
1099+ ///
1100+ /// # Examples
1101+ ///
1102+ /// Basic usage:
1103+ ///
1104+ /// ```
1105+ /// # #![feature(never_type)]
1106+ /// # #![feature(unwrap_infallible)]
1107+ ///
1108+ /// fn only_good_news() -> Result<String, !> {
1109+ /// Ok("this is fine".into())
1110+ /// }
1111+ ///
1112+ /// let s: String = only_good_news().unwrap_infallible();
1113+ /// println!("{}", s);
1114+ /// ```
1115+ #[ inline]
1116+ pub fn unwrap_infallible ( self ) -> T {
1117+ match self {
1118+ Ok ( x) => x,
1119+ Err ( e) => e. into ( ) ,
1120+ }
1121+ }
1122+ }
1123+
10861124#[ unstable( feature = "inner_deref" , reason = "newly added" , issue = "50264" ) ]
10871125impl < T : Deref , E > Result < T , E > {
10881126 /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
0 commit comments