diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index c4bab254c146c..80b607f4834c9 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -3000,7 +3000,7 @@ impl Pointer for *const T { // metadata type to reduce the amount of codegen work needed for each distinct type. let ptr: *const T = *self; let ptr_addr = ptr.expose_provenance(); - if <::Metadata as core::unit::IsUnit>::is_unit() { + if <::Metadata as core::unit::IsUnit>::IS_UNIT { pointer_fmt_inner(ptr_addr, f) } else { wide_pointer_fmt_inner(ptr_addr, &core::ptr::metadata(ptr), f) diff --git a/library/core/src/unit.rs b/library/core/src/unit.rs index d54816c444bc4..5d9c79538e371 100644 --- a/library/core/src/unit.rs +++ b/library/core/src/unit.rs @@ -1,3 +1,5 @@ +use crate::intrinsics::type_id; + /// Collapses all unit items from an iterator into one. /// /// This is more useful when combined with higher-level abstractions, like @@ -19,17 +21,10 @@ impl FromIterator<()> for () { } pub(crate) trait IsUnit { - fn is_unit() -> bool; + const IS_UNIT: bool; } impl IsUnit for T { - default fn is_unit() -> bool { - false - } -} - -impl IsUnit for () { - fn is_unit() -> bool { - true - } + // `type_id` erases lifetimes, but that's OK here because "is it ()" never depends on lifetimes + const IS_UNIT: bool = type_id::() == type_id::<()>(); }