Auto merge of #31116 - bluss:expect-out-cold, r=alexcrichton
Use cold functions for panic formatting Option::expect, Result::unwrap, expect These methods are marked inline, but insert a big chunk of formatting code, as well as other error path related code, such as deallocating a std::io::Error if you have one. We can explicitly separate out that code path into a function that is never inline, since the panicking case should always be rare.
This commit is contained in:
commit
d63b8e539f
@ -295,7 +295,7 @@ impl<T> Option<T> {
|
||||
pub fn expect(self, msg: &str) -> T {
|
||||
match self {
|
||||
Some(val) => val,
|
||||
None => panic!("{}", msg),
|
||||
None => expect_failed(msg),
|
||||
}
|
||||
}
|
||||
|
||||
@ -697,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,8 +684,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
|
||||
pub fn unwrap(self) -> T {
|
||||
match self {
|
||||
Ok(t) => t,
|
||||
Err(e) =>
|
||||
panic!("called `Result::unwrap()` on an `Err` value: {:?}", e)
|
||||
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", e),
|
||||
}
|
||||
}
|
||||
|
||||
@ -706,7 +705,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
|
||||
pub fn expect(self, msg: &str) -> T {
|
||||
match self {
|
||||
Ok(t) => t,
|
||||
Err(e) => panic!("{}: {:?}", msg, e),
|
||||
Err(e) => unwrap_failed(msg, e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -734,13 +733,19 @@ 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) =>
|
||||
panic!("called `Result::unwrap_err()` on an `Ok` value: {:?}", t),
|
||||
Err(e) => e
|
||||
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", t),
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Trait implementations
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
Loading…
x
Reference in New Issue
Block a user