Path: blob/main/crates/bevy_reflect/src/impls/smallvec.rs
9349 views
use crate::{1list::{List, ListInfo, ListIter},2utility::GenericTypeInfoCell,3ApplyError, FromReflect, FromType, Generics, GetTypeRegistration, MaybeTyped, PartialReflect,4Reflect, ReflectFromPtr, ReflectKind, ReflectMut, ReflectOwned, ReflectRef, TypeInfo,5TypeParamInfo, TypePath, TypeRegistration, Typed,6};7use alloc::{boxed::Box, vec::Vec};8use bevy_reflect::ReflectCloneError;9use bevy_reflect_derive::impl_type_path;10use core::any::Any;11use smallvec::{Array as SmallArray, SmallVec};1213impl<T: SmallArray + TypePath + Send + Sync> List for SmallVec<T>14where15T::Item: FromReflect + MaybeTyped + TypePath,16{17fn get(&self, index: usize) -> Option<&dyn PartialReflect> {18if index < SmallVec::len(self) {19Some(&self[index] as &dyn PartialReflect)20} else {21None22}23}2425fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {26if index < SmallVec::len(self) {27Some(&mut self[index] as &mut dyn PartialReflect)28} else {29None30}31}3233fn insert(&mut self, index: usize, value: Box<dyn PartialReflect>) {34let value = value.try_take::<T::Item>().unwrap_or_else(|value| {35<T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {36panic!(37"Attempted to insert invalid value of type {}.",38value.reflect_type_path()39)40})41});42SmallVec::insert(self, index, value);43}4445fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {46Box::new(self.remove(index))47}4849fn push(&mut self, value: Box<dyn PartialReflect>) {50let value = value.try_take::<T::Item>().unwrap_or_else(|value| {51<T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {52panic!(53"Attempted to push invalid value of type {}.",54value.reflect_type_path()55)56})57});58SmallVec::push(self, value);59}6061fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {62self.pop()63.map(|value| Box::new(value) as Box<dyn PartialReflect>)64}6566fn len(&self) -> usize {67<SmallVec<T>>::len(self)68}6970fn iter(&self) -> ListIter<'_> {71ListIter::new(self)72}7374fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {75self.drain(..)76.map(|value| Box::new(value) as Box<dyn PartialReflect>)77.collect()78}79}8081impl<T: SmallArray + TypePath + Send + Sync> PartialReflect for SmallVec<T>82where83T::Item: FromReflect + MaybeTyped + TypePath,84{85fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {86Some(<Self as Typed>::type_info())87}8889#[inline]90fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {91self92}9394fn as_partial_reflect(&self) -> &dyn PartialReflect {95self96}9798fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {99self100}101102fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {103Ok(self)104}105106fn try_as_reflect(&self) -> Option<&dyn Reflect> {107Some(self)108}109110fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {111Some(self)112}113114fn apply(&mut self, value: &dyn PartialReflect) {115crate::list::list_apply(self, value);116}117118fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {119crate::list::list_try_apply(self, value)120}121122fn reflect_kind(&self) -> ReflectKind {123ReflectKind::List124}125126fn reflect_ref(&self) -> ReflectRef<'_> {127ReflectRef::List(self)128}129130fn reflect_mut(&mut self) -> ReflectMut<'_> {131ReflectMut::List(self)132}133134fn reflect_owned(self: Box<Self>) -> ReflectOwned {135ReflectOwned::List(self)136}137138fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {139Ok(Box::new(140// `(**self)` avoids getting `SmallVec<T> as List::iter`, which141// would give us the wrong item type.142(**self)143.iter()144.map(PartialReflect::reflect_clone_and_take)145.collect::<Result<Self, ReflectCloneError>>()?,146))147}148149fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {150crate::list::list_partial_eq(self, value)151}152153fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<core::cmp::Ordering> {154crate::list::list_partial_cmp(self, value)155}156}157158impl<T: SmallArray + TypePath + Send + Sync> Reflect for SmallVec<T>159where160T::Item: FromReflect + MaybeTyped + TypePath,161{162fn into_any(self: Box<Self>) -> Box<dyn Any> {163self164}165166fn as_any(&self) -> &dyn Any {167self168}169170fn as_any_mut(&mut self) -> &mut dyn Any {171self172}173174fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {175self176}177178fn as_reflect(&self) -> &dyn Reflect {179self180}181182fn as_reflect_mut(&mut self) -> &mut dyn Reflect {183self184}185186fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {187*self = value.take()?;188Ok(())189}190}191192impl<T: SmallArray + TypePath + Send + Sync + 'static> Typed for SmallVec<T>193where194T::Item: FromReflect + MaybeTyped + TypePath,195{196fn type_info() -> &'static TypeInfo {197static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();198CELL.get_or_insert::<Self, _>(|| {199TypeInfo::List(200ListInfo::new::<Self, T::Item>()201.with_generics(Generics::from_iter([TypeParamInfo::new::<T>("T")])),202)203})204}205}206207impl_type_path!(::smallvec::SmallVec<T: SmallArray>);208209impl<T: SmallArray + TypePath + Send + Sync> FromReflect for SmallVec<T>210where211T::Item: FromReflect + MaybeTyped + TypePath,212{213fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {214let ref_list = reflect.reflect_ref().as_list().ok()?;215216let mut new_list = Self::with_capacity(ref_list.len());217218for field in ref_list.iter() {219new_list.push(<T as SmallArray>::Item::from_reflect(field)?);220}221222Some(new_list)223}224}225226impl<T: SmallArray + TypePath + Send + Sync> GetTypeRegistration for SmallVec<T>227where228T::Item: FromReflect + MaybeTyped + TypePath,229{230fn get_type_registration() -> TypeRegistration {231let mut registration = TypeRegistration::of::<SmallVec<T>>();232registration.insert::<ReflectFromPtr>(FromType::<SmallVec<T>>::from_type());233registration234}235}236237#[cfg(feature = "functions")]238crate::func::macros::impl_function_traits!(SmallVec<T>; <T: SmallArray + TypePath + Send + Sync> where T::Item: FromReflect + MaybeTyped + TypePath);239240241