X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/34028c35e70de959ec61758db88dfc84f75764cf..8f041216babc57cbd1b89e169aaf66163822e4b2:/src/raw.rs diff --git a/src/raw.rs b/src/raw.rs index 17c3869..73c20e2 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -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] @@ -38,6 +40,7 @@ fn type_id_hasher() { fn verify_hashing_with(type_id: TypeId) { let mut hasher = TypeIdHasher::default(); type_id.hash(&mut hasher); + // SAFETY: u64 is valid for all bit patterns. 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. @@ -56,7 +59,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>, } @@ -70,13 +73,6 @@ impl Clone for RawMap where Box: Clone { } } -impl Default for RawMap { - #[inline] - fn default() -> RawMap { - RawMap::new() - } -} - impl_common_methods! { field: RawMap.inner; new() => HashMap::with_hasher(Default::default()); @@ -213,10 +209,11 @@ impl RawMap { } /// Inserts a key-value pair from the map. If the key already had a value present in the map, - /// that value is returned. Otherwise, None is returned. + /// that value is returned. Otherwise, `None` is returned. /// - /// It is the caller’s responsibility to ensure that the key corresponds with the type ID of - /// the value. If they do not, memory safety may be violated. + /// # Safety + /// + /// `key` and the type ID of `value` must match, or *undefined behaviour* occurs. #[inline] pub unsafe fn insert(&mut self, key: TypeId, value: Box) -> Option> { self.inner.insert(key, value) @@ -285,8 +282,9 @@ impl<'a, A: ?Sized + UncheckedAnyExt> Entry<'a, A> { /// Ensures a value is in the entry by inserting the default if empty, and returns /// a mutable reference to the value in the entry. /// - /// It is the caller’s responsibility to ensure that the key of the entry corresponds with - /// the type ID of `value`. If they do not, memory safety may be violated. + /// # Safety + /// + /// The type ID of `default` must match the entry’s key, or *undefined behaviour* occurs. #[inline] pub unsafe fn or_insert(self, default: Box) -> &'a mut A { match self { @@ -298,8 +296,10 @@ impl<'a, A: ?Sized + UncheckedAnyExt> Entry<'a, A> { /// Ensures a value is in the entry by inserting the result of the default function if empty, /// and returns a mutable reference to the value in the entry. /// - /// It is the caller’s responsibility to ensure that the key of the entry corresponds with - /// the type ID of `value`. If they do not, memory safety may be violated. + /// # Safety + /// + /// The type ID of the value returned by `default` must match the entry’s key, + /// or *undefined behaviour* occurs. #[inline] pub unsafe fn or_insert_with Box>(self, default: F) -> &'a mut A { match self { @@ -331,8 +331,9 @@ impl<'a, A: ?Sized + UncheckedAnyExt> OccupiedEntry<'a, A> { /// Sets the value of the entry, and returns the entry's old value. /// - /// It is the caller’s responsibility to ensure that the key of the entry corresponds with - /// the type ID of `value`. If they do not, memory safety may be violated. + /// # Safety + /// + /// The type ID of `value` must match the entry’s key, or *undefined behaviour* occurs. #[inline] pub unsafe fn insert(&mut self, value: Box) -> Box { self.inner.insert(value) @@ -347,10 +348,11 @@ impl<'a, A: ?Sized + UncheckedAnyExt> OccupiedEntry<'a, A> { impl<'a, A: ?Sized + UncheckedAnyExt> VacantEntry<'a, A> { /// Sets the value of the entry with the VacantEntry's key, - /// and returns a mutable reference to it + /// and returns a mutable reference to it. + /// + /// # Safety /// - /// It is the caller’s responsibility to ensure that the key of the entry corresponds with - /// the type ID of `value`. If they do not, memory safety may be violated. + /// The type ID of `value` must match the entry’s key, or *undefined behaviour* occurs. #[inline] pub unsafe fn insert(self, value: Box) -> &'a mut A { &mut **self.inner.insert(value)