Skip to content

Commit 5e34a53

Browse files
committed
Add reflection support for VecDeque (#5792)
# Objective Fixes #5791 ## Solution Implemented all the required reflection traits for `VecDeque`, taking from `Vec`'s impls.
1 parent b6efe0f commit 5e34a53

File tree

1 file changed

+128
-0
lines changed
  • crates/bevy_reflect/src/impls

1 file changed

+128
-0
lines changed

crates/bevy_reflect/src/impls/std.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use bevy_utils::{Duration, HashMap, HashSet, Instant};
1313
use std::{
1414
any::Any,
1515
borrow::Cow,
16+
collections::VecDeque,
1617
hash::{Hash, Hasher},
1718
num::{
1819
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
@@ -238,6 +239,133 @@ impl<T: FromReflect> FromReflect for Vec<T> {
238239
}
239240
}
240241

242+
impl<T: FromReflect> Array for VecDeque<T> {
243+
#[inline]
244+
fn get(&self, index: usize) -> Option<&dyn Reflect> {
245+
VecDeque::get(self, index).map(|value| value as &dyn Reflect)
246+
}
247+
248+
#[inline]
249+
fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
250+
VecDeque::get_mut(self, index).map(|value| value as &mut dyn Reflect)
251+
}
252+
253+
#[inline]
254+
fn len(&self) -> usize {
255+
VecDeque::len(self)
256+
}
257+
258+
#[inline]
259+
fn iter(&self) -> ArrayIter {
260+
ArrayIter {
261+
array: self,
262+
index: 0,
263+
}
264+
}
265+
}
266+
267+
impl<T: FromReflect> List for VecDeque<T> {
268+
fn push(&mut self, value: Box<dyn Reflect>) {
269+
let value = value.take::<T>().unwrap_or_else(|value| {
270+
T::from_reflect(&*value).unwrap_or_else(|| {
271+
panic!(
272+
"Attempted to push invalid value of type {}.",
273+
value.type_name()
274+
)
275+
})
276+
});
277+
VecDeque::push_back(self, value);
278+
}
279+
}
280+
281+
impl<T: FromReflect> Reflect for VecDeque<T> {
282+
fn type_name(&self) -> &str {
283+
std::any::type_name::<Self>()
284+
}
285+
286+
fn get_type_info(&self) -> &'static TypeInfo {
287+
<Self as Typed>::type_info()
288+
}
289+
290+
fn into_any(self: Box<Self>) -> Box<dyn Any> {
291+
self
292+
}
293+
294+
fn as_any(&self) -> &dyn Any {
295+
self
296+
}
297+
298+
fn as_any_mut(&mut self) -> &mut dyn Any {
299+
self
300+
}
301+
302+
fn as_reflect(&self) -> &dyn Reflect {
303+
self
304+
}
305+
306+
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
307+
self
308+
}
309+
310+
fn apply(&mut self, value: &dyn Reflect) {
311+
crate::list_apply(self, value);
312+
}
313+
314+
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
315+
*self = value.take()?;
316+
Ok(())
317+
}
318+
319+
fn reflect_ref(&self) -> ReflectRef {
320+
ReflectRef::List(self)
321+
}
322+
323+
fn reflect_mut(&mut self) -> ReflectMut {
324+
ReflectMut::List(self)
325+
}
326+
327+
fn clone_value(&self) -> Box<dyn Reflect> {
328+
Box::new(List::clone_dynamic(self))
329+
}
330+
331+
fn reflect_hash(&self) -> Option<u64> {
332+
crate::array_hash(self)
333+
}
334+
335+
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
336+
crate::list_partial_eq(self, value)
337+
}
338+
}
339+
340+
impl<T: FromReflect> Typed for VecDeque<T> {
341+
fn type_info() -> &'static TypeInfo {
342+
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
343+
CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))
344+
}
345+
}
346+
347+
impl<T: FromReflect> GetTypeRegistration for VecDeque<T> {
348+
fn get_type_registration() -> TypeRegistration {
349+
let mut registration = TypeRegistration::of::<VecDeque<T>>();
350+
registration.insert::<ReflectFromPtr>(FromType::<VecDeque<T>>::from_type());
351+
registration
352+
}
353+
}
354+
355+
impl<T: FromReflect> FromReflect for VecDeque<T> {
356+
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
357+
if let ReflectRef::List(ref_list) = reflect.reflect_ref() {
358+
let mut new_list = Self::with_capacity(ref_list.len());
359+
for field in ref_list.iter() {
360+
new_list.push_back(T::from_reflect(field)?);
361+
}
362+
Some(new_list)
363+
} else {
364+
None
365+
}
366+
}
367+
}
368+
241369
impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
242370
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
243371
key.downcast_ref::<K>()

0 commit comments

Comments
 (0)