Skip to content

Commit 81c83dd

Browse files
committed
ReflectComponentFns
1 parent bfafa78 commit 81c83dd

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::{unsafe_world_cell::UnsafeWorldCell, FromWorld, World},
8+
world::{unsafe_world_cell::UnsafeWorldCell, 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
///
5658
/// # Safety
@@ -85,9 +87,9 @@ impl ReflectComponent {
8587
///
8688
/// # Panics
8789
///
88-
/// Panics if there is no [`Component`] of the given type or the `entity` does not exist.
89-
pub fn apply(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
90-
(self.0.apply)(world, entity, component);
90+
/// Panics if there is no [`Component`] of the given type.
91+
pub fn apply(&self, entity: EntityMut, component: &dyn Reflect) {
92+
(self.0.apply)(entity, component);
9193
}
9294

9395
/// 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.
@@ -103,14 +105,19 @@ impl ReflectComponent {
103105
///
104106
/// # Panics
105107
///
106-
/// Panics if there is no [`Component`] of the given type or the `entity` does not exist.
107-
pub fn remove(&self, world: &mut World, entity: Entity) {
108-
(self.0.remove)(world, entity);
108+
/// Panics if there is no [`Component`] of the given type.
109+
pub fn remove(&self, entity: EntityMut) {
110+
(self.0.remove)(entity);
111+
}
112+
113+
/// Returns whether entity contains this [`Component`]
114+
pub fn contains(&self, entity: EntityRef) -> bool {
115+
(self.0.contains)(entity)
109116
}
110117

111118
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
112-
pub fn reflect<'a>(&self, world: &'a World, entity: Entity) -> Option<&'a dyn Reflect> {
113-
(self.0.reflect)(world, entity)
119+
pub fn reflect<'a>(&self, entity: EntityRef<'a>) -> Option<&'a dyn Reflect> {
120+
(self.0.reflect)(entity)
114121
}
115122

116123
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
@@ -181,8 +188,8 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
181188
component.apply(reflected_component);
182189
world.entity_mut(entity).insert(component);
183190
},
184-
apply: |world, entity, reflected_component| {
185-
let mut component = world.get_mut::<C>(entity).unwrap();
191+
apply: |mut entity, reflected_component| {
192+
let mut component = entity.get_mut::<C>().unwrap();
186193
component.apply(reflected_component);
187194
},
188195
apply_or_insert: |world, entity, reflected_component| {
@@ -194,9 +201,10 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
194201
world.entity_mut(entity).insert(component);
195202
}
196203
},
197-
remove: |world, entity| {
198-
world.entity_mut(entity).remove::<C>();
204+
remove: |mut entity| {
205+
entity.remove::<C>();
199206
},
207+
contains: |entity| entity.contains::<C>(),
200208
copy: |source_world, destination_world, source_entity, destination_entity| {
201209
let source_component = source_world.get::<C>(source_entity).unwrap();
202210
let mut destination_component = C::from_world(destination_world);
@@ -205,12 +213,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
205213
.entity_mut(destination_entity)
206214
.insert(destination_component);
207215
},
208-
reflect: |world, entity| {
209-
world
210-
.get_entity(entity)?
211-
.get::<C>()
212-
.map(|c| c as &dyn Reflect)
213-
},
216+
reflect: |entity| entity.get::<C>().map(|c| c as &dyn Reflect),
214217
reflect_mut: |world, entity| {
215218
// SAFETY: reflect_mut is an unsafe function pointer used by
216219
// 1. `reflect_unchecked_mut` which must be called with an UnsafeWorldCell with access to the the component `C` on the `entity`, and

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)