From 257bff3192e2c7313a4f8cfcac8839a573b42f6b Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Fri, 22 Jan 2016 18:19:00 +0100 Subject: [PATCH] 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. --- src/libcore/option.rs | 16 +++++++++------- 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 Option { 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` if it is `Some(v)`. /// /// # Panics @@ -703,6 +697,14 @@ impl Option { } } +// 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 Result { 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 Result { 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 Result { @@ -745,17 +733,17 @@ impl Result { #[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(msg: &str, error: E) -> ! { + panic!("{}: {:?}", msg, error) } /////////////////////////////////////////////////////////////////////////////