Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,7 @@ impl<T: PointeeSized> 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 <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() {
if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::IS_UNIT {

@hanna-kruppe hanna-kruppe Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered just detecting ZST metadata, since it shouldn't make a difference and SizedTypeProperties::IS_ZST already exists, but detecting () specifically was requested in the PR that added this code: #135080 (comment)

View changes since the review

pointer_fmt_inner(ptr_addr, f)
} else {
wide_pointer_fmt_inner(ptr_addr, &core::ptr::metadata(ptr), f)
Expand Down
15 changes: 5 additions & 10 deletions library/core/src/unit.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,17 +21,10 @@ impl FromIterator<()> for () {
}

pub(crate) trait IsUnit {
fn is_unit() -> bool;
const IS_UNIT: bool;
}

impl<T: ?Sized> 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::<Self>() == type_id::<()>();

@hanna-kruppe hanna-kruppe Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this replaces use of specialization with use of non-'static TypeId. I'm not sure if that's better or worse as far as using unstable features in std is concerned. The change to the intrinsic that makes this work is relatively recent, and part of the experimental reflection work. On the other hand, non-'static TypeId is implementable on stable.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @oli-obk, who implemented non-'static TypeId in #152381.

Tracking issue: #146922

}
Loading