mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-23 04:14:28 +00:00
Move cold panic functions in Option and Result
Move functions out of their impl blocks; avoids unnecessary monomorphization by type parameters that are irrelevant to the message output.
This commit is contained in:
parent
30be6a666d
commit
257bff3192
@ -295,16 +295,10 @@ impl<T> Option<T> {
|
||||
pub fn expect(self, msg: &str) -> T {
|
||||
match self {
|
||||
Some(val) => val,
|
||||
None => Self::expect_failed(msg),
|
||||
None => expect_failed(msg),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
fn expect_failed(msg: &str) -> ! {
|
||||
panic!("{}", msg)
|
||||
}
|
||||
|
||||
/// Moves the value `v` out of the `Option<T>` if it is `Some(v)`.
|
||||
///
|
||||
/// # Panics
|
||||
@ -703,6 +697,14 @@ impl<T: Default> Option<T> {
|
||||
}
|
||||
}
|
||||
|
||||
// This is a separate function to reduce the code size of .expect() itself.
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
fn expect_failed(msg: &str) -> ! {
|
||||
panic!("{}", msg)
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Trait implementations
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -684,16 +684,10 @@ impl<T, E: fmt::Debug> Result<T, E> {
|
||||
pub fn unwrap(self) -> T {
|
||||
match self {
|
||||
Ok(t) => t,
|
||||
Err(e) => Self::unwrap_failed(e),
|
||||
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
fn unwrap_failed(error: E) -> ! {
|
||||
panic!("called `Result::unwrap()` on an `Err` value: {:?}", error)
|
||||
}
|
||||
|
||||
/// Unwraps a result, yielding the content of an `Ok`.
|
||||
///
|
||||
/// # Panics
|
||||
@ -711,15 +705,9 @@ impl<T, E: fmt::Debug> Result<T, E> {
|
||||
pub fn expect(self, msg: &str) -> T {
|
||||
match self {
|
||||
Ok(t) => t,
|
||||
Err(e) => Self::expect_failed(msg, e),
|
||||
Err(e) => unwrap_failed(msg, e),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
fn expect_failed(msg: &str, error: E) -> ! {
|
||||
panic!("{}: {:?}", msg, error)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug, E> Result<T, E> {
|
||||
@ -745,17 +733,17 @@ impl<T: fmt::Debug, E> Result<T, E> {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn unwrap_err(self) -> E {
|
||||
match self {
|
||||
Ok(t) => Self::unwrap_err_failed(t),
|
||||
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", t),
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
fn unwrap_err_failed(t: T) -> ! {
|
||||
panic!("called `Result::unwrap_err()` on an `Ok` value: {:?}", t)
|
||||
}
|
||||
|
||||
// This is a separate function to reduce the code size of the methods
|
||||
#[inline(never)]
|
||||
#[cold]
|
||||
fn unwrap_failed<E: fmt::Debug>(msg: &str, error: E) -> ! {
|
||||
panic!("{}: {:?}", msg, error)
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
Loading…
Reference in New Issue
Block a user