X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/143ee062680311ca9c2ed5b7089bb0d741bc17c0..6a2a404af73f1dfc1379c279da2dee702fb900ae:/src/raw.rs diff --git a/src/raw.rs b/src/raw.rs index db96a0c..e75de32 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -2,7 +2,7 @@ //! //! All relevant details are in the `RawAnyMap` struct. -use std::any::{Any, TypeId}; +use std::any::TypeId; use std::borrow::Borrow; use std::collections::hash_map::{self, HashMap}; use std::collections::hash_state::HashState; @@ -13,10 +13,16 @@ use std::mem; use std::ops::{Index, IndexMut}; use std::ptr; +#[cfg(not(feature = "clone"))] +pub use std::any::Any; +#[cfg(feature = "clone")] +pub use with_clone::Any; + struct TypeIdHasher { value: u64, } +#[cfg_attr(feature = "clone", derive(Clone))] struct TypeIdState; impl HashState for TypeIdState { @@ -50,6 +56,7 @@ impl Hasher for TypeIdHasher { /// contents of an `AnyMap`. 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)] +#[cfg_attr(feature = "clone", derive(Clone))] pub struct RawAnyMap { inner: HashMap, TypeIdState>, } @@ -224,17 +231,17 @@ impl RawAnyMap { } -impl Index for RawAnyMap where TypeId: Borrow, Q: Eq + Hash { +impl Index for RawAnyMap where TypeId: Borrow, Q: Eq + Hash { type Output = Any; - fn index<'a>(&'a self, index: &Q) -> &'a Any { - self.get(index).expect("no entry found for key") + fn index<'a>(&'a self, index: Q) -> &'a Any { + self.get(&index).expect("no entry found for key") } } -impl IndexMut for RawAnyMap where TypeId: Borrow, Q: Eq + Hash { - fn index_mut<'a>(&'a mut self, index: &Q) -> &'a mut Any { - self.get_mut(index).expect("no entry found for key") +impl IndexMut for RawAnyMap where TypeId: Borrow, Q: Eq + Hash { + fn index_mut<'a>(&'a mut self, index: Q) -> &'a mut Any { + self.get_mut(&index).expect("no entry found for key") } } @@ -266,11 +273,27 @@ pub enum Entry<'a> { } impl<'a> Entry<'a> { - /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant. - pub fn get(self) -> Result<&'a mut Any, VacantEntry<'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. + pub unsafe fn or_insert(self, default: Box) -> &'a mut Any { + match self { + Entry::Occupied(inner) => inner.into_mut(), + Entry::Vacant(inner) => inner.insert(default), + } + } + + /// 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. + pub unsafe fn or_insert_with Box>(self, default: F) -> &'a mut Any { match self { - Entry::Occupied(inner) => Ok(inner.into_mut()), - Entry::Vacant(inner) => Err(inner), + Entry::Occupied(inner) => inner.into_mut(), + Entry::Vacant(inner) => inner.insert(default()), } } }