diff options
| author | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-01-22 18:19:00 +0100 |
|---|---|---|
| committer | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-01-22 19:07:11 +0100 |
| commit | 257bff3192e2c7313a4f8cfcac8839a573b42f6b (patch) | |
| tree | 6abe3be36f1a4c268b37d388636a426d072acd26 | |
| parent | 30be6a666d3918b3a149ff2c2c3242c9b1912655 (diff) | |
| download | rust-257bff3192e2c7313a4f8cfcac8839a573b42f6b.tar.gz rust-257bff3192e2c7313a4f8cfcac8839a573b42f6b.zip | |
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.
| -rw-r--r-- | src/libcore/option.rs | 16 | ||||
| -rw-r--r-- | src/libcore/result.rs | 30 |
2 files changed, 18 insertions, 28 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 7174661d281..eeb0c173b9b 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -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 ///////////////////////////////////////////////////////////////////////////// diff --git a/src/libcore/result.rs b/src/libcore/result.rs index b91cc1b3e6a..9bd6ed12798 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -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) } ///////////////////////////////////////////////////////////////////////////// |
