Skip to content

Commit e621fcc

Browse files
committed
Fix List::push by requiring Default
1 parent a4e5e27 commit e621fcc

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

crates/bevy_ecs/src/entity/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ impl fmt::Debug for Entity {
7777
}
7878
}
7979

80+
impl Default for Entity {
81+
fn default() -> Self {
82+
Self::new(u32::MAX)
83+
}
84+
}
85+
8086
impl SparseSetIndex for Entity {
8187
fn sparse_set_index(&self) -> usize {
8288
self.id() as usize

crates/bevy_reflect/src/impls/smallvec.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{serde::Serializable, List, ListIter, Reflect, ReflectMut, ReflectRef
55

66
impl<T: Array + Send + Sync + 'static> List for SmallVec<T>
77
where
8-
T::Item: Reflect + Clone,
8+
T::Item: Reflect + Clone + Default,
99
{
1010
fn get(&self, index: usize) -> Option<&dyn Reflect> {
1111
if index < SmallVec::len(self) {
@@ -29,10 +29,10 @@ where
2929

3030
fn push(&mut self, value: Box<dyn Reflect>) {
3131
let value = value.take::<T::Item>().unwrap_or_else(|value| {
32-
panic!(
33-
"Attempted to push invalid value of type {}.",
34-
value.type_name()
35-
)
32+
// If `value` is not of the same type, it may be a dynamic type that needs to be applied
33+
let mut new_value = T::Item::default();
34+
(&mut new_value as &mut dyn Reflect).apply(value.as_ref());
35+
new_value
3636
});
3737
SmallVec::push(self, value);
3838
}
@@ -48,7 +48,7 @@ where
4848
// SAFE: any and any_mut both return self
4949
unsafe impl<T: Array + Send + Sync + 'static> Reflect for SmallVec<T>
5050
where
51-
T::Item: Reflect + Clone,
51+
T::Item: Reflect + Clone + Default,
5252
{
5353
fn type_name(&self) -> &str {
5454
std::any::type_name::<Self>()

crates/bevy_reflect/src/impls/std.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl_reflect_value!(HashSet<T: Serialize + Hash + Eq + Clone + for<'de> Deserial
3535
impl_reflect_value!(Range<T: Serialize + Clone + for<'de> Deserialize<'de> + Send + Sync + 'static>(Serialize, Deserialize));
3636
impl_reflect_value!(Duration);
3737

38-
impl<T: Reflect> List for Vec<T> {
38+
impl<T: Reflect + Default> List for Vec<T> {
3939
fn get(&self, index: usize) -> Option<&dyn Reflect> {
4040
<[T]>::get(self, index).map(|value| value as &dyn Reflect)
4141
}
@@ -57,17 +57,17 @@ impl<T: Reflect> List for Vec<T> {
5757

5858
fn push(&mut self, value: Box<dyn Reflect>) {
5959
let value = value.take::<T>().unwrap_or_else(|value| {
60-
panic!(
61-
"Attempted to push invalid value of type {}.",
62-
value.type_name()
63-
)
60+
// If `value` is not of the same type, it may be a dynamic type that needs to be applied
61+
let mut new_value = T::default();
62+
(&mut new_value as &mut dyn Reflect).apply(value.as_ref());
63+
new_value
6464
});
6565
Vec::push(self, value);
6666
}
6767
}
6868

6969
// SAFE: any and any_mut both return self
70-
unsafe impl<T: Reflect> Reflect for Vec<T> {
70+
unsafe impl<T: Reflect + Default> Reflect for Vec<T> {
7171
fn type_name(&self) -> &str {
7272
std::any::type_name::<Self>()
7373
}
@@ -114,7 +114,7 @@ unsafe impl<T: Reflect> Reflect for Vec<T> {
114114
}
115115
}
116116

117-
impl<T: Reflect + for<'de> Deserialize<'de>> GetTypeRegistration for Vec<T> {
117+
impl<T: Reflect + Default + for<'de> Deserialize<'de>> GetTypeRegistration for Vec<T> {
118118
fn get_type_registration() -> TypeRegistration {
119119
let mut registration = TypeRegistration::of::<Vec<T>>();
120120
registration.insert::<ReflectDeserialize>(FromType::<Vec<T>>::from_type());

crates/bevy_reflect/src/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ mod tests {
326326
bar: C,
327327
}
328328

329-
#[derive(Reflect)]
329+
#[derive(Reflect, Default)]
330330
struct C {
331331
baz: f32,
332332
}

crates/bevy_transform/src/components/parent.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@ use bevy_ecs::{
66
use bevy_reflect::Reflect;
77
use std::ops::{Deref, DerefMut};
88

9-
#[derive(Debug, Copy, Clone, Eq, PartialEq, Reflect)]
9+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Reflect, Default)]
1010
#[reflect(Component, MapEntities, PartialEq)]
1111
pub struct Parent(pub Entity);
1212

13-
// TODO: We need to impl either FromWorld or Default so Parent can be registered as Properties.
14-
// This is because Properties deserialize by creating an instance and apply a patch on top.
15-
// However Parent should only ever be set with a real user-defined entity. Its worth looking into
16-
// better ways to handle cases like this.
17-
impl FromWorld for Parent {
18-
fn from_world(_world: &mut World) -> Self {
19-
Parent(Entity::new(u32::MAX))
20-
}
21-
}
22-
2313
impl MapEntities for Parent {
2414
fn map_entities(&mut self, entity_map: &EntityMap) -> Result<(), MapEntitiesError> {
2515
self.0 = entity_map.get(self.0)?;

0 commit comments

Comments
 (0)