# 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 crate::utility::{reflect_hasher, GenericTypeInfoCell, NonGenericTypeInfoCell};
|
||||||
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
|
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
|
||||||
|
use bevy_utils::HashSet;
|
||||||
use bevy_utils::{Duration, Instant};
|
use bevy_utils::{Duration, Instant};
|
||||||
use bevy_utils::{HashMap, HashSet};
|
|
||||||
use std::{
|
use std::{
|
||||||
any::Any,
|
any::Any,
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
hash::{Hash, Hasher},
|
hash::{BuildHasher, Hash, Hasher},
|
||||||
num::{
|
num::{
|
||||||
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
|
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
|
||||||
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
|
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
|
||||||
@ -347,16 +347,23 @@ impl_reflect_for_veclike!(
|
|||||||
VecDeque::<T>
|
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> {
|
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
|
||||||
key.downcast_ref::<K>()
|
key.downcast_ref::<K>()
|
||||||
.and_then(|key| HashMap::get(self, key))
|
.and_then(|key| Self::get(self, key))
|
||||||
.map(|value| value as &dyn Reflect)
|
.map(|value| value as &dyn Reflect)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> {
|
fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> {
|
||||||
key.downcast_ref::<K>()
|
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)
|
.map(|value| value as &mut dyn Reflect)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -428,9 +435,14 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
|
|||||||
.and_then(|key| self.remove(key))
|
.and_then(|key| self.remove(key))
|
||||||
.map(|value| Box::new(value) as Box<dyn Reflect>)
|
.map(|value| Box::new(value) as Box<dyn Reflect>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
fn type_name(&self) -> &str {
|
||||||
std::any::type_name::<Self>()
|
std::any::type_name::<Self>()
|
||||||
}
|
}
|
||||||
@ -492,31 +504,42 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Reflect for HashMap<K, V> {
|
|||||||
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
|
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
|
||||||
map_partial_eq(self, value)
|
map_partial_eq(self, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
fn type_info() -> &'static TypeInfo {
|
||||||
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
|
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
|
||||||
CELL.get_or_insert::<Self, _>(|| TypeInfo::Map(MapInfo::new::<Self, K, V>()))
|
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
|
where
|
||||||
K: FromReflect + Eq + Hash,
|
K: FromReflect + Eq + Hash,
|
||||||
V: FromReflect,
|
V: FromReflect,
|
||||||
{
|
S: BuildHasher + Send + Sync + 'static,
|
||||||
|
{
|
||||||
fn get_type_registration() -> TypeRegistration {
|
fn get_type_registration() -> TypeRegistration {
|
||||||
let mut registration = TypeRegistration::of::<HashMap<K, V>>();
|
let mut registration = TypeRegistration::of::<Self>();
|
||||||
registration.insert::<ReflectFromPtr>(FromType::<HashMap<K, V>>::from_type());
|
registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
|
||||||
registration
|
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> {
|
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
|
||||||
if let ReflectRef::Map(ref_map) = reflect.reflect_ref() {
|
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() {
|
for (key, value) in ref_map.iter() {
|
||||||
let new_key = K::from_reflect(key)?;
|
let new_key = K::from_reflect(key)?;
|
||||||
let new_value = V::from_reflect(value)?;
|
let new_value = V::from_reflect(value)?;
|
||||||
@ -527,8 +550,13 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> FromReflect for HashMap<K, V> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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] {
|
impl<T: Reflect, const N: usize> Array for [T; N] {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn get(&self, index: usize) -> Option<&dyn Reflect> {
|
fn get(&self, index: usize) -> Option<&dyn Reflect> {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user