From 724f94758def9f71ad27ff49e47e908a431c2728 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Mon, 7 Mar 2016 00:11:37 +1100 Subject: [PATCH] Fix order of ptr::copy_nonoverlapping parameters. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- src/raw.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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 -- 2.42.0