Skip to content

Commit f45450e

Browse files
authored
Added reflect support for std::HashSet, BTreeSet and BTreeMap. (#12124)
# Objective Added reflect support for `std::HashSet`, `BTreeSet` and `BTreeMap`. The set support is limited to `reflect_value` since that's the level of support prior art `bevy_util::HashSet` got. ## Changelog Dropped `Hash` Requirement on `MapInfo` since it's not needed on `BTreeMap`s.
1 parent 1cded6a commit f45450e

File tree

2 files changed

+234
-6
lines changed

2 files changed

+234
-6
lines changed

crates/bevy_reflect/src/impls/std.rs

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ impl_reflect_value!(
100100
::core::result::Result < T: Clone + Reflect + TypePath,
101101
E: Clone + Reflect + TypePath > ()
102102
);
103+
impl_reflect_value!(::std::collections::BTreeSet<T: Ord + Eq + Clone + Send + Sync>());
104+
impl_reflect_value!(::std::collections::HashSet<T: Hash + Eq + Clone + Send + Sync, S: TypePath + Clone + Send + Sync>());
103105
impl_reflect_value!(::bevy_utils::HashSet<T: Hash + Eq + Clone + Send + Sync>());
104106
impl_reflect_value!(::core::ops::Range<T: Clone + Send + Sync>());
105107
impl_reflect_value!(::core::ops::RangeInclusive<T: Clone + Send + Sync>());
@@ -620,6 +622,220 @@ impl_type_path!(::bevy_utils::hashbrown::hash_map::DefaultHashBuilder);
620622
impl_type_path!(::bevy_utils::NoOpHash);
621623
impl_type_path!(::bevy_utils::hashbrown::HashMap<K, V, S>);
622624

625+
macro_rules! impl_reflect_for_btree_map {
626+
($ty:path) => {
627+
impl<K, V> Map for $ty
628+
where
629+
K: FromReflect + TypePath + Eq + Ord,
630+
V: FromReflect + TypePath,
631+
{
632+
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
633+
key.downcast_ref::<K>()
634+
.and_then(|key| Self::get(self, key))
635+
.map(|value| value as &dyn Reflect)
636+
}
637+
638+
fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> {
639+
key.downcast_ref::<K>()
640+
.and_then(move |key| Self::get_mut(self, key))
641+
.map(|value| value as &mut dyn Reflect)
642+
}
643+
644+
fn get_at(&self, index: usize) -> Option<(&dyn Reflect, &dyn Reflect)> {
645+
self.iter()
646+
.nth(index)
647+
.map(|(key, value)| (key as &dyn Reflect, value as &dyn Reflect))
648+
}
649+
650+
fn get_at_mut(&mut self, index: usize) -> Option<(&dyn Reflect, &mut dyn Reflect)> {
651+
self.iter_mut()
652+
.nth(index)
653+
.map(|(key, value)| (key as &dyn Reflect, value as &mut dyn Reflect))
654+
}
655+
656+
fn len(&self) -> usize {
657+
Self::len(self)
658+
}
659+
660+
fn iter(&self) -> MapIter {
661+
MapIter::new(self)
662+
}
663+
664+
fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)> {
665+
self.into_iter()
666+
.map(|(key, value)| {
667+
(
668+
Box::new(key) as Box<dyn Reflect>,
669+
Box::new(value) as Box<dyn Reflect>,
670+
)
671+
})
672+
.collect()
673+
}
674+
675+
fn clone_dynamic(&self) -> DynamicMap {
676+
let mut dynamic_map = DynamicMap::default();
677+
dynamic_map.set_represented_type(self.get_represented_type_info());
678+
for (k, v) in self {
679+
let key = K::from_reflect(k).unwrap_or_else(|| {
680+
panic!(
681+
"Attempted to clone invalid key of type {}.",
682+
k.reflect_type_path()
683+
)
684+
});
685+
dynamic_map.insert_boxed(Box::new(key), v.clone_value());
686+
}
687+
dynamic_map
688+
}
689+
690+
fn insert_boxed(
691+
&mut self,
692+
key: Box<dyn Reflect>,
693+
value: Box<dyn Reflect>,
694+
) -> Option<Box<dyn Reflect>> {
695+
let key = K::take_from_reflect(key).unwrap_or_else(|key| {
696+
panic!(
697+
"Attempted to insert invalid key of type {}.",
698+
key.reflect_type_path()
699+
)
700+
});
701+
let value = V::take_from_reflect(value).unwrap_or_else(|value| {
702+
panic!(
703+
"Attempted to insert invalid value of type {}.",
704+
value.reflect_type_path()
705+
)
706+
});
707+
self.insert(key, value)
708+
.map(|old_value| Box::new(old_value) as Box<dyn Reflect>)
709+
}
710+
711+
fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>> {
712+
let mut from_reflect = None;
713+
key.downcast_ref::<K>()
714+
.or_else(|| {
715+
from_reflect = K::from_reflect(key);
716+
from_reflect.as_ref()
717+
})
718+
.and_then(|key| self.remove(key))
719+
.map(|value| Box::new(value) as Box<dyn Reflect>)
720+
}
721+
}
722+
723+
impl<K, V> Reflect for $ty
724+
where
725+
K: FromReflect + TypePath + Eq + Ord,
726+
V: FromReflect + TypePath,
727+
{
728+
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
729+
Some(<Self as Typed>::type_info())
730+
}
731+
732+
fn into_any(self: Box<Self>) -> Box<dyn Any> {
733+
self
734+
}
735+
736+
fn as_any(&self) -> &dyn Any {
737+
self
738+
}
739+
740+
fn as_any_mut(&mut self) -> &mut dyn Any {
741+
self
742+
}
743+
744+
#[inline]
745+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
746+
self
747+
}
748+
749+
fn as_reflect(&self) -> &dyn Reflect {
750+
self
751+
}
752+
753+
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
754+
self
755+
}
756+
757+
fn apply(&mut self, value: &dyn Reflect) {
758+
map_apply(self, value);
759+
}
760+
761+
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
762+
*self = value.take()?;
763+
Ok(())
764+
}
765+
766+
fn reflect_kind(&self) -> ReflectKind {
767+
ReflectKind::Map
768+
}
769+
770+
fn reflect_ref(&self) -> ReflectRef {
771+
ReflectRef::Map(self)
772+
}
773+
774+
fn reflect_mut(&mut self) -> ReflectMut {
775+
ReflectMut::Map(self)
776+
}
777+
778+
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
779+
ReflectOwned::Map(self)
780+
}
781+
782+
fn clone_value(&self) -> Box<dyn Reflect> {
783+
Box::new(self.clone_dynamic())
784+
}
785+
786+
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
787+
map_partial_eq(self, value)
788+
}
789+
}
790+
791+
impl<K, V> Typed for $ty
792+
where
793+
K: FromReflect + TypePath + Eq + Ord,
794+
V: FromReflect + TypePath,
795+
{
796+
fn type_info() -> &'static TypeInfo {
797+
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
798+
CELL.get_or_insert::<Self, _>(|| TypeInfo::Map(MapInfo::new::<Self, K, V>()))
799+
}
800+
}
801+
802+
impl<K, V> GetTypeRegistration for $ty
803+
where
804+
K: FromReflect + TypePath + Eq + Ord,
805+
V: FromReflect + TypePath,
806+
{
807+
fn get_type_registration() -> TypeRegistration {
808+
let mut registration = TypeRegistration::of::<Self>();
809+
registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
810+
registration
811+
}
812+
}
813+
814+
impl<K, V> FromReflect for $ty
815+
where
816+
K: FromReflect + TypePath + Eq + Ord,
817+
V: FromReflect + TypePath,
818+
{
819+
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
820+
if let ReflectRef::Map(ref_map) = reflect.reflect_ref() {
821+
let mut new_map = Self::new();
822+
for (key, value) in ref_map.iter() {
823+
let new_key = K::from_reflect(key)?;
824+
let new_value = V::from_reflect(value)?;
825+
new_map.insert(new_key, new_value);
826+
}
827+
Some(new_map)
828+
} else {
829+
None
830+
}
831+
}
832+
}
833+
};
834+
}
835+
836+
impl_reflect_for_btree_map!(::std::collections::BTreeMap<K, V>);
837+
impl_type_path!(::std::collections::BTreeMap<K, V>);
838+
623839
impl<T: Reflect + TypePath, const N: usize> Array for [T; N] {
624840
#[inline]
625841
fn get(&self, index: usize) -> Option<&dyn Reflect> {
@@ -1658,6 +1874,7 @@ mod tests {
16581874
use bevy_utils::HashMap;
16591875
use bevy_utils::{Duration, Instant};
16601876
use static_assertions::assert_impl_all;
1877+
use std::collections::BTreeMap;
16611878
use std::f32::consts::{PI, TAU};
16621879
use std::path::Path;
16631880

@@ -1732,6 +1949,21 @@ mod tests {
17321949
assert!(!a.reflect_partial_eq(c).unwrap_or_default());
17331950
}
17341951

1952+
#[test]
1953+
fn should_partial_eq_btree_map() {
1954+
let mut a = BTreeMap::new();
1955+
a.insert(0usize, 1.23_f64);
1956+
let b = a.clone();
1957+
let mut c = BTreeMap::new();
1958+
c.insert(0usize, 3.21_f64);
1959+
1960+
let a: &dyn Reflect = &a;
1961+
let b: &dyn Reflect = &b;
1962+
let c: &dyn Reflect = &c;
1963+
assert!(a.reflect_partial_eq(b).unwrap_or_default());
1964+
assert!(!a.reflect_partial_eq(c).unwrap_or_default());
1965+
}
1966+
17351967
#[test]
17361968
fn should_partial_eq_option() {
17371969
let a: &dyn Reflect = &Some(123);

crates/bevy_reflect/src/map.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::any::{Any, TypeId};
22
use std::fmt::{Debug, Formatter};
3-
use std::hash::Hash;
43

54
use bevy_reflect_derive::impl_type_path;
65
use bevy_utils::{Entry, HashMap};
@@ -107,11 +106,8 @@ pub struct MapInfo {
107106

108107
impl MapInfo {
109108
/// Create a new [`MapInfo`].
110-
pub fn new<
111-
TMap: Map + TypePath,
112-
TKey: Hash + Reflect + TypePath,
113-
TValue: Reflect + TypePath,
114-
>() -> Self {
109+
pub fn new<TMap: Map + TypePath, TKey: Reflect + TypePath, TValue: Reflect + TypePath>() -> Self
110+
{
115111
Self {
116112
type_path: TypePathTable::of::<TMap>(),
117113
type_id: TypeId::of::<TMap>(),

0 commit comments

Comments
 (0)