mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-01 01:23:26 +00:00
Move Result::expect_err and Result::unwrap_err
This commit is contained in:
parent
aa2aca2c8c
commit
06ea5ebe4e
@ -1114,6 +1114,66 @@ impl<T, E> Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the contained [`Err`] value, consuming the `self` value.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the value is an [`Ok`], with a panic message including the
|
||||
/// passed message, and the content of the [`Ok`].
|
||||
///
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```should_panic
|
||||
/// let x: Result<u32, &str> = Ok(10);
|
||||
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
|
||||
/// ```
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
#[stable(feature = "result_expect_err", since = "1.17.0")]
|
||||
pub fn expect_err(self, msg: &str) -> E
|
||||
where
|
||||
T: fmt::Debug,
|
||||
{
|
||||
match self {
|
||||
Ok(t) => unwrap_failed(msg, &t),
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the contained [`Err`] value, consuming the `self` value.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the value is an [`Ok`], with a custom panic message provided
|
||||
/// by the [`Ok`]'s value.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```should_panic
|
||||
/// let x: Result<u32, &str> = Ok(2);
|
||||
/// x.unwrap_err(); // panics with `2`
|
||||
/// ```
|
||||
///
|
||||
/// ```
|
||||
/// let x: Result<u32, &str> = Err("emergency failure");
|
||||
/// assert_eq!(x.unwrap_err(), "emergency failure");
|
||||
/// ```
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn unwrap_err(self) -> E
|
||||
where
|
||||
T: fmt::Debug,
|
||||
{
|
||||
match self {
|
||||
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t),
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Boolean operations on the values, eager and lazy
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
@ -1439,62 +1499,6 @@ impl<T: Clone, E> Result<&mut T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug, E> Result<T, E> {
|
||||
/// Returns the contained [`Err`] value, consuming the `self` value.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the value is an [`Ok`], with a panic message including the
|
||||
/// passed message, and the content of the [`Ok`].
|
||||
///
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```should_panic
|
||||
/// let x: Result<u32, &str> = Ok(10);
|
||||
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
|
||||
/// ```
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
#[stable(feature = "result_expect_err", since = "1.17.0")]
|
||||
pub fn expect_err(self, msg: &str) -> E {
|
||||
match self {
|
||||
Ok(t) => unwrap_failed(msg, &t),
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the contained [`Err`] value, consuming the `self` value.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the value is an [`Ok`], with a custom panic message provided
|
||||
/// by the [`Ok`]'s value.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```should_panic
|
||||
/// let x: Result<u32, &str> = Ok(2);
|
||||
/// x.unwrap_err(); // panics with `2`
|
||||
/// ```
|
||||
///
|
||||
/// ```
|
||||
/// let x: Result<u32, &str> = Err("emergency failure");
|
||||
/// assert_eq!(x.unwrap_err(), "emergency failure");
|
||||
/// ```
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn unwrap_err(self) -> E {
|
||||
match self {
|
||||
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t),
|
||||
Err(e) => 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.
|
||||
|
Loading…
Reference in New Issue
Block a user