Make TypeIdHasher safe, bump MSRV
[anymap] / src / raw.rs
index 17c3869c9153be5d559d1610829fe5db853aca1c..0094b3412726fc99517f70868677e39b59f3d5cc 100644 (file)
@@ -5,12 +5,12 @@
 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)]
 use std::mem;
 use std::ops::{Index, IndexMut};
-use std::ptr;
 
 use any::{Any, UncheckedAnyExt};
 
@@ -22,11 +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
-        debug_assert!(bytes.len() == 8);
-        unsafe {
-            ptr::copy_nonoverlapping(&bytes[0] as *const u8 as *const u64, &mut self.value, 1)
-        }
+        // 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<A: ?Sized + UncheckedAnyExt = Any> {
+pub struct RawMap<A: ?Sized + UncheckedAnyExt = dyn Any> {
     inner: HashMap<TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>,
 }
 
@@ -70,13 +72,6 @@ impl<A: ?Sized + UncheckedAnyExt> Clone for RawMap<A> where Box<A>: Clone {
     }
 }
 
-impl<A: ?Sized + UncheckedAnyExt> Default for RawMap<A> {
-    #[inline]
-    fn default() -> RawMap<A> {
-        RawMap::new()
-    }
-}
-
 impl_common_methods! {
     field: RawMap.inner;
     new() => HashMap::with_hasher(Default::default());