Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit affa2f9

Browse files
committedJun 29, 2024
Print TypeId as a u128 for Debug
Since <rust-lang#121358>, `TypeId` is represented as a `(u64, u64)`. This also made the debug implementation a lot larger, which is especially apparent with pretty formatting. Make this less noisy by converting the inner value back to a `u128` then printing as a tuple struct. Current: TypeId { t: (1403077013027291752, 4518903163082958039) } TypeId { t: ( 1403077013027291752, 4518903163082958039, ), } New: TypeId(25882202575019293479932656973818029271) TypeId( 25882202575019293479932656973818029271, )
1 parent 171f5db commit affa2f9

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed
 

‎core/src/any.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl dyn Any + Send + Sync {
602602
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
603603
/// noting that the hashes and ordering will vary between Rust releases. Beware
604604
/// of relying on them inside of your code!
605-
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)]
605+
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
606606
#[stable(feature = "rust1", since = "1.0.0")]
607607
pub struct TypeId {
608608
// We avoid using `u128` because that imposes higher alignment requirements on many platforms.
@@ -644,6 +644,10 @@ impl TypeId {
644644
let t2 = t as u64;
645645
TypeId { t: (t1, t2) }
646646
}
647+
648+
fn as_u128(self) -> u128 {
649+
u128::from(self.t.0) << 64 | u128::from(self.t.1)
650+
}
647651
}
648652

649653
#[stable(feature = "rust1", since = "1.0.0")]
@@ -666,6 +670,13 @@ impl hash::Hash for TypeId {
666670
}
667671
}
668672

673+
#[stable(feature = "rust1", since = "1.0.0")]
674+
impl fmt::Debug for TypeId {
675+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
676+
f.debug_tuple("TypeId").field(&self.as_u128()).finish()
677+
}
678+
}
679+
669680
/// Returns the name of a type as a string slice.
670681
///
671682
/// # Note

0 commit comments

Comments
 (0)
Failed to load comments.