diff options
| author | bors <bors@rust-lang.org> | 2016-01-23 00:33:23 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-01-23 00:33:23 +0000 |
| commit | d63b8e539ff1605cfe2ea3c1893139bd77e3bb8d (patch) | |
| tree | bb8ebf89ac6a196ab35e9dfc05cfece1841be41a /src/libcore | |
| parent | c2740b6adbd219a17efb66a3f75d78404ab715b6 (diff) | |
| parent | 257bff3192e2c7313a4f8cfcac8839a573b42f6b (diff) | |
| download | rust-d63b8e539ff1605cfe2ea3c1893139bd77e3bb8d.tar.gz rust-d63b8e539ff1605cfe2ea3c1893139bd77e3bb8d.zip | |
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.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/option.rs | 10 | ||||
| -rw-r--r-- | src/libcore/result.rs | 17 |
2 files changed, 20 insertions, 7 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 8d40faf3bc6..eeb0c173b9b 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -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 ///////////////////////////////////////////////////////////////////////////// diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 6ec76c821b3..9bd6ed12798 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -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 ///////////////////////////////////////////////////////////////////////////// |
