From: Chris Morgan Date: Sun, 6 Mar 2016 13:11:37 +0000 (+1100) Subject: Fix order of ptr::copy_nonoverlapping parameters. X-Git-Tag: 0.12.1~10 X-Git-Url: https://git.chrismorgan.info/anymap/commitdiff_plain/724f94758def9f71ad27ff49e47e908a431c2728 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. --- diff --git a/src/raw.rs b/src/raw.rs index 6aed3e8..31a78e4 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -24,7 +24,7 @@ impl Hasher for TypeIdHasher { // This expects to receive one and exactly one 64-bit value debug_assert!(bytes.len() == 8); 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 } } +#[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::(type_id) }); + } + // Pick a variety of types, just to demonstrate it’s all sane. Normal, zero-sized, unsized, &c. + verify_hashing_with(TypeId::of::()); + verify_hashing_with(TypeId::of::<()>()); + verify_hashing_with(TypeId::of::()); + verify_hashing_with(TypeId::of::<&str>()); + verify_hashing_with(TypeId::of::>()); +} + /// The raw, underlying form of a `Map`. /// /// At its essence, this is a wrapper around `HashMap>`, with the portions that