Skip to content

Commit 9907583

Browse files
Stop leaking implementation detail in Debug impls of opaque types
This changes the `Debug` impl of: - `core::any::TypeId` - `core::mem::Discriminant` - `std::thread::ThreadId` - `std::time::Instant` All these types are explicitly described as "opaque" in the documentation, so they shouldn't print implementation details in their `Debug` implementation. The change for `ThreadId` can possibly be reverted in the future, once the method `thread_id_value` is stabilized: rust-lang#67939 The same goes for `Discriminant` once/if the `DiscriminantKind` trait gets stabilized. But until then, these are opaque types.
1 parent 15598a8 commit 9907583

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

library/core/src/any.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl dyn Any + Send + Sync {
435435
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
436436
/// noting that the hashes and ordering will vary between Rust releases. Beware
437437
/// of relying on them inside of your code!
438-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
438+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
439439
#[stable(feature = "rust1", since = "1.0.0")]
440440
pub struct TypeId {
441441
t: u64,
@@ -464,6 +464,13 @@ impl TypeId {
464464
}
465465
}
466466

467+
#[stable(feature = "rust1", since = "1.0.0")]
468+
impl fmt::Debug for TypeId {
469+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470+
f.pad("TypeId(_)")
471+
}
472+
}
473+
467474
/// Returns the name of a type as a string slice.
468475
///
469476
/// # Note

library/core/src/mem/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ impl<T> hash::Hash for Discriminant<T> {
988988
#[stable(feature = "discriminant_value", since = "1.21.0")]
989989
impl<T> fmt::Debug for Discriminant<T> {
990990
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
991-
fmt.debug_tuple("Discriminant").field(&self.0).finish()
991+
fmt.pad("Discriminant(_)")
992992
}
993993
}
994994

library/std/src/thread/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ pub fn park_timeout(dur: Duration) {
985985
///
986986
/// [`id`]: Thread::id
987987
#[stable(feature = "thread_id", since = "1.19.0")]
988-
#[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
988+
#[derive(Eq, PartialEq, Clone, Copy, Hash)]
989989
pub struct ThreadId(NonZeroU64);
990990

991991
impl ThreadId {
@@ -1025,6 +1025,13 @@ impl ThreadId {
10251025
}
10261026
}
10271027

1028+
#[stable(feature = "thread_id", since = "1.19.0")]
1029+
impl fmt::Debug for ThreadId {
1030+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1031+
f.pad("ThreadId(_)")
1032+
}
1033+
}
1034+
10281035
////////////////////////////////////////////////////////////////////////////////
10291036
// Thread
10301037
////////////////////////////////////////////////////////////////////////////////

library/std/src/time.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ impl Sub<Instant> for Instant {
410410
#[stable(feature = "time2", since = "1.8.0")]
411411
impl fmt::Debug for Instant {
412412
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
413-
self.0.fmt(f)
413+
// Instant is an opaque type and only useful with `Duration`. So it
414+
// would be misleading to show implementation detail in the output.
415+
f.pad("Instant(_)")
414416
}
415417
}
416418

0 commit comments

Comments
 (0)