|
153 | 153 | #![stable(feature = "rust1", since = "1.0.0")]
|
154 | 154 |
|
155 | 155 | use crate::fmt;
|
| 156 | +use crate::hash; |
156 | 157 | use crate::intrinsics;
|
157 | 158 |
|
158 | 159 | ///////////////////////////////////////////////////////////////////////////////
|
@@ -662,10 +663,10 @@ impl dyn Any + Send + Sync {
|
662 | 663 | /// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
|
663 | 664 | /// noting that the hashes and ordering will vary between Rust releases. Beware
|
664 | 665 | /// of relying on them inside of your code!
|
665 |
| -#[derive(Clone, Copy, Debug, Hash, Eq, PartialOrd, Ord)] |
| 666 | +#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)] |
666 | 667 | #[stable(feature = "rust1", since = "1.0.0")]
|
667 | 668 | pub struct TypeId {
|
668 |
| - t: u64, |
| 669 | + t: u128, |
669 | 670 | }
|
670 | 671 |
|
671 | 672 | #[stable(feature = "rust1", since = "1.0.0")]
|
@@ -696,7 +697,31 @@ impl TypeId {
|
696 | 697 | #[stable(feature = "rust1", since = "1.0.0")]
|
697 | 698 | #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
|
698 | 699 | pub const fn of<T: ?Sized + 'static>() -> TypeId {
|
699 |
| - TypeId { t: intrinsics::type_id::<T>() } |
| 700 | + #[cfg(bootstrap)] |
| 701 | + let t = intrinsics::type_id::<T>() as u128; |
| 702 | + #[cfg(not(bootstrap))] |
| 703 | + let t: u128 = intrinsics::type_id::<T>(); |
| 704 | + TypeId { t } |
| 705 | + } |
| 706 | +} |
| 707 | + |
| 708 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 709 | +impl hash::Hash for TypeId { |
| 710 | + #[inline] |
| 711 | + fn hash<H: hash::Hasher>(&self, state: &mut H) { |
| 712 | + // We only hash the lower 64 bits of our (128 bit) internal numeric ID, |
| 713 | + // because: |
| 714 | + // - The hashing algorithm which backs `TypeId` is expected to be |
| 715 | + // unbiased and high quality, meaning further mixing would be somewhat |
| 716 | + // redundant compared to choosing (the lower) 64 bits arbitrarily. |
| 717 | + // - `Hasher::finish` returns a u64 anyway, so the extra entropy we'd |
| 718 | + // get from hashing the full value would probably not be useful |
| 719 | + // (especially given the previous point about the lower 64 bits being |
| 720 | + // high quality on their own). |
| 721 | + // - It is correct to do so -- only hashing a subset of `self` is still |
| 722 | + // with an `Eq` implementation that considers the entire value, as |
| 723 | + // ours does. |
| 724 | + (self.t as u64).hash(state); |
700 | 725 | }
|
701 | 726 | }
|
702 | 727 |
|
|
0 commit comments