Remove unnecessary impl_reflect_for_btree_map macro (#12146)

# Objective

To remove the `impl_reflect_for_btree_map` macro as per #12140.

## Solution

Replaced the `impl_reflect_for_btree_map` macro.
This commit is contained in:
Antony 2024-02-26 20:04:11 -05:00 committed by GitHub
parent f7f7e326e5
commit fd0f1a37ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -622,218 +622,213 @@ impl_type_path!(::bevy_utils::hashbrown::hash_map::DefaultHashBuilder);
impl_type_path!(::bevy_utils::NoOpHash); impl_type_path!(::bevy_utils::NoOpHash);
impl_type_path!(::bevy_utils::hashbrown::HashMap<K, V, S>); impl_type_path!(::bevy_utils::hashbrown::HashMap<K, V, S>);
macro_rules! impl_reflect_for_btree_map { impl<K, V> Map for ::std::collections::BTreeMap<K, V>
($ty:path) => { where
impl<K, V> Map for $ty K: FromReflect + TypePath + Eq + Ord,
where V: FromReflect + TypePath,
K: FromReflect + TypePath + Eq + Ord, {
V: FromReflect + TypePath, fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
{ key.downcast_ref::<K>()
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> { .and_then(|key| Self::get(self, key))
key.downcast_ref::<K>() .map(|value| value as &dyn Reflect)
.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> { fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> {
key.downcast_ref::<K>() key.downcast_ref::<K>()
.and_then(move |key| Self::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)
} }
fn get_at(&self, index: usize) -> Option<(&dyn Reflect, &dyn Reflect)> { fn get_at(&self, index: usize) -> Option<(&dyn Reflect, &dyn Reflect)> {
self.iter() self.iter()
.nth(index) .nth(index)
.map(|(key, value)| (key as &dyn Reflect, value as &dyn Reflect)) .map(|(key, value)| (key as &dyn Reflect, value as &dyn Reflect))
} }
fn get_at_mut(&mut self, index: usize) -> Option<(&dyn Reflect, &mut dyn Reflect)> { fn get_at_mut(&mut self, index: usize) -> Option<(&dyn Reflect, &mut dyn Reflect)> {
self.iter_mut() self.iter_mut()
.nth(index) .nth(index)
.map(|(key, value)| (key as &dyn Reflect, value as &mut dyn Reflect)) .map(|(key, value)| (key as &dyn Reflect, value as &mut dyn Reflect))
} }
fn len(&self) -> usize { fn len(&self) -> usize {
Self::len(self) Self::len(self)
} }
fn iter(&self) -> MapIter { fn iter(&self) -> MapIter {
MapIter::new(self) MapIter::new(self)
} }
fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)> { fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)> {
self.into_iter() self.into_iter()
.map(|(key, value)| { .map(|(key, value)| {
( (
Box::new(key) as Box<dyn Reflect>, Box::new(key) as Box<dyn Reflect>,
Box::new(value) as Box<dyn Reflect>, Box::new(value) as Box<dyn Reflect>,
) )
}) })
.collect() .collect()
} }
fn clone_dynamic(&self) -> DynamicMap { fn clone_dynamic(&self) -> DynamicMap {
let mut dynamic_map = DynamicMap::default(); let mut dynamic_map = DynamicMap::default();
dynamic_map.set_represented_type(self.get_represented_type_info()); dynamic_map.set_represented_type(self.get_represented_type_info());
for (k, v) in self { for (k, v) in self {
let key = K::from_reflect(k).unwrap_or_else(|| { let key = K::from_reflect(k).unwrap_or_else(|| {
panic!( panic!(
"Attempted to clone invalid key of type {}.", "Attempted to clone invalid key of type {}.",
k.reflect_type_path() k.reflect_type_path()
) )
}); });
dynamic_map.insert_boxed(Box::new(key), v.clone_value()); dynamic_map.insert_boxed(Box::new(key), v.clone_value());
}
dynamic_map
}
fn insert_boxed(
&mut self,
key: Box<dyn Reflect>,
value: Box<dyn Reflect>,
) -> Option<Box<dyn Reflect>> {
let key = K::take_from_reflect(key).unwrap_or_else(|key| {
panic!(
"Attempted to insert invalid key of type {}.",
key.reflect_type_path()
)
});
let value = V::take_from_reflect(value).unwrap_or_else(|value| {
panic!(
"Attempted to insert invalid value of type {}.",
value.reflect_type_path()
)
});
self.insert(key, value)
.map(|old_value| Box::new(old_value) as Box<dyn Reflect>)
}
fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>> {
let mut from_reflect = None;
key.downcast_ref::<K>()
.or_else(|| {
from_reflect = K::from_reflect(key);
from_reflect.as_ref()
})
.and_then(|key| self.remove(key))
.map(|value| Box::new(value) as Box<dyn Reflect>)
}
} }
dynamic_map
}
impl<K, V> Reflect for $ty fn insert_boxed(
where &mut self,
K: FromReflect + TypePath + Eq + Ord, key: Box<dyn Reflect>,
V: FromReflect + TypePath, value: Box<dyn Reflect>,
{ ) -> Option<Box<dyn Reflect>> {
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { let key = K::take_from_reflect(key).unwrap_or_else(|key| {
Some(<Self as Typed>::type_info()) panic!(
} "Attempted to insert invalid key of type {}.",
key.reflect_type_path()
)
});
let value = V::take_from_reflect(value).unwrap_or_else(|value| {
panic!(
"Attempted to insert invalid value of type {}.",
value.reflect_type_path()
)
});
self.insert(key, value)
.map(|old_value| Box::new(old_value) as Box<dyn Reflect>)
}
fn into_any(self: Box<Self>) -> Box<dyn Any> { fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>> {
self let mut from_reflect = None;
} key.downcast_ref::<K>()
.or_else(|| {
fn as_any(&self) -> &dyn Any { from_reflect = K::from_reflect(key);
self from_reflect.as_ref()
} })
.and_then(|key| self.remove(key))
fn as_any_mut(&mut self) -> &mut dyn Any { .map(|value| Box::new(value) as Box<dyn Reflect>)
self }
} }
#[inline] impl<K, V> Reflect for ::std::collections::BTreeMap<K, V>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { where
self K: FromReflect + TypePath + Eq + Ord,
} V: FromReflect + TypePath,
{
fn as_reflect(&self) -> &dyn Reflect { fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
self Some(<Self as Typed>::type_info())
} }
fn as_reflect_mut(&mut self) -> &mut dyn Reflect { fn into_any(self: Box<Self>) -> Box<dyn Any> {
self self
} }
fn apply(&mut self, value: &dyn Reflect) { fn as_any(&self) -> &dyn Any {
map_apply(self, value); self
} }
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> { fn as_any_mut(&mut self) -> &mut dyn Any {
*self = value.take()?; self
Ok(()) }
}
#[inline]
fn reflect_kind(&self) -> ReflectKind { fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
ReflectKind::Map self
} }
fn reflect_ref(&self) -> ReflectRef { fn as_reflect(&self) -> &dyn Reflect {
ReflectRef::Map(self) self
} }
fn reflect_mut(&mut self) -> ReflectMut { fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
ReflectMut::Map(self) self
} }
fn reflect_owned(self: Box<Self>) -> ReflectOwned { fn apply(&mut self, value: &dyn Reflect) {
ReflectOwned::Map(self) map_apply(self, value);
} }
fn clone_value(&self) -> Box<dyn Reflect> { fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
Box::new(self.clone_dynamic()) *self = value.take()?;
} Ok(())
}
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
map_partial_eq(self, value) fn reflect_kind(&self) -> ReflectKind {
} ReflectKind::Map
} }
impl<K, V> Typed for $ty fn reflect_ref(&self) -> ReflectRef {
where ReflectRef::Map(self)
K: FromReflect + TypePath + Eq + Ord, }
V: FromReflect + TypePath,
{ fn reflect_mut(&mut self) -> ReflectMut {
fn type_info() -> &'static TypeInfo { ReflectMut::Map(self)
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new(); }
CELL.get_or_insert::<Self, _>(|| TypeInfo::Map(MapInfo::new::<Self, K, V>()))
} fn reflect_owned(self: Box<Self>) -> ReflectOwned {
} ReflectOwned::Map(self)
}
impl<K, V> GetTypeRegistration for $ty
where fn clone_value(&self) -> Box<dyn Reflect> {
K: FromReflect + TypePath + Eq + Ord, Box::new(self.clone_dynamic())
V: FromReflect + TypePath, }
{
fn get_type_registration() -> TypeRegistration { fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
let mut registration = TypeRegistration::of::<Self>(); map_partial_eq(self, value)
registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type()); }
registration }
}
} impl<K, V> Typed for ::std::collections::BTreeMap<K, V>
where
impl<K, V> FromReflect for $ty K: FromReflect + TypePath + Eq + Ord,
where V: FromReflect + TypePath,
K: FromReflect + TypePath + Eq + Ord, {
V: FromReflect + TypePath, fn type_info() -> &'static TypeInfo {
{ static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> { CELL.get_or_insert::<Self, _>(|| TypeInfo::Map(MapInfo::new::<Self, K, V>()))
if let ReflectRef::Map(ref_map) = reflect.reflect_ref() { }
let mut new_map = Self::new(); }
for (key, value) in ref_map.iter() {
let new_key = K::from_reflect(key)?; impl<K, V> GetTypeRegistration for ::std::collections::BTreeMap<K, V>
let new_value = V::from_reflect(value)?; where
new_map.insert(new_key, new_value); K: FromReflect + TypePath + Eq + Ord,
} V: FromReflect + TypePath,
Some(new_map) {
} else { fn get_type_registration() -> TypeRegistration {
None let mut registration = TypeRegistration::of::<Self>();
} registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
} registration
} }
}; }
impl<K, V> FromReflect for ::std::collections::BTreeMap<K, V>
where
K: FromReflect + TypePath + Eq + Ord,
V: FromReflect + TypePath,
{
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
if let ReflectRef::Map(ref_map) = reflect.reflect_ref() {
let mut new_map = Self::new();
for (key, value) in ref_map.iter() {
let new_key = K::from_reflect(key)?;
let new_value = V::from_reflect(value)?;
new_map.insert(new_key, new_value);
}
Some(new_map)
} else {
None
}
}
} }
impl_reflect_for_btree_map!(::std::collections::BTreeMap<K, V>);
impl_type_path!(::std::collections::BTreeMap<K, V>); impl_type_path!(::std::collections::BTreeMap<K, V>);
impl<T: Reflect + TypePath, const N: usize> Array for [T; N] { impl<T: Reflect + TypePath, const N: usize> Array for [T; N] {