Fix order of ptr::copy_nonoverlapping parameters.

Clippy helped me spot this. It didn’t cause any bugs, just bad
performance as all keys would hash to 0 and thus end up in the same
bucket.
This commit is contained in:
Chris Morgan 2016-03-07 00:11:37 +11:00
parent f1ea6f1cf9
commit 724f94758d

View file

@ -24,7 +24,7 @@ impl Hasher for TypeIdHasher {
// This expects to receive one and exactly one 64-bit value // This expects to receive one and exactly one 64-bit value
debug_assert!(bytes.len() == 8); debug_assert!(bytes.len() == 8);
unsafe { unsafe {
ptr::copy_nonoverlapping(&mut self.value, mem::transmute(&bytes[0]), 1) ptr::copy_nonoverlapping(mem::transmute(&bytes[0]), &mut self.value, 1)
} }
} }
@ -32,6 +32,21 @@ impl Hasher for TypeIdHasher {
fn finish(&self) -> u64 { self.value } fn finish(&self) -> u64 { self.value }
} }
#[test]
fn type_id_hasher() {
fn verify_hashing_with(type_id: TypeId) {
let mut hasher = TypeIdHasher::default();
type_id.hash(&mut hasher);
assert_eq!(hasher.finish(), unsafe { mem::transmute::<TypeId, u64>(type_id) });
}
// Pick a variety of types, just to demonstrate its all sane. Normal, zero-sized, unsized, &c.
verify_hashing_with(TypeId::of::<usize>());
verify_hashing_with(TypeId::of::<()>());
verify_hashing_with(TypeId::of::<str>());
verify_hashing_with(TypeId::of::<&str>());
verify_hashing_with(TypeId::of::<Vec<u8>>());
}
/// The raw, underlying form of a `Map`. /// The raw, underlying form of a `Map`.
/// ///
/// At its essence, this is a wrapper around `HashMap<TypeId, Box<Any>>`, with the portions that /// At its essence, this is a wrapper around `HashMap<TypeId, Box<Any>>`, with the portions that