Skip to content

Commit 257bff3

Browse files
committed
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.
1 parent 30be6a6 commit 257bff3

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

src/libcore/option.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,10 @@ impl<T> Option<T> {
295295
pub fn expect(self, msg: &str) -> T {
296296
match self {
297297
Some(val) => val,
298-
None => Self::expect_failed(msg),
298+
None => expect_failed(msg),
299299
}
300300
}
301301

302-
#[inline(never)]
303-
#[cold]
304-
fn expect_failed(msg: &str) -> ! {
305-
panic!("{}", msg)
306-
}
307-
308302
/// Moves the value `v` out of the `Option<T>` if it is `Some(v)`.
309303
///
310304
/// # Panics
@@ -703,6 +697,14 @@ impl<T: Default> Option<T> {
703697
}
704698
}
705699

700+
// This is a separate function to reduce the code size of .expect() itself.
701+
#[inline(never)]
702+
#[cold]
703+
fn expect_failed(msg: &str) -> ! {
704+
panic!("{}", msg)
705+
}
706+
707+
706708
/////////////////////////////////////////////////////////////////////////////
707709
// Trait implementations
708710
/////////////////////////////////////////////////////////////////////////////

src/libcore/result.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -684,16 +684,10 @@ impl<T, E: fmt::Debug> Result<T, E> {
684684
pub fn unwrap(self) -> T {
685685
match self {
686686
Ok(t) => t,
687-
Err(e) => Self::unwrap_failed(e),
687+
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", e),
688688
}
689689
}
690690

691-
#[inline(never)]
692-
#[cold]
693-
fn unwrap_failed(error: E) -> ! {
694-
panic!("called `Result::unwrap()` on an `Err` value: {:?}", error)
695-
}
696-
697691
/// Unwraps a result, yielding the content of an `Ok`.
698692
///
699693
/// # Panics
@@ -711,15 +705,9 @@ impl<T, E: fmt::Debug> Result<T, E> {
711705
pub fn expect(self, msg: &str) -> T {
712706
match self {
713707
Ok(t) => t,
714-
Err(e) => Self::expect_failed(msg, e),
708+
Err(e) => unwrap_failed(msg, e),
715709
}
716710
}
717-
718-
#[inline(never)]
719-
#[cold]
720-
fn expect_failed(msg: &str, error: E) -> ! {
721-
panic!("{}: {:?}", msg, error)
722-
}
723711
}
724712

725713
impl<T: fmt::Debug, E> Result<T, E> {
@@ -745,17 +733,17 @@ impl<T: fmt::Debug, E> Result<T, E> {
745733
#[stable(feature = "rust1", since = "1.0.0")]
746734
pub fn unwrap_err(self) -> E {
747735
match self {
748-
Ok(t) => Self::unwrap_err_failed(t),
736+
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", t),
749737
Err(e) => e,
750738
}
751739
}
740+
}
752741

753-
#[inline(never)]
754-
#[cold]
755-
fn unwrap_err_failed(t: T) -> ! {
756-
panic!("called `Result::unwrap_err()` on an `Ok` value: {:?}", t)
757-
}
758-
742+
// This is a separate function to reduce the code size of the methods
743+
#[inline(never)]
744+
#[cold]
745+
fn unwrap_failed<E: fmt::Debug>(msg: &str, error: E) -> ! {
746+
panic!("{}: {:?}", msg, error)
759747
}
760748

761749
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)