bevy_reflect/impls/
smallvec.rs1use crate::{
2 utility::GenericTypeInfoCell, ApplyError, FromReflect, FromType, Generics, GetTypeRegistration,
3 List, ListInfo, ListIter, MaybeTyped, PartialReflect, Reflect, ReflectFromPtr, ReflectKind,
4 ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypeParamInfo, TypePath, TypeRegistration,
5 Typed,
6};
7use alloc::{borrow::Cow, boxed::Box, string::ToString, vec::Vec};
8use bevy_reflect::ReflectCloneError;
9use bevy_reflect_derive::impl_type_path;
10use core::any::Any;
11use smallvec::{Array as SmallArray, SmallVec};
12
13impl<T: SmallArray + TypePath + Send + Sync> List for SmallVec<T>
14where
15 T::Item: FromReflect + MaybeTyped + TypePath,
16{
17 fn get(&self, index: usize) -> Option<&dyn PartialReflect> {
18 if index < SmallVec::len(self) {
19 Some(&self[index] as &dyn PartialReflect)
20 } else {
21 None
22 }
23 }
24
25 fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {
26 if index < SmallVec::len(self) {
27 Some(&mut self[index] as &mut dyn PartialReflect)
28 } else {
29 None
30 }
31 }
32
33 fn insert(&mut self, index: usize, value: Box<dyn PartialReflect>) {
34 let value = value.try_take::<T::Item>().unwrap_or_else(|value| {
35 <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {
36 panic!(
37 "Attempted to insert invalid value of type {}.",
38 value.reflect_type_path()
39 )
40 })
41 });
42 SmallVec::insert(self, index, value);
43 }
44
45 fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {
46 Box::new(self.remove(index))
47 }
48
49 fn push(&mut self, value: Box<dyn PartialReflect>) {
50 let value = value.try_take::<T::Item>().unwrap_or_else(|value| {
51 <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {
52 panic!(
53 "Attempted to push invalid value of type {}.",
54 value.reflect_type_path()
55 )
56 })
57 });
58 SmallVec::push(self, value);
59 }
60
61 fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {
62 self.pop()
63 .map(|value| Box::new(value) as Box<dyn PartialReflect>)
64 }
65
66 fn len(&self) -> usize {
67 <SmallVec<T>>::len(self)
68 }
69
70 fn iter(&self) -> ListIter {
71 ListIter::new(self)
72 }
73
74 fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {
75 self.drain(..)
76 .map(|value| Box::new(value) as Box<dyn PartialReflect>)
77 .collect()
78 }
79}
80impl<T: SmallArray + TypePath + Send + Sync> PartialReflect for SmallVec<T>
81where
82 T::Item: FromReflect + MaybeTyped + TypePath,
83{
84 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
85 Some(<Self as Typed>::type_info())
86 }
87
88 #[inline]
89 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
90 self
91 }
92
93 fn as_partial_reflect(&self) -> &dyn PartialReflect {
94 self
95 }
96
97 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
98 self
99 }
100
101 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
102 Ok(self)
103 }
104
105 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
106 Some(self)
107 }
108
109 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
110 Some(self)
111 }
112
113 fn apply(&mut self, value: &dyn PartialReflect) {
114 crate::list_apply(self, value);
115 }
116
117 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
118 crate::list_try_apply(self, value)
119 }
120
121 fn reflect_kind(&self) -> ReflectKind {
122 ReflectKind::List
123 }
124
125 fn reflect_ref(&self) -> ReflectRef {
126 ReflectRef::List(self)
127 }
128
129 fn reflect_mut(&mut self) -> ReflectMut {
130 ReflectMut::List(self)
131 }
132
133 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
134 ReflectOwned::List(self)
135 }
136
137 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
138 Ok(Box::new(
139 self.iter()
140 .map(|value| {
141 value
142 .reflect_clone()?
143 .take()
144 .map_err(|_| ReflectCloneError::FailedDowncast {
145 expected: Cow::Borrowed(<T::Item as TypePath>::type_path()),
146 received: Cow::Owned(value.reflect_type_path().to_string()),
147 })
148 })
149 .collect::<Result<Self, ReflectCloneError>>()?,
150 ))
151 }
152
153 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
154 crate::list_partial_eq(self, value)
155 }
156}
157
158impl<T: SmallArray + TypePath + Send + Sync> Reflect for SmallVec<T>
159where
160 T::Item: FromReflect + MaybeTyped + TypePath,
161{
162 fn into_any(self: Box<Self>) -> Box<dyn Any> {
163 self
164 }
165
166 fn as_any(&self) -> &dyn Any {
167 self
168 }
169
170 fn as_any_mut(&mut self) -> &mut dyn Any {
171 self
172 }
173
174 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
175 self
176 }
177
178 fn as_reflect(&self) -> &dyn Reflect {
179 self
180 }
181
182 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
183 self
184 }
185
186 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
187 *self = value.take()?;
188 Ok(())
189 }
190}
191
192impl<T: SmallArray + TypePath + Send + Sync + 'static> Typed for SmallVec<T>
193where
194 T::Item: FromReflect + MaybeTyped + TypePath,
195{
196 fn type_info() -> &'static TypeInfo {
197 static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
198 CELL.get_or_insert::<Self, _>(|| {
199 TypeInfo::List(
200 ListInfo::new::<Self, T::Item>()
201 .with_generics(Generics::from_iter([TypeParamInfo::new::<T>("T")])),
202 )
203 })
204 }
205}
206
207impl_type_path!(::smallvec::SmallVec<T: SmallArray>);
208
209impl<T: SmallArray + TypePath + Send + Sync> FromReflect for SmallVec<T>
210where
211 T::Item: FromReflect + MaybeTyped + TypePath,
212{
213 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
214 let ref_list = reflect.reflect_ref().as_list().ok()?;
215
216 let mut new_list = Self::with_capacity(ref_list.len());
217
218 for field in ref_list.iter() {
219 new_list.push(<T as SmallArray>::Item::from_reflect(field)?);
220 }
221
222 Some(new_list)
223 }
224}
225
226impl<T: SmallArray + TypePath + Send + Sync> GetTypeRegistration for SmallVec<T>
227where
228 T::Item: FromReflect + MaybeTyped + TypePath,
229{
230 fn get_type_registration() -> TypeRegistration {
231 let mut registration = TypeRegistration::of::<SmallVec<T>>();
232 registration.insert::<ReflectFromPtr>(FromType::<SmallVec<T>>::from_type());
233 registration
234 }
235}
236
237#[cfg(feature = "functions")]
238crate::func::macros::impl_function_traits!(SmallVec<T>; <T: SmallArray + TypePath + Send + Sync> where T::Item: FromReflect + MaybeTyped + TypePath);