# Objective Implement `Reflect` for `std::collections::HashMap<K, V, S>` as well as `hashbrown::HashMap<K, V, S>` rather than just for `hashbrown::HashMap<K, V, RandomState>`. Fixes #7739. ## Solution Rather than implementing on `HashMap<K, V>` I instead implemented most of the related traits on `HashMap<K, V, S> where S: BuildHasher + Send + Sync + 'static` and then `FromReflect` also needs the extra bound `S: Default` because it needs to use `with_capacity_and_hasher` so needs to be able to generate a default hasher. As the API of `hashbrown::HashMap` is identical to `collections::HashMap` making them both work just required creating an `impl_reflect_for_hashmap` macro like the `impl_reflect_for_veclike` above and then applying this to both HashMaps. --- ## Changelog `std::collections::HashMap` can now be reflected. Also more `State` generics than just `RandomState` can now be reflected for both `hashbrown::HashMap` and `collections::HashMap`
This commit is contained in:
parent
aba8e81481
commit
3d3444b981
@ -10,14 +10,14 @@ use crate::{
|
||||
|
||||
use crate::utility::{reflect_hasher, GenericTypeInfoCell, NonGenericTypeInfoCell};
|
||||
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
|
||||
use bevy_utils::HashSet;
|
||||
use bevy_utils::{Duration, Instant};
|
||||
use bevy_utils::{HashMap, HashSet};
|
||||
use std::{
|
||||
any::Any,
|
||||
borrow::Cow,
|
||||
collections::VecDeque,
|
||||
ffi::OsString,
|
||||
hash::{Hash, Hasher},
|
||||
hash::{BuildHasher, Hash, Hasher},
|
||||
num::{
|
||||
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
|
||||
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
|
||||
@ -347,16 +347,23 @@ impl_reflect_for_veclike!(
|
||||
VecDeque::<T>
|
||||
);
|
||||
|
||||
impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
|
||||
macro_rules! impl_reflect_for_hashmap {
|
||||
($ty:ty) => {
|
||||
impl<K, V, S> Map for $ty
|
||||
where
|
||||
K: FromReflect + Eq + Hash,
|
||||
V: FromReflect,
|
||||
S: BuildHasher + Send + Sync + 'static,
|
||||
{
|
||||
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
|
||||
key.downcast_ref::<K>()
|
||||
.and_then(|key| HashMap::get(self, key))
|
||||
.and_then(|key| Self::get(self, key))
|
||||
.map(|value| value as &dyn Reflect)
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> {
|
||||
key.downcast_ref::<K>()
|
||||
.and_then(move |key| HashMap::get_mut(self, key))
|
||||
.and_then(move |key| Self::get_mut(self, key))
|
||||
.map(|value| value as &mut dyn Reflect)
|
||||
}
|
||||
|
||||
@ -430,7 +437,12 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: FromReflect + Eq + Hash, V: FromReflect> Reflect for HashMap<K, V> {
|
||||
impl<K, V, S> Reflect for $ty
|
||||
where
|
||||
K: FromReflect + Eq + Hash,
|
||||
V: FromReflect,
|
||||
S: BuildHasher + Send + Sync + 'static,
|
||||
{
|
||||
fn type_name(&self) -> &str {
|
||||
std::any::type_name::<Self>()
|
||||
}
|
||||
@ -494,29 +506,40 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Reflect for HashMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: FromReflect + Eq + Hash, V: FromReflect> Typed for HashMap<K, V> {
|
||||
impl<K, V, S> Typed for $ty
|
||||
where
|
||||
K: FromReflect + Eq + Hash,
|
||||
V: FromReflect,
|
||||
S: BuildHasher + Send + Sync + 'static,
|
||||
{
|
||||
fn type_info() -> &'static TypeInfo {
|
||||
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
|
||||
CELL.get_or_insert::<Self, _>(|| TypeInfo::Map(MapInfo::new::<Self, K, V>()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> GetTypeRegistration for HashMap<K, V>
|
||||
impl<K, V, S> GetTypeRegistration for $ty
|
||||
where
|
||||
K: FromReflect + Eq + Hash,
|
||||
V: FromReflect,
|
||||
S: BuildHasher + Send + Sync + 'static,
|
||||
{
|
||||
fn get_type_registration() -> TypeRegistration {
|
||||
let mut registration = TypeRegistration::of::<HashMap<K, V>>();
|
||||
registration.insert::<ReflectFromPtr>(FromType::<HashMap<K, V>>::from_type());
|
||||
let mut registration = TypeRegistration::of::<Self>();
|
||||
registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
|
||||
registration
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: FromReflect + Eq + Hash, V: FromReflect> FromReflect for HashMap<K, V> {
|
||||
impl<K, V, S> FromReflect for $ty
|
||||
where
|
||||
K: FromReflect + Eq + Hash,
|
||||
V: FromReflect,
|
||||
S: BuildHasher + Default + Send + Sync + 'static,
|
||||
{
|
||||
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
|
||||
if let ReflectRef::Map(ref_map) = reflect.reflect_ref() {
|
||||
let mut new_map = Self::with_capacity(ref_map.len());
|
||||
let mut new_map = Self::with_capacity_and_hasher(ref_map.len(), S::default());
|
||||
for (key, value) in ref_map.iter() {
|
||||
let new_key = K::from_reflect(key)?;
|
||||
let new_value = V::from_reflect(value)?;
|
||||
@ -528,6 +551,11 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> FromReflect for HashMap<K, V> {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_reflect_for_hashmap!(bevy_utils::hashbrown::HashMap<K, V, S>);
|
||||
impl_reflect_for_hashmap!(std::collections::HashMap<K, V, S>);
|
||||
|
||||
impl<T: Reflect, const N: usize> Array for [T; N] {
|
||||
#[inline]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user