Add reflection support for VecDeque (#6831)
# Objective This is an adoption of #5792. Fixes #5791. ## Solution Implemented all the required reflection traits for `VecDeque`, taking from `Vec`'s impls. --- ## Changelog Added: `std::collections::VecDeque` now implements `Reflect` and all relevant traits. Co-authored-by: james7132 <contact@jamessliu.com>
This commit is contained in:
parent
6903a9411b
commit
b37a6ca9a2
@ -15,6 +15,7 @@ use bevy_utils::{HashMap, HashSet};
|
||||
use std::{
|
||||
any::Any,
|
||||
borrow::Cow,
|
||||
collections::VecDeque,
|
||||
ffi::OsString,
|
||||
hash::{Hash, Hasher},
|
||||
num::{
|
||||
@ -177,20 +178,22 @@ impl_from_reflect_value!(NonZeroU16);
|
||||
impl_from_reflect_value!(NonZeroU8);
|
||||
impl_from_reflect_value!(NonZeroI8);
|
||||
|
||||
impl<T: FromReflect> Array for Vec<T> {
|
||||
macro_rules! impl_reflect_for_veclike {
|
||||
($ty:ty, $push:expr, $pop:expr, $sub:ty) => {
|
||||
impl<T: FromReflect> Array for $ty {
|
||||
#[inline]
|
||||
fn get(&self, index: usize) -> Option<&dyn Reflect> {
|
||||
<[T]>::get(self, index).map(|value| value as &dyn Reflect)
|
||||
<$sub>::get(self, index).map(|value| value as &dyn Reflect)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
|
||||
<[T]>::get_mut(self, index).map(|value| value as &mut dyn Reflect)
|
||||
<$sub>::get_mut(self, index).map(|value| value as &mut dyn Reflect)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn len(&self) -> usize {
|
||||
<[T]>::len(self)
|
||||
<$sub>::len(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -209,7 +212,7 @@ impl<T: FromReflect> Array for Vec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromReflect> List for Vec<T> {
|
||||
impl<T: FromReflect> List for $ty {
|
||||
fn push(&mut self, value: Box<dyn Reflect>) {
|
||||
let value = value.take::<T>().unwrap_or_else(|value| {
|
||||
T::from_reflect(&*value).unwrap_or_else(|| {
|
||||
@ -219,15 +222,15 @@ impl<T: FromReflect> List for Vec<T> {
|
||||
)
|
||||
})
|
||||
});
|
||||
Vec::push(self, value);
|
||||
$push(self, value);
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> Option<Box<dyn Reflect>> {
|
||||
self.pop().map(|value| Box::new(value) as Box<dyn Reflect>)
|
||||
$pop(self).map(|value| Box::new(value) as Box<dyn Reflect>)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromReflect> Reflect for Vec<T> {
|
||||
impl<T: FromReflect> Reflect for $ty {
|
||||
fn type_name(&self) -> &str {
|
||||
std::any::type_name::<Self>()
|
||||
}
|
||||
@ -294,14 +297,14 @@ impl<T: FromReflect> Reflect for Vec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromReflect> Typed for Vec<T> {
|
||||
impl<T: FromReflect> Typed for $ty {
|
||||
fn type_info() -> &'static TypeInfo {
|
||||
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
|
||||
CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromReflect> GetTypeRegistration for Vec<T> {
|
||||
impl<T: FromReflect> GetTypeRegistration for $ty {
|
||||
fn get_type_registration() -> TypeRegistration {
|
||||
let mut registration = TypeRegistration::of::<Vec<T>>();
|
||||
registration.insert::<ReflectFromPtr>(FromType::<Vec<T>>::from_type());
|
||||
@ -309,12 +312,12 @@ impl<T: FromReflect> GetTypeRegistration for Vec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromReflect> FromReflect for Vec<T> {
|
||||
impl<T: FromReflect> FromReflect for $ty {
|
||||
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
|
||||
if let ReflectRef::List(ref_list) = reflect.reflect_ref() {
|
||||
let mut new_list = Self::with_capacity(ref_list.len());
|
||||
for field in ref_list.iter() {
|
||||
new_list.push(T::from_reflect(field)?);
|
||||
$push(&mut new_list, T::from_reflect(field)?);
|
||||
}
|
||||
Some(new_list)
|
||||
} else {
|
||||
@ -322,6 +325,16 @@ impl<T: FromReflect> FromReflect for Vec<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_reflect_for_veclike!(Vec<T>, Vec::push, Vec::pop, [T]);
|
||||
impl_reflect_for_veclike!(
|
||||
VecDeque<T>,
|
||||
VecDeque::push_back,
|
||||
VecDeque::pop_back,
|
||||
VecDeque::<T>
|
||||
);
|
||||
|
||||
impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
|
||||
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
|
||||
|
Loading…
Reference in New Issue
Block a user