Skip to content

Commit 5c6f02b

Browse files
13ros27ProfLander
authored andcommitted
impl Reflect for std::collections::HashMap instead of only bevy::utils::HashMap (bevyengine#7739) (bevyengine#7782)
# 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 bevyengine#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`
1 parent d7c2029 commit 5c6f02b

File tree

1 file changed

+183
-155
lines changed
  • crates/bevy_reflect/src/impls

1 file changed

+183
-155
lines changed

crates/bevy_reflect/src/impls/std.rs

Lines changed: 183 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use crate::{
1010

1111
use crate::utility::{reflect_hasher, GenericTypeInfoCell, NonGenericTypeInfoCell};
1212
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
13+
use bevy_utils::HashSet;
1314
use bevy_utils::{Duration, Instant};
14-
use bevy_utils::{HashMap, HashSet};
1515
use std::{
1616
any::Any,
1717
borrow::Cow,
1818
collections::VecDeque,
1919
ffi::OsString,
20-
hash::{Hash, Hasher},
20+
hash::{BuildHasher, Hash, Hasher},
2121
num::{
2222
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
2323
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
@@ -347,188 +347,216 @@ impl_reflect_for_veclike!(
347347
VecDeque::<T>
348348
);
349349

350-
impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
351-
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
352-
key.downcast_ref::<K>()
353-
.and_then(|key| HashMap::get(self, key))
354-
.map(|value| value as &dyn Reflect)
355-
}
350+
macro_rules! impl_reflect_for_hashmap {
351+
($ty:ty) => {
352+
impl<K, V, S> Map for $ty
353+
where
354+
K: FromReflect + Eq + Hash,
355+
V: FromReflect,
356+
S: BuildHasher + Send + Sync + 'static,
357+
{
358+
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
359+
key.downcast_ref::<K>()
360+
.and_then(|key| Self::get(self, key))
361+
.map(|value| value as &dyn Reflect)
362+
}
356363

357-
fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> {
358-
key.downcast_ref::<K>()
359-
.and_then(move |key| HashMap::get_mut(self, key))
360-
.map(|value| value as &mut dyn Reflect)
361-
}
364+
fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> {
365+
key.downcast_ref::<K>()
366+
.and_then(move |key| Self::get_mut(self, key))
367+
.map(|value| value as &mut dyn Reflect)
368+
}
362369

363-
fn get_at(&self, index: usize) -> Option<(&dyn Reflect, &dyn Reflect)> {
364-
self.iter()
365-
.nth(index)
366-
.map(|(key, value)| (key as &dyn Reflect, value as &dyn Reflect))
367-
}
370+
fn get_at(&self, index: usize) -> Option<(&dyn Reflect, &dyn Reflect)> {
371+
self.iter()
372+
.nth(index)
373+
.map(|(key, value)| (key as &dyn Reflect, value as &dyn Reflect))
374+
}
368375

369-
fn len(&self) -> usize {
370-
Self::len(self)
371-
}
376+
fn len(&self) -> usize {
377+
Self::len(self)
378+
}
372379

373-
fn iter(&self) -> MapIter {
374-
MapIter {
375-
map: self,
376-
index: 0,
377-
}
378-
}
380+
fn iter(&self) -> MapIter {
381+
MapIter {
382+
map: self,
383+
index: 0,
384+
}
385+
}
379386

380-
fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)> {
381-
self.into_iter()
382-
.map(|(key, value)| {
383-
(
384-
Box::new(key) as Box<dyn Reflect>,
385-
Box::new(value) as Box<dyn Reflect>,
386-
)
387-
})
388-
.collect()
389-
}
387+
fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)> {
388+
self.into_iter()
389+
.map(|(key, value)| {
390+
(
391+
Box::new(key) as Box<dyn Reflect>,
392+
Box::new(value) as Box<dyn Reflect>,
393+
)
394+
})
395+
.collect()
396+
}
397+
398+
fn clone_dynamic(&self) -> DynamicMap {
399+
let mut dynamic_map = DynamicMap::default();
400+
dynamic_map.set_name(self.type_name().to_string());
401+
for (k, v) in self {
402+
dynamic_map.insert_boxed(k.clone_value(), v.clone_value());
403+
}
404+
dynamic_map
405+
}
390406

391-
fn clone_dynamic(&self) -> DynamicMap {
392-
let mut dynamic_map = DynamicMap::default();
393-
dynamic_map.set_name(self.type_name().to_string());
394-
for (k, v) in self {
395-
dynamic_map.insert_boxed(k.clone_value(), v.clone_value());
407+
fn insert_boxed(
408+
&mut self,
409+
key: Box<dyn Reflect>,
410+
value: Box<dyn Reflect>,
411+
) -> Option<Box<dyn Reflect>> {
412+
let key = K::take_from_reflect(key).unwrap_or_else(|key| {
413+
panic!(
414+
"Attempted to insert invalid key of type {}.",
415+
key.type_name()
416+
)
417+
});
418+
let value = V::take_from_reflect(value).unwrap_or_else(|value| {
419+
panic!(
420+
"Attempted to insert invalid value of type {}.",
421+
value.type_name()
422+
)
423+
});
424+
self.insert(key, value)
425+
.map(|old_value| Box::new(old_value) as Box<dyn Reflect>)
426+
}
427+
428+
fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>> {
429+
let mut from_reflect = None;
430+
key.downcast_ref::<K>()
431+
.or_else(|| {
432+
from_reflect = K::from_reflect(key);
433+
from_reflect.as_ref()
434+
})
435+
.and_then(|key| self.remove(key))
436+
.map(|value| Box::new(value) as Box<dyn Reflect>)
437+
}
396438
}
397-
dynamic_map
398-
}
399-
400-
fn insert_boxed(
401-
&mut self,
402-
key: Box<dyn Reflect>,
403-
value: Box<dyn Reflect>,
404-
) -> Option<Box<dyn Reflect>> {
405-
let key = K::take_from_reflect(key).unwrap_or_else(|key| {
406-
panic!(
407-
"Attempted to insert invalid key of type {}.",
408-
key.type_name()
409-
)
410-
});
411-
let value = V::take_from_reflect(value).unwrap_or_else(|value| {
412-
panic!(
413-
"Attempted to insert invalid value of type {}.",
414-
value.type_name()
415-
)
416-
});
417-
self.insert(key, value)
418-
.map(|old_value| Box::new(old_value) as Box<dyn Reflect>)
419-
}
420-
421-
fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>> {
422-
let mut from_reflect = None;
423-
key.downcast_ref::<K>()
424-
.or_else(|| {
425-
from_reflect = K::from_reflect(key);
426-
from_reflect.as_ref()
427-
})
428-
.and_then(|key| self.remove(key))
429-
.map(|value| Box::new(value) as Box<dyn Reflect>)
430-
}
431-
}
432439

433-
impl<K: FromReflect + Eq + Hash, V: FromReflect> Reflect for HashMap<K, V> {
434-
fn type_name(&self) -> &str {
435-
std::any::type_name::<Self>()
436-
}
440+
impl<K, V, S> Reflect for $ty
441+
where
442+
K: FromReflect + Eq + Hash,
443+
V: FromReflect,
444+
S: BuildHasher + Send + Sync + 'static,
445+
{
446+
fn type_name(&self) -> &str {
447+
std::any::type_name::<Self>()
448+
}
437449

438-
fn get_type_info(&self) -> &'static TypeInfo {
439-
<Self as Typed>::type_info()
440-
}
450+
fn get_type_info(&self) -> &'static TypeInfo {
451+
<Self as Typed>::type_info()
452+
}
441453

442-
fn into_any(self: Box<Self>) -> Box<dyn Any> {
443-
self
444-
}
454+
fn into_any(self: Box<Self>) -> Box<dyn Any> {
455+
self
456+
}
445457

446-
fn as_any(&self) -> &dyn Any {
447-
self
448-
}
458+
fn as_any(&self) -> &dyn Any {
459+
self
460+
}
449461

450-
fn as_any_mut(&mut self) -> &mut dyn Any {
451-
self
452-
}
462+
fn as_any_mut(&mut self) -> &mut dyn Any {
463+
self
464+
}
453465

454-
#[inline]
455-
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
456-
self
457-
}
466+
#[inline]
467+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
468+
self
469+
}
458470

459-
fn as_reflect(&self) -> &dyn Reflect {
460-
self
461-
}
471+
fn as_reflect(&self) -> &dyn Reflect {
472+
self
473+
}
462474

463-
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
464-
self
465-
}
475+
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
476+
self
477+
}
466478

467-
fn apply(&mut self, value: &dyn Reflect) {
468-
map_apply(self, value);
469-
}
479+
fn apply(&mut self, value: &dyn Reflect) {
480+
map_apply(self, value);
481+
}
470482

471-
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
472-
*self = value.take()?;
473-
Ok(())
474-
}
483+
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
484+
*self = value.take()?;
485+
Ok(())
486+
}
475487

476-
fn reflect_ref(&self) -> ReflectRef {
477-
ReflectRef::Map(self)
478-
}
488+
fn reflect_ref(&self) -> ReflectRef {
489+
ReflectRef::Map(self)
490+
}
479491

480-
fn reflect_mut(&mut self) -> ReflectMut {
481-
ReflectMut::Map(self)
482-
}
492+
fn reflect_mut(&mut self) -> ReflectMut {
493+
ReflectMut::Map(self)
494+
}
483495

484-
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
485-
ReflectOwned::Map(self)
486-
}
496+
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
497+
ReflectOwned::Map(self)
498+
}
487499

488-
fn clone_value(&self) -> Box<dyn Reflect> {
489-
Box::new(self.clone_dynamic())
490-
}
500+
fn clone_value(&self) -> Box<dyn Reflect> {
501+
Box::new(self.clone_dynamic())
502+
}
491503

492-
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
493-
map_partial_eq(self, value)
494-
}
495-
}
504+
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
505+
map_partial_eq(self, value)
506+
}
507+
}
496508

497-
impl<K: FromReflect + Eq + Hash, V: FromReflect> Typed for HashMap<K, V> {
498-
fn type_info() -> &'static TypeInfo {
499-
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
500-
CELL.get_or_insert::<Self, _>(|| TypeInfo::Map(MapInfo::new::<Self, K, V>()))
501-
}
502-
}
509+
impl<K, V, S> Typed for $ty
510+
where
511+
K: FromReflect + Eq + Hash,
512+
V: FromReflect,
513+
S: BuildHasher + Send + Sync + 'static,
514+
{
515+
fn type_info() -> &'static TypeInfo {
516+
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
517+
CELL.get_or_insert::<Self, _>(|| TypeInfo::Map(MapInfo::new::<Self, K, V>()))
518+
}
519+
}
503520

504-
impl<K, V> GetTypeRegistration for HashMap<K, V>
505-
where
506-
K: FromReflect + Eq + Hash,
507-
V: FromReflect,
508-
{
509-
fn get_type_registration() -> TypeRegistration {
510-
let mut registration = TypeRegistration::of::<HashMap<K, V>>();
511-
registration.insert::<ReflectFromPtr>(FromType::<HashMap<K, V>>::from_type());
512-
registration
513-
}
514-
}
521+
impl<K, V, S> GetTypeRegistration for $ty
522+
where
523+
K: FromReflect + Eq + Hash,
524+
V: FromReflect,
525+
S: BuildHasher + Send + Sync + 'static,
526+
{
527+
fn get_type_registration() -> TypeRegistration {
528+
let mut registration = TypeRegistration::of::<Self>();
529+
registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
530+
registration
531+
}
532+
}
515533

516-
impl<K: FromReflect + Eq + Hash, V: FromReflect> FromReflect for HashMap<K, V> {
517-
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
518-
if let ReflectRef::Map(ref_map) = reflect.reflect_ref() {
519-
let mut new_map = Self::with_capacity(ref_map.len());
520-
for (key, value) in ref_map.iter() {
521-
let new_key = K::from_reflect(key)?;
522-
let new_value = V::from_reflect(value)?;
523-
new_map.insert(new_key, new_value);
534+
impl<K, V, S> FromReflect for $ty
535+
where
536+
K: FromReflect + Eq + Hash,
537+
V: FromReflect,
538+
S: BuildHasher + Default + Send + Sync + 'static,
539+
{
540+
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
541+
if let ReflectRef::Map(ref_map) = reflect.reflect_ref() {
542+
let mut new_map = Self::with_capacity_and_hasher(ref_map.len(), S::default());
543+
for (key, value) in ref_map.iter() {
544+
let new_key = K::from_reflect(key)?;
545+
let new_value = V::from_reflect(value)?;
546+
new_map.insert(new_key, new_value);
547+
}
548+
Some(new_map)
549+
} else {
550+
None
551+
}
524552
}
525-
Some(new_map)
526-
} else {
527-
None
528553
}
529-
}
554+
};
530555
}
531556

557+
impl_reflect_for_hashmap!(bevy_utils::hashbrown::HashMap<K, V, S>);
558+
impl_reflect_for_hashmap!(std::collections::HashMap<K, V, S>);
559+
532560
impl<T: Reflect, const N: usize> Array for [T; N] {
533561
#[inline]
534562
fn get(&self, index: usize) -> Option<&dyn Reflect> {

0 commit comments

Comments
 (0)