Make TypeId::hash
more robust in case of upstream rustc changes (#11334)
Based on discussion after #11268 was merged: Instead of panicking should the impl of `TypeId::hash` change significantly, have a fallback and detect this in a test.
This commit is contained in:
parent
03404c48ca
commit
cd12e7c836
@ -67,11 +67,13 @@ impl std::hash::Hasher for NoOpTypeIdHasher {
|
|||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self, _bytes: &[u8]) {
|
fn write(&mut self, bytes: &[u8]) {
|
||||||
// This will never be called: TypeId always just calls write_u64 once!
|
// This will never be called: TypeId always just calls write_u64 once!
|
||||||
// This is unlikely to ever change, but as it isn't officially guaranteed,
|
// This is a known trick and unlikely to change, but isn't officially guaranteed.
|
||||||
// panicking will let us detect this as fast as possible.
|
// Don't break applications (slower fallback, just check in test):
|
||||||
unimplemented!("Hashing of std::any::TypeId changed");
|
self.0 = bytes.iter().fold(self.0, |hash, b| {
|
||||||
|
hash.rotate_left(8).wrapping_add(*b as u64)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_u64(&mut self, i: u64) {
|
fn write_u64(&mut self, i: u64) {
|
||||||
@ -1750,6 +1752,23 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fast_typeid_hash() {
|
||||||
|
struct Hasher;
|
||||||
|
|
||||||
|
impl std::hash::Hasher for Hasher {
|
||||||
|
fn finish(&self) -> u64 {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
fn write(&mut self, _: &[u8]) {
|
||||||
|
panic!("Hashing of std::any::TypeId changed");
|
||||||
|
}
|
||||||
|
fn write_u64(&mut self, _: u64) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::hash::Hash::hash(&TypeId::of::<()>(), &mut Hasher);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct ComponentA(u32);
|
struct ComponentA(u32);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user