bevy_reflect/impls/
smallvec.rs1use bevy_reflect_derive::impl_type_path;
2use smallvec::{Array as SmallArray, SmallVec};
3
4use core::any::Any;
5
6use crate::{
7 self as bevy_reflect, utility::GenericTypeInfoCell, ApplyError, FromReflect, FromType,
8 Generics, GetTypeRegistration, List, ListInfo, ListIter, MaybeTyped, PartialReflect, Reflect,
9 ReflectFromPtr, ReflectKind, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypeParamInfo,
10 TypePath, TypeRegistration, Typed,
11};
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 clone_value(&self) -> Box<dyn PartialReflect> {
138 Box::new(self.clone_dynamic())
139 }
140
141 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
142 crate::list_partial_eq(self, value)
143 }
144}
145
146impl<T: SmallArray + TypePath + Send + Sync> Reflect for SmallVec<T>
147where
148 T::Item: FromReflect + MaybeTyped + TypePath,
149{
150 fn into_any(self: Box<Self>) -> Box<dyn Any> {
151 self
152 }
153
154 fn as_any(&self) -> &dyn Any {
155 self
156 }
157
158 fn as_any_mut(&mut self) -> &mut dyn Any {
159 self
160 }
161
162 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
163 self
164 }
165
166 fn as_reflect(&self) -> &dyn Reflect {
167 self
168 }
169
170 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
171 self
172 }
173
174 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
175 *self = value.take()?;
176 Ok(())
177 }
178}
179
180impl<T: SmallArray + TypePath + Send + Sync + 'static> Typed for SmallVec<T>
181where
182 T::Item: FromReflect + MaybeTyped + TypePath,
183{
184 fn type_info() -> &'static TypeInfo {
185 static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
186 CELL.get_or_insert::<Self, _>(|| {
187 TypeInfo::List(
188 ListInfo::new::<Self, T::Item>()
189 .with_generics(Generics::from_iter([TypeParamInfo::new::<T>("T")])),
190 )
191 })
192 }
193}
194
195impl_type_path!(::smallvec::SmallVec<T: SmallArray>);
196
197impl<T: SmallArray + TypePath + Send + Sync> FromReflect for SmallVec<T>
198where
199 T::Item: FromReflect + MaybeTyped + TypePath,
200{
201 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
202 let ref_list = reflect.reflect_ref().as_list().ok()?;
203
204 let mut new_list = Self::with_capacity(ref_list.len());
205
206 for field in ref_list.iter() {
207 new_list.push(<T as SmallArray>::Item::from_reflect(field)?);
208 }
209
210 Some(new_list)
211 }
212}
213
214impl<T: SmallArray + TypePath + Send + Sync> GetTypeRegistration for SmallVec<T>
215where
216 T::Item: FromReflect + MaybeTyped + TypePath,
217{
218 fn get_type_registration() -> TypeRegistration {
219 let mut registration = TypeRegistration::of::<SmallVec<T>>();
220 registration.insert::<ReflectFromPtr>(FromType::<SmallVec<T>>::from_type());
221 registration
222 }
223}
224
225#[cfg(feature = "functions")]
226crate::func::macros::impl_function_traits!(SmallVec<T>; <T: SmallArray + TypePath + Send + Sync> where T::Item: FromReflect + MaybeTyped + TypePath);