diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 21e82d43a800c..0167be4d514c0 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -950,19 +950,19 @@ impl Error { where E: error::Error + Send + Sync + 'static, { - match self.repr.into_data() { - ErrorData::Custom(b) if b.error.is::() => { - let res = (*b).error.downcast::(); - - // downcast is a really trivial and is marked as inline, so - // it's likely be inlined here. - // - // And the compiler should be able to eliminate the branch - // that produces `Err` here since b.error.is::() - // returns true. - Ok(*res.unwrap()) + if let ErrorData::Custom(c) = self.repr.data() + && c.error.is::() + { + if let ErrorData::Custom(b) = self.repr.into_data() + && let Ok(err) = b.error.downcast::() + { + Ok(*err) + } else { + // Safety: We have just checked that the condition is true + unsafe { crate::hint::unreachable_unchecked() } } - repr_data => Err(Self { repr: Repr::new(repr_data) }), + } else { + Err(self) } } diff --git a/library/std/src/io/error/repr_bitpacked.rs b/library/std/src/io/error/repr_bitpacked.rs index 716da37168d01..7353816a8171b 100644 --- a/library/std/src/io/error/repr_bitpacked.rs +++ b/library/std/src/io/error/repr_bitpacked.rs @@ -133,15 +133,6 @@ unsafe impl Send for Repr {} unsafe impl Sync for Repr {} impl Repr { - pub(super) fn new(dat: ErrorData>) -> Self { - match dat { - ErrorData::Os(code) => Self::new_os(code), - ErrorData::Simple(kind) => Self::new_simple(kind), - ErrorData::SimpleMessage(simple_message) => Self::new_simple_message(simple_message), - ErrorData::Custom(b) => Self::new_custom(b), - } - } - pub(super) fn new_custom(b: Box) -> Self { let p = Box::into_raw(b).cast::(); // Should only be possible if an allocator handed out a pointer with diff --git a/library/std/src/io/error/repr_unpacked.rs b/library/std/src/io/error/repr_unpacked.rs index dc8a95577c959..b3e7b5f024ea0 100644 --- a/library/std/src/io/error/repr_unpacked.rs +++ b/library/std/src/io/error/repr_unpacked.rs @@ -10,9 +10,6 @@ pub(super) struct Repr(Inner); impl Repr { #[inline] - pub(super) fn new(dat: ErrorData>) -> Self { - Self(dat) - } pub(super) fn new_custom(b: Box) -> Self { Self(Inner::Custom(b)) }