Path: blob/main/crates/bevy_reflect/src/impls/smallvec.rs
6599 views
use crate::{1utility::GenericTypeInfoCell, ApplyError, FromReflect, FromType, Generics, GetTypeRegistration,2List, ListInfo, ListIter, MaybeTyped, PartialReflect, Reflect, ReflectFromPtr, ReflectKind,3ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypeParamInfo, TypePath, TypeRegistration,4Typed,5};6use alloc::{boxed::Box, vec::Vec};7use bevy_reflect::ReflectCloneError;8use bevy_reflect_derive::impl_type_path;9use core::any::Any;10use smallvec::{Array as SmallArray, SmallVec};1112impl<T: SmallArray + TypePath + Send + Sync> List for SmallVec<T>13where14T::Item: FromReflect + MaybeTyped + TypePath,15{16fn get(&self, index: usize) -> Option<&dyn PartialReflect> {17if index < SmallVec::len(self) {18Some(&self[index] as &dyn PartialReflect)19} else {20None21}22}2324fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {25if index < SmallVec::len(self) {26Some(&mut self[index] as &mut dyn PartialReflect)27} else {28None29}30}3132fn insert(&mut self, index: usize, value: Box<dyn PartialReflect>) {33let value = value.try_take::<T::Item>().unwrap_or_else(|value| {34<T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {35panic!(36"Attempted to insert invalid value of type {}.",37value.reflect_type_path()38)39})40});41SmallVec::insert(self, index, value);42}4344fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {45Box::new(self.remove(index))46}4748fn push(&mut self, value: Box<dyn PartialReflect>) {49let value = value.try_take::<T::Item>().unwrap_or_else(|value| {50<T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {51panic!(52"Attempted to push invalid value of type {}.",53value.reflect_type_path()54)55})56});57SmallVec::push(self, value);58}5960fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {61self.pop()62.map(|value| Box::new(value) as Box<dyn PartialReflect>)63}6465fn len(&self) -> usize {66<SmallVec<T>>::len(self)67}6869fn iter(&self) -> ListIter<'_> {70ListIter::new(self)71}7273fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {74self.drain(..)75.map(|value| Box::new(value) as Box<dyn PartialReflect>)76.collect()77}78}7980impl<T: SmallArray + TypePath + Send + Sync> PartialReflect for SmallVec<T>81where82T::Item: FromReflect + MaybeTyped + TypePath,83{84fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {85Some(<Self as Typed>::type_info())86}8788#[inline]89fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {90self91}9293fn as_partial_reflect(&self) -> &dyn PartialReflect {94self95}9697fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {98self99}100101fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {102Ok(self)103}104105fn try_as_reflect(&self) -> Option<&dyn Reflect> {106Some(self)107}108109fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {110Some(self)111}112113fn apply(&mut self, value: &dyn PartialReflect) {114crate::list_apply(self, value);115}116117fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {118crate::list_try_apply(self, value)119}120121fn reflect_kind(&self) -> ReflectKind {122ReflectKind::List123}124125fn reflect_ref(&self) -> ReflectRef<'_> {126ReflectRef::List(self)127}128129fn reflect_mut(&mut self) -> ReflectMut<'_> {130ReflectMut::List(self)131}132133fn reflect_owned(self: Box<Self>) -> ReflectOwned {134ReflectOwned::List(self)135}136137fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {138Ok(Box::new(139// `(**self)` avoids getting `SmallVec<T> as List::iter`, which140// would give us the wrong item type.141(**self)142.iter()143.map(PartialReflect::reflect_clone_and_take)144.collect::<Result<Self, ReflectCloneError>>()?,145))146}147148fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {149crate::list_partial_eq(self, value)150}151}152153impl<T: SmallArray + TypePath + Send + Sync> Reflect for SmallVec<T>154where155T::Item: FromReflect + MaybeTyped + TypePath,156{157fn into_any(self: Box<Self>) -> Box<dyn Any> {158self159}160161fn as_any(&self) -> &dyn Any {162self163}164165fn as_any_mut(&mut self) -> &mut dyn Any {166self167}168169fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {170self171}172173fn as_reflect(&self) -> &dyn Reflect {174self175}176177fn as_reflect_mut(&mut self) -> &mut dyn Reflect {178self179}180181fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {182*self = value.take()?;183Ok(())184}185}186187impl<T: SmallArray + TypePath + Send + Sync + 'static> Typed for SmallVec<T>188where189T::Item: FromReflect + MaybeTyped + TypePath,190{191fn type_info() -> &'static TypeInfo {192static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();193CELL.get_or_insert::<Self, _>(|| {194TypeInfo::List(195ListInfo::new::<Self, T::Item>()196.with_generics(Generics::from_iter([TypeParamInfo::new::<T>("T")])),197)198})199}200}201202impl_type_path!(::smallvec::SmallVec<T: SmallArray>);203204impl<T: SmallArray + TypePath + Send + Sync> FromReflect for SmallVec<T>205where206T::Item: FromReflect + MaybeTyped + TypePath,207{208fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {209let ref_list = reflect.reflect_ref().as_list().ok()?;210211let mut new_list = Self::with_capacity(ref_list.len());212213for field in ref_list.iter() {214new_list.push(<T as SmallArray>::Item::from_reflect(field)?);215}216217Some(new_list)218}219}220221impl<T: SmallArray + TypePath + Send + Sync> GetTypeRegistration for SmallVec<T>222where223T::Item: FromReflect + MaybeTyped + TypePath,224{225fn get_type_registration() -> TypeRegistration {226let mut registration = TypeRegistration::of::<SmallVec<T>>();227registration.insert::<ReflectFromPtr>(FromType::<SmallVec<T>>::from_type());228registration229}230}231232#[cfg(feature = "functions")]233crate::func::macros::impl_function_traits!(SmallVec<T>; <T: SmallArray + TypePath + Send + Sync> where T::Item: FromReflect + MaybeTyped + TypePath);234235236