Remove hash for AnyMap (since it's a hashmap already) (#1951)

This commit is contained in:
Dominik Maier 2024-03-19 20:41:15 +01:00 committed by GitHub
parent 2efa747292
commit 14fd3040bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -109,7 +109,10 @@ where
/// Each element needs to be registered so that it can be deserialized. /// Each element needs to be registered so that it can be deserialized.
pub mod serdeany_registry { pub mod serdeany_registry {
use alloc::boxed::Box; use alloc::{
boxed::Box,
string::{String, ToString},
};
use core::{fmt, hash::BuildHasherDefault}; use core::{fmt, hash::BuildHasherDefault};
use hashbrown::{ use hashbrown::{
@ -118,10 +121,11 @@ pub mod serdeany_registry {
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{SerdeAny, TypeRepr};
use crate::{ use crate::{
hash_std, serdeany::{
serdeany::{type_repr, type_repr_owned, DeserializeCallback, DeserializeCallbackSeed}, type_repr, type_repr_owned, DeserializeCallback, DeserializeCallbackSeed, SerdeAny,
TypeRepr,
},
Error, Error,
}; };
@ -421,7 +425,7 @@ pub mod serdeany_registry {
#[allow(unused_qualifications)] #[allow(unused_qualifications)]
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct NamedSerdeAnyMap { pub struct NamedSerdeAnyMap {
map: HashMap<TypeRepr, HashMap<u64, Box<dyn crate::serdeany::SerdeAny>>>, map: HashMap<TypeRepr, HashMap<String, Box<dyn crate::serdeany::SerdeAny>>>,
} }
// Cloning by serializing and deserializing. It ain't fast, but it's honest work. // Cloning by serializing and deserializing. It ain't fast, but it's honest work.
@ -448,9 +452,7 @@ pub mod serdeany_registry {
match self.map.get(type_repr) { match self.map.get(type_repr) {
None => None, None => None,
Some(h) => h Some(h) => h.get(name).map(|x| x.as_any().downcast_ref::<T>().unwrap()),
.get(&hash_std(name.as_bytes()))
.map(|x| x.as_any().downcast_ref::<T>().unwrap()),
} }
} }
@ -468,7 +470,7 @@ pub mod serdeany_registry {
match self.map.get_mut(type_repr) { match self.map.get_mut(type_repr) {
None => None, None => None,
Some(h) => h Some(h) => h
.remove(&hash_std(name.as_bytes())) .remove(name)
.map(|x| x.as_any_boxed().downcast::<T>().unwrap()), .map(|x| x.as_any_boxed().downcast::<T>().unwrap()),
} }
} }
@ -487,7 +489,7 @@ pub mod serdeany_registry {
match self.map.get_mut(type_repr) { match self.map.get_mut(type_repr) {
None => None, None => None,
Some(h) => h Some(h) => h
.get_mut(&hash_std(name.as_bytes())) .get_mut(name)
.map(|x| x.as_any_mut().downcast_mut::<T>().unwrap()), .map(|x| x.as_any_mut().downcast_mut::<T>().unwrap()),
} }
} }
@ -501,7 +503,7 @@ pub mod serdeany_registry {
&self, &self,
) -> Option< ) -> Option<
core::iter::Map< core::iter::Map<
Values<'_, u64, Box<dyn crate::serdeany::SerdeAny>>, Values<'_, String, Box<dyn crate::serdeany::SerdeAny>>,
fn(&Box<dyn crate::serdeany::SerdeAny>) -> &T, fn(&Box<dyn crate::serdeany::SerdeAny>) -> &T,
>, >,
> >
@ -527,7 +529,7 @@ pub mod serdeany_registry {
&mut self, &mut self,
) -> Option< ) -> Option<
core::iter::Map< core::iter::Map<
ValuesMut<'_, u64, Box<dyn crate::serdeany::SerdeAny>>, ValuesMut<'_, String, Box<dyn crate::serdeany::SerdeAny>>,
fn(&mut Box<dyn crate::serdeany::SerdeAny>) -> &mut T, fn(&mut Box<dyn crate::serdeany::SerdeAny>) -> &mut T,
>, >,
> >
@ -588,21 +590,15 @@ pub mod serdeany_registry {
where where
T: crate::serdeany::SerdeAny, T: crate::serdeany::SerdeAny,
{ {
self.entry::<T>(name).insert(Box::new(val)); self.entry::<T>(name.into()).insert(Box::new(val));
} }
/// Get an entry to an element into this map. /// Get a reference to the type map.
#[inline] #[inline]
#[allow(unused_qualifications)] #[allow(unused_qualifications)]
pub fn entry<T>( fn outer_map_mut<T>(
&mut self, &mut self,
name: &str, ) -> &mut hashbrown::hash_map::HashMap<String, Box<dyn SerdeAny + 'static>>
) -> hashbrown::hash_map::Entry<
'_,
u64,
Box<dyn SerdeAny + 'static>,
BuildHasherDefault<ahash::AHasher>,
>
where where
T: crate::serdeany::SerdeAny, T: crate::serdeany::SerdeAny,
{ {
@ -628,7 +624,43 @@ pub mod serdeany_registry {
.from_key(type_repr) .from_key(type_repr)
.or_insert_with(|| (type_repr_owned::<T>(), HashMap::default())) .or_insert_with(|| (type_repr_owned::<T>(), HashMap::default()))
.1 .1
.entry(hash_std(name.as_bytes())) }
/// Get an entry to an element into this map.
/// Prefer [`Self::raw_entry_mut`] as it won't need an owned key.
#[inline]
#[allow(unused_qualifications)]
fn entry<T>(
&mut self,
name: String,
) -> hashbrown::hash_map::Entry<
'_,
String,
Box<dyn SerdeAny + 'static>,
BuildHasherDefault<ahash::AHasher>,
>
where
T: crate::serdeany::SerdeAny,
{
self.outer_map_mut::<T>().entry(name)
}
/// Get a raw entry to an element into this map.
#[inline]
#[allow(unused_qualifications)]
fn raw_entry_mut<T>(
&mut self,
name: &str,
) -> hashbrown::hash_map::RawEntryMut<
'_,
String,
Box<dyn SerdeAny + 'static>,
BuildHasherDefault<ahash::AHasher>,
>
where
T: crate::serdeany::SerdeAny,
{
self.outer_map_mut::<T>().raw_entry_mut().from_key(name)
} }
/// Gets a value by name, or inserts it using the given construction function `default` /// Gets a value by name, or inserts it using the given construction function `default`
@ -636,8 +668,10 @@ pub mod serdeany_registry {
where where
T: SerdeAny, T: SerdeAny,
{ {
let ret = self.entry::<T>(name).or_insert_with(|| Box::new(default())); let ret = self
ret.as_mut().as_any_mut().downcast_mut::<T>().unwrap() .raw_entry_mut::<T>(name)
.or_insert_with(|| (name.to_string(), Box::new(default())));
ret.1.as_any_mut().downcast_mut::<T>().unwrap()
} }
/// Gets a value by name, or inserts it using the given construction function `default` (returning a boxed value) /// Gets a value by name, or inserts it using the given construction function `default` (returning a boxed value)
@ -649,8 +683,10 @@ pub mod serdeany_registry {
where where
T: SerdeAny + 'static, T: SerdeAny + 'static,
{ {
let ret = self.entry::<T>(name).or_insert_with(|| default()); let ret = self
ret.as_mut().as_any_mut().downcast_mut::<T>().unwrap() .raw_entry_mut::<T>(name)
.or_insert_with(|| (name.to_string(), default()));
ret.1.as_any_mut().downcast_mut::<T>().unwrap()
} }
/// Returns the `len` of this map. /// Returns the `len` of this map.
@ -693,7 +729,7 @@ pub mod serdeany_registry {
match self.map.get(type_repr) { match self.map.get(type_repr) {
None => false, None => false,
Some(h) => h.contains_key(&hash_std(name.as_bytes())), Some(h) => h.contains_key(name),
} }
} }