Add Result::unwrap_infallible
Implementation of https://github.com/rust-lang/rfcs/pull/2799
This commit is contained in:
parent
6e9172f633
commit
a20013b129
1 changed files with 38 additions and 0 deletions
|
|
@ -1083,6 +1083,44 @@ impl<T: Default, E> Result<T, E> {
|
|||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
|
||||
impl<T, E: Into<!>> Result<T, E> {
|
||||
/// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`].
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/// [`Ok`]: enum.Result.html#variant.Ok
|
||||
/// [`Err`]: enum.Result.html#variant.Err
|
||||
/// [`unwrap`]: enum.Result.html#method.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().unwrap_infallible();
|
||||
/// println!("{}", s);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn unwrap_infallible(self) -> T {
|
||||
match self {
|
||||
Ok(x) => x,
|
||||
Err(e) => e.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T: Deref, E> Result<T, E> {
|
||||
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue