Skip to content

Commit ace8653

Browse files
ItsDootjames7132
authored andcommitted
Add reflection support for VecDeque (bevyengine#6831)
# Objective This is an adoption of bevyengine#5792. Fixes bevyengine#5791. ## Solution Implemented all the required reflection traits for `VecDeque`, taking from `Vec`'s impls. --- ## Changelog Added: `std::collections::VecDeque` now implements `Reflect` and all relevant traits. Co-authored-by: james7132 <[email protected]>
1 parent 09b27d7 commit ace8653

File tree

1 file changed

+130
-117
lines changed
  • crates/bevy_reflect/src/impls

1 file changed

+130
-117
lines changed

crates/bevy_reflect/src/impls/std.rs

Lines changed: 130 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bevy_utils::{HashMap, HashSet};
1515
use std::{
1616
any::Any,
1717
borrow::Cow,
18+
collections::VecDeque,
1819
ffi::OsString,
1920
hash::{Hash, Hasher},
2021
num::{
@@ -177,152 +178,164 @@ impl_from_reflect_value!(NonZeroU16);
177178
impl_from_reflect_value!(NonZeroU8);
178179
impl_from_reflect_value!(NonZeroI8);
179180

180-
impl<T: FromReflect> Array for Vec<T> {
181-
#[inline]
182-
fn get(&self, index: usize) -> Option<&dyn Reflect> {
183-
<[T]>::get(self, index).map(|value| value as &dyn Reflect)
184-
}
181+
macro_rules! impl_reflect_for_veclike {
182+
($ty:ty, $push:expr, $pop:expr, $sub:ty) => {
183+
impl<T: FromReflect> Array for $ty {
184+
#[inline]
185+
fn get(&self, index: usize) -> Option<&dyn Reflect> {
186+
<$sub>::get(self, index).map(|value| value as &dyn Reflect)
187+
}
185188

186-
#[inline]
187-
fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
188-
<[T]>::get_mut(self, index).map(|value| value as &mut dyn Reflect)
189-
}
189+
#[inline]
190+
fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
191+
<$sub>::get_mut(self, index).map(|value| value as &mut dyn Reflect)
192+
}
190193

191-
#[inline]
192-
fn len(&self) -> usize {
193-
<[T]>::len(self)
194-
}
194+
#[inline]
195+
fn len(&self) -> usize {
196+
<$sub>::len(self)
197+
}
195198

196-
#[inline]
197-
fn iter(&self) -> ArrayIter {
198-
ArrayIter {
199-
array: self,
200-
index: 0,
201-
}
202-
}
199+
#[inline]
200+
fn iter(&self) -> ArrayIter {
201+
ArrayIter {
202+
array: self,
203+
index: 0,
204+
}
205+
}
203206

204-
#[inline]
205-
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
206-
self.into_iter()
207-
.map(|value| Box::new(value) as Box<dyn Reflect>)
208-
.collect()
209-
}
210-
}
207+
#[inline]
208+
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
209+
self.into_iter()
210+
.map(|value| Box::new(value) as Box<dyn Reflect>)
211+
.collect()
212+
}
213+
}
211214

212-
impl<T: FromReflect> List for Vec<T> {
213-
fn push(&mut self, value: Box<dyn Reflect>) {
214-
let value = value.take::<T>().unwrap_or_else(|value| {
215-
T::from_reflect(&*value).unwrap_or_else(|| {
216-
panic!(
217-
"Attempted to push invalid value of type {}.",
218-
value.type_name()
219-
)
220-
})
221-
});
222-
Vec::push(self, value);
223-
}
215+
impl<T: FromReflect> List for $ty {
216+
fn push(&mut self, value: Box<dyn Reflect>) {
217+
let value = value.take::<T>().unwrap_or_else(|value| {
218+
T::from_reflect(&*value).unwrap_or_else(|| {
219+
panic!(
220+
"Attempted to push invalid value of type {}.",
221+
value.type_name()
222+
)
223+
})
224+
});
225+
$push(self, value);
226+
}
224227

225-
fn pop(&mut self) -> Option<Box<dyn Reflect>> {
226-
self.pop().map(|value| Box::new(value) as Box<dyn Reflect>)
227-
}
228-
}
228+
fn pop(&mut self) -> Option<Box<dyn Reflect>> {
229+
$pop(self).map(|value| Box::new(value) as Box<dyn Reflect>)
230+
}
231+
}
229232

230-
impl<T: FromReflect> Reflect for Vec<T> {
231-
fn type_name(&self) -> &str {
232-
std::any::type_name::<Self>()
233-
}
233+
impl<T: FromReflect> Reflect for $ty {
234+
fn type_name(&self) -> &str {
235+
std::any::type_name::<Self>()
236+
}
234237

235-
fn get_type_info(&self) -> &'static TypeInfo {
236-
<Self as Typed>::type_info()
237-
}
238+
fn get_type_info(&self) -> &'static TypeInfo {
239+
<Self as Typed>::type_info()
240+
}
238241

239-
fn into_any(self: Box<Self>) -> Box<dyn Any> {
240-
self
241-
}
242+
fn into_any(self: Box<Self>) -> Box<dyn Any> {
243+
self
244+
}
242245

243-
fn as_any(&self) -> &dyn Any {
244-
self
245-
}
246+
fn as_any(&self) -> &dyn Any {
247+
self
248+
}
246249

247-
fn as_any_mut(&mut self) -> &mut dyn Any {
248-
self
249-
}
250+
fn as_any_mut(&mut self) -> &mut dyn Any {
251+
self
252+
}
250253

251-
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
252-
self
253-
}
254+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
255+
self
256+
}
254257

255-
fn as_reflect(&self) -> &dyn Reflect {
256-
self
257-
}
258+
fn as_reflect(&self) -> &dyn Reflect {
259+
self
260+
}
258261

259-
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
260-
self
261-
}
262+
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
263+
self
264+
}
262265

263-
fn apply(&mut self, value: &dyn Reflect) {
264-
crate::list_apply(self, value);
265-
}
266+
fn apply(&mut self, value: &dyn Reflect) {
267+
crate::list_apply(self, value);
268+
}
266269

267-
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
268-
*self = value.take()?;
269-
Ok(())
270-
}
270+
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
271+
*self = value.take()?;
272+
Ok(())
273+
}
271274

272-
fn reflect_ref(&self) -> ReflectRef {
273-
ReflectRef::List(self)
274-
}
275+
fn reflect_ref(&self) -> ReflectRef {
276+
ReflectRef::List(self)
277+
}
275278

276-
fn reflect_mut(&mut self) -> ReflectMut {
277-
ReflectMut::List(self)
278-
}
279+
fn reflect_mut(&mut self) -> ReflectMut {
280+
ReflectMut::List(self)
281+
}
279282

280-
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
281-
ReflectOwned::List(self)
282-
}
283+
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
284+
ReflectOwned::List(self)
285+
}
283286

284-
fn clone_value(&self) -> Box<dyn Reflect> {
285-
Box::new(List::clone_dynamic(self))
286-
}
287+
fn clone_value(&self) -> Box<dyn Reflect> {
288+
Box::new(List::clone_dynamic(self))
289+
}
287290

288-
fn reflect_hash(&self) -> Option<u64> {
289-
crate::array_hash(self)
290-
}
291+
fn reflect_hash(&self) -> Option<u64> {
292+
crate::array_hash(self)
293+
}
291294

292-
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
293-
crate::list_partial_eq(self, value)
294-
}
295-
}
295+
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
296+
crate::list_partial_eq(self, value)
297+
}
298+
}
296299

297-
impl<T: FromReflect> Typed for Vec<T> {
298-
fn type_info() -> &'static TypeInfo {
299-
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
300-
CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))
301-
}
302-
}
300+
impl<T: FromReflect> Typed for $ty {
301+
fn type_info() -> &'static TypeInfo {
302+
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
303+
CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))
304+
}
305+
}
303306

304-
impl<T: FromReflect> GetTypeRegistration for Vec<T> {
305-
fn get_type_registration() -> TypeRegistration {
306-
let mut registration = TypeRegistration::of::<Vec<T>>();
307-
registration.insert::<ReflectFromPtr>(FromType::<Vec<T>>::from_type());
308-
registration
309-
}
310-
}
307+
impl<T: FromReflect> GetTypeRegistration for $ty {
308+
fn get_type_registration() -> TypeRegistration {
309+
let mut registration = TypeRegistration::of::<Vec<T>>();
310+
registration.insert::<ReflectFromPtr>(FromType::<Vec<T>>::from_type());
311+
registration
312+
}
313+
}
311314

312-
impl<T: FromReflect> FromReflect for Vec<T> {
313-
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
314-
if let ReflectRef::List(ref_list) = reflect.reflect_ref() {
315-
let mut new_list = Self::with_capacity(ref_list.len());
316-
for field in ref_list.iter() {
317-
new_list.push(T::from_reflect(field)?);
315+
impl<T: FromReflect> FromReflect for $ty {
316+
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
317+
if let ReflectRef::List(ref_list) = reflect.reflect_ref() {
318+
let mut new_list = Self::with_capacity(ref_list.len());
319+
for field in ref_list.iter() {
320+
$push(&mut new_list, T::from_reflect(field)?);
321+
}
322+
Some(new_list)
323+
} else {
324+
None
325+
}
318326
}
319-
Some(new_list)
320-
} else {
321-
None
322327
}
323-
}
328+
};
324329
}
325330

331+
impl_reflect_for_veclike!(Vec<T>, Vec::push, Vec::pop, [T]);
332+
impl_reflect_for_veclike!(
333+
VecDeque<T>,
334+
VecDeque::push_back,
335+
VecDeque::pop_back,
336+
VecDeque::<T>
337+
);
338+
326339
impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
327340
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
328341
key.downcast_ref::<K>()

0 commit comments

Comments
 (0)