Skip to content

Commit

Permalink
Rollup merge of #127134 - tgross35:typeid-debug, r=Nilstrieb
Browse files Browse the repository at this point in the history
Print `TypeId` as a `u128` for `Debug`

Since <#121358>, `TypeId` is represented as a `(u64, u64)`. This also made the debug implementation a lot larger, which is especially apparent with pretty formatting.

Change this to convert the inner value back to a `u128` and then print as a tuple struct to make this less noisy.

Current:

    TypeId { t: (1403077013027291752, 4518903163082958039) }
    TypeId {
        t: (
            1403077013027291752,
            4518903163082958039,
        ),
    }

New:

    TypeId(25882202575019293479932656973818029271)
    TypeId(
        25882202575019293479932656973818029271,
    )
  • Loading branch information
matthiaskrgr authored Jun 30, 2024
2 parents da8bd26 + 682e7c1 commit 5d7da40
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ impl dyn Any + Send + Sync {
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
/// noting that the hashes and ordering will vary between Rust releases. Beware
/// of relying on them inside of your code!
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct TypeId {
// We avoid using `u128` because that imposes higher alignment requirements on many platforms.
Expand Down Expand Up @@ -644,6 +644,10 @@ impl TypeId {
let t2 = t as u64;
TypeId { t: (t1, t2) }
}

fn as_u128(self) -> u128 {
u128::from(self.t.0) << 64 | u128::from(self.t.1)
}
}

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

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Debug for TypeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_tuple("TypeId").field(&self.as_u128()).finish()
}
}

/// Returns the name of a type as a string slice.
///
/// # Note
Expand Down

0 comments on commit 5d7da40

Please sign in to comment.