diff options
| author | David Tolnay <dtolnay@gmail.com> | 2021-12-30 10:28:23 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2021-12-30 10:28:23 -0800 |
| commit | 778ca204a6187eb9db99b06d1fa4c759e4e67f05 (patch) | |
| tree | 4e77e69ec0c2662dc033d31864553ab95b334f7b | |
| parent | 06ea5ebe4ef89cdb626930c4e8b235d430d47fee (diff) | |
| download | rust-778ca204a6187eb9db99b06d1fa4c759e4e67f05.tar.gz rust-778ca204a6187eb9db99b06d1fa4c759e4e67f05.zip | |
Move Result::into_ok
| -rw-r--r-- | library/core/src/result.rs | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 9cd949f7a12..6810e44d81e 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1174,6 +1174,43 @@ impl<T, E> Result<T, E> { } } + /// Returns the contained [`Ok`] value, but never panics. + /// + /// Unlike [`unwrap`], this method is known to never panic on the + /// result types it is implemented for. Therefore, it can be used + /// instead of `unwrap` as a maintainability safeguard that will fail + /// to compile if the error type of the `Result` is later changed + /// to an error that can actually occur. + /// + /// [`unwrap`]: Result::unwrap + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// # #![feature(never_type)] + /// # #![feature(unwrap_infallible)] + /// + /// fn only_good_news() -> Result<String, !> { + /// Ok("this is fine".into()) + /// } + /// + /// let s: String = only_good_news().into_ok(); + /// println!("{}", s); + /// ``` + #[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")] + #[inline] + pub fn into_ok(self) -> T + where + E: Into<!>, + { + match self { + Ok(x) => x, + Err(e) => e.into(), + } + } + //////////////////////////////////////////////////////////////////////// // Boolean operations on the values, eager and lazy ///////////////////////////////////////////////////////////////////////// @@ -1500,42 +1537,6 @@ impl<T: Clone, E> Result<&mut T, E> { } #[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")] -impl<T, E: Into<!>> Result<T, E> { - /// Returns the contained [`Ok`] value, but never panics. - /// - /// Unlike [`unwrap`], this method is known to never panic on the - /// result types it is implemented for. Therefore, it can be used - /// instead of `unwrap` as a maintainability safeguard that will fail - /// to compile if the error type of the `Result` is later changed - /// to an error that can actually occur. - /// - /// [`unwrap`]: Result::unwrap - /// - /// # Examples - /// - /// Basic usage: - /// - /// ``` - /// # #![feature(never_type)] - /// # #![feature(unwrap_infallible)] - /// - /// fn only_good_news() -> Result<String, !> { - /// Ok("this is fine".into()) - /// } - /// - /// let s: String = only_good_news().into_ok(); - /// println!("{}", s); - /// ``` - #[inline] - pub fn into_ok(self) -> T { - match self { - Ok(x) => x, - Err(e) => e.into(), - } - } -} - -#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")] impl<T: Into<!>, E> Result<T, E> { /// Returns the contained [`Err`] value, but never panics. /// |
