mirror of
https://github.com/bevyengine/bevy
synced 2024-12-21 10:33:08 +00:00
acbee7795d
# Objective > ℹ️ **Note**: This is a rebased version of #2383. A large portion of it has not been touched (only a few minor changes) so that any additional discussion may happen here. All credit should go to @NathanSWard for their work on the original PR. - Currently reflection is not supported for arrays. - Fixes #1213 ## Solution * Implement reflection for arrays via the `Array` trait. * Note, `Array` is different from `List` in the way that you cannot push elements onto an array as they are statically sized. * Now `List` is defined as a sub-trait of `Array`. --- ## Changelog * Added the `Array` reflection trait * Allows arrays up to length 32 to be reflected via the `Array` trait ## Migration Guide * The `List` trait now has the `Array` supertrait. This means that `clone_dynamic` will need to specify which version to use: ```rust // Before let cloned = my_list.clone_dynamic(); // After let cloned = List::clone_dynamic(&my_list); ``` * All implementers of `List` will now need to implement `Array` (this mostly involves moving the existing methods to the `Array` impl) Co-authored-by: NathanW <nathansward@comcast.net> Co-authored-by: MrGVSV <49806985+MrGVSV@users.noreply.github.com>
131 lines
3.1 KiB
Rust
131 lines
3.1 KiB
Rust
use smallvec::SmallVec;
|
|
use std::any::Any;
|
|
|
|
use crate::{
|
|
serde::Serializable, Array, ArrayIter, FromReflect, List, Reflect, ReflectMut, ReflectRef,
|
|
};
|
|
|
|
impl<T: smallvec::Array + Send + Sync + 'static> Array for SmallVec<T>
|
|
where
|
|
T::Item: FromReflect + Clone,
|
|
{
|
|
fn get(&self, index: usize) -> Option<&dyn Reflect> {
|
|
if index < SmallVec::len(self) {
|
|
Some(&self[index] as &dyn Reflect)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
|
|
if index < SmallVec::len(self) {
|
|
Some(&mut self[index] as &mut dyn Reflect)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn len(&self) -> usize {
|
|
<SmallVec<T>>::len(self)
|
|
}
|
|
|
|
fn iter(&self) -> ArrayIter {
|
|
ArrayIter {
|
|
array: self,
|
|
index: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: smallvec::Array + Send + Sync + 'static> List for SmallVec<T>
|
|
where
|
|
T::Item: FromReflect + Clone,
|
|
{
|
|
fn push(&mut self, value: Box<dyn Reflect>) {
|
|
let value = value.take::<T::Item>().unwrap_or_else(|value| {
|
|
<T as smallvec::Array>::Item::from_reflect(&*value).unwrap_or_else(|| {
|
|
panic!(
|
|
"Attempted to push invalid value of type {}.",
|
|
value.type_name()
|
|
)
|
|
})
|
|
});
|
|
SmallVec::push(self, value);
|
|
}
|
|
}
|
|
|
|
// SAFE: any and any_mut both return self
|
|
unsafe impl<T: smallvec::Array + Send + Sync + 'static> Reflect for SmallVec<T>
|
|
where
|
|
T::Item: FromReflect + Clone,
|
|
{
|
|
fn type_name(&self) -> &str {
|
|
std::any::type_name::<Self>()
|
|
}
|
|
|
|
fn any(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
|
|
fn any_mut(&mut self) -> &mut dyn Any {
|
|
self
|
|
}
|
|
|
|
fn as_reflect(&self) -> &dyn Reflect {
|
|
self
|
|
}
|
|
|
|
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
|
|
self
|
|
}
|
|
|
|
fn apply(&mut self, value: &dyn Reflect) {
|
|
crate::list_apply(self, value);
|
|
}
|
|
|
|
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
|
|
*self = value.take()?;
|
|
Ok(())
|
|
}
|
|
|
|
fn reflect_ref(&self) -> ReflectRef {
|
|
ReflectRef::List(self)
|
|
}
|
|
|
|
fn reflect_mut(&mut self) -> ReflectMut {
|
|
ReflectMut::List(self)
|
|
}
|
|
|
|
fn clone_value(&self) -> Box<dyn Reflect> {
|
|
Box::new(List::clone_dynamic(self))
|
|
}
|
|
|
|
fn reflect_hash(&self) -> Option<u64> {
|
|
None
|
|
}
|
|
|
|
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
|
|
crate::list_partial_eq(self, value)
|
|
}
|
|
|
|
fn serializable(&self) -> Option<Serializable> {
|
|
None
|
|
}
|
|
}
|
|
|
|
impl<T: smallvec::Array + Send + Sync + 'static> FromReflect for SmallVec<T>
|
|
where
|
|
T::Item: FromReflect + Clone,
|
|
{
|
|
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 as smallvec::Array>::Item::from_reflect(field)?);
|
|
}
|
|
Some(new_list)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|