Skip to content

Commit 0f9fc12

Browse files
committed
ReflectComponentFns
1 parent 82b0e71 commit 0f9fc12

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

crates/bevy_ecs/src/reflect.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
component::Component,
66
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
77
system::Resource,
8-
world::{FromWorld, World},
8+
world::{EntityMut, EntityRef, FromWorld, World},
99
};
1010
use bevy_reflect::{
1111
impl_from_reflect_value, impl_reflect_value, FromType, Reflect, ReflectDeserialize,
@@ -44,13 +44,15 @@ pub struct ReflectComponentFns {
4444
/// Function pointer implementing [`ReflectComponent::insert()`].
4545
pub insert: fn(&mut World, Entity, &dyn Reflect),
4646
/// Function pointer implementing [`ReflectComponent::apply()`].
47-
pub apply: fn(&mut World, Entity, &dyn Reflect),
47+
pub apply: fn(EntityMut, &dyn Reflect),
4848
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
4949
pub apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
5050
/// Function pointer implementing [`ReflectComponent::remove()`].
51-
pub remove: fn(&mut World, Entity),
51+
pub remove: fn(EntityMut),
52+
/// Function pointer implementing [`ReflectComponent::contains()`].
53+
pub contains: fn(EntityRef) -> bool,
5254
/// Function pointer implementing [`ReflectComponent::reflect()`].
53-
pub reflect: fn(&World, Entity) -> Option<&dyn Reflect>,
55+
pub reflect: fn(EntityRef) -> Option<&dyn Reflect>,
5456
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
5557
pub reflect_mut: unsafe fn(&World, Entity) -> Option<Mut<dyn Reflect>>,
5658
/// Function pointer implementing [`ReflectComponent::copy()`].
@@ -82,9 +84,9 @@ impl ReflectComponent {
8284
///
8385
/// # Panics
8486
///
85-
/// Panics if there is no [`Component`] of the given type or the `entity` does not exist.
86-
pub fn apply(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
87-
(self.0.apply)(world, entity, component);
87+
/// Panics if there is no [`Component`] of the given type.
88+
pub fn apply(&self, entity: EntityMut, component: &dyn Reflect) {
89+
(self.0.apply)(entity, component);
8890
}
8991

9092
/// Uses reflection to set the value of this [`Component`] type in the entity to the given value or insert a new one if it does not exist.
@@ -100,14 +102,19 @@ impl ReflectComponent {
100102
///
101103
/// # Panics
102104
///
103-
/// Panics if there is no [`Component`] of the given type or the `entity` does not exist.
104-
pub fn remove(&self, world: &mut World, entity: Entity) {
105-
(self.0.remove)(world, entity);
105+
/// Panics if there is no [`Component`] of the given type.
106+
pub fn remove(&self, entity: EntityMut) {
107+
(self.0.remove)(entity);
108+
}
109+
110+
/// Returns whether entity contains this [`Component`]
111+
pub fn contains(&self, entity: EntityRef) -> bool {
112+
(self.0.contains)(entity)
106113
}
107114

108115
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
109-
pub fn reflect<'a>(&self, world: &'a World, entity: Entity) -> Option<&'a dyn Reflect> {
110-
(self.0.reflect)(world, entity)
116+
pub fn reflect<'a>(&self, entity: EntityRef<'a>) -> Option<&'a dyn Reflect> {
117+
(self.0.reflect)(entity)
111118
}
112119

113120
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
@@ -178,8 +185,8 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
178185
component.apply(reflected_component);
179186
world.entity_mut(entity).insert(component);
180187
},
181-
apply: |world, entity, reflected_component| {
182-
let mut component = world.get_mut::<C>(entity).unwrap();
188+
apply: |mut entity, reflected_component| {
189+
let mut component = entity.get_mut::<C>().unwrap();
183190
component.apply(reflected_component);
184191
},
185192
apply_or_insert: |world, entity, reflected_component| {
@@ -191,9 +198,10 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
191198
world.entity_mut(entity).insert(component);
192199
}
193200
},
194-
remove: |world, entity| {
195-
world.entity_mut(entity).remove::<C>();
201+
remove: |mut entity| {
202+
entity.remove::<C>();
196203
},
204+
contains: |entity| entity.contains::<C>(),
197205
copy: |source_world, destination_world, source_entity, destination_entity| {
198206
let source_component = source_world.get::<C>(source_entity).unwrap();
199207
let mut destination_component = C::from_world(destination_world);
@@ -202,12 +210,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
202210
.entity_mut(destination_entity)
203211
.insert(destination_component);
204212
},
205-
reflect: |world, entity| {
206-
world
207-
.get_entity(entity)?
208-
.get::<C>()
209-
.map(|c| c as &dyn Reflect)
210-
},
213+
reflect: |entity| entity.get::<C>().map(|c| c as &dyn Reflect),
211214
reflect_mut: |world, entity| {
212215
// SAFETY: reflect_mut is an unsafe function pointer used by `reflect_unchecked_mut` which promises to never
213216
// produce aliasing mutable references, and reflect_mut, which has mutable world access

crates/bevy_scene/src/dynamic_scene_builder.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,18 @@ impl<'w> DynamicSceneBuilder<'w> {
124124
components: Vec::new(),
125125
};
126126

127-
for component_id in self.original_world.entity(entity).archetype().components() {
127+
let entity = self.original_world.entity(entity);
128+
for component_id in entity.archetype().components() {
128129
let reflect_component = self
129130
.original_world
130131
.components()
131132
.get_info(component_id)
132133
.and_then(|info| type_registry.get(info.type_id().unwrap()))
133-
.and_then(|registration| registration.data::<ReflectComponent>());
134+
.and_then(|registration| registration.data::<ReflectComponent>())
135+
.and_then(|reflect_component| reflect_component.reflect(entity));
134136

135137
if let Some(reflect_component) = reflect_component {
136-
if let Some(component) = reflect_component.reflect(self.original_world, entity)
137-
{
138-
entry.components.push(component.clone_value());
139-
}
138+
entry.components.push(reflect_component.clone_value());
140139
}
141140
}
142141
self.extracted_scene.insert(index, entry);

0 commit comments

Comments
 (0)