X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/6dab74b72178bb02bd26a8bcaf3cf588997d7800..7866ca8d779805dc3a3134f2d247889a1721633c:/src/raw.rs?ds=sidebyside diff --git a/src/raw.rs b/src/raw.rs index dbd5e2c..0094b34 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -5,6 +5,7 @@ use std::any::TypeId; use std::borrow::Borrow; use std::collections::hash_map::{self, HashMap}; +use std::convert::TryInto; use std::hash::Hash; use std::hash::{Hasher, BuildHasherDefault}; #[cfg(test)] @@ -21,12 +22,13 @@ struct TypeIdHasher { impl Hasher for TypeIdHasher { #[inline] fn write(&mut self, bytes: &[u8]) { - // This expects to receive one and exactly one 64-bit value - assert!(bytes.len() == 8); - self.value = u64::from(bytes[0]) | u64::from(bytes[1]) << 8 | - u64::from(bytes[2]) << 16 | u64::from(bytes[3]) << 24 | - u64::from(bytes[4]) << 32 | u64::from(bytes[5]) << 40 | - u64::from(bytes[6]) << 48 | u64::from(bytes[7]) << 56; + // This expects to receive exactly one 64-bit value, and there’s no realistic chance of + // that changing, but I don’t want to depend on something that isn’t expressly part of the + // contract for safety. But I’m OK with release builds putting everything in one bucket + // if it *did* change (and debug builds panicking). + debug_assert_eq!(bytes.len(), 8); + let _ = bytes.try_into() + .map(|array| self.value = u64::from_ne_bytes(array)); } #[inline] @@ -56,7 +58,7 @@ fn type_id_hasher() { /// contents of an `Map`. However, because you will then be dealing with `Any` trait objects, it /// doesn’t tend to be so very useful. Still, if you need it, it’s here. #[derive(Debug)] -pub struct RawMap { +pub struct RawMap { inner: HashMap, BuildHasherDefault>, }