Skip to content

Commit 4936e49

Browse files
committed
EntityMut reflect_mut
Nit
1 parent 81c83dd commit 4936e49

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

crates/bevy_ecs/src/reflect.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use crate::{
55
component::Component,
66
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
77
system::Resource,
8-
world::{unsafe_world_cell::UnsafeWorldCell, EntityMut, EntityRef, FromWorld, World},
8+
world::{
9+
unsafe_world_cell::{UnsafeWorldCell, UnsafeWorldCellEntityRef},
10+
EntityMut, EntityRef, FromWorld, World,
11+
},
912
};
1013
use bevy_reflect::{
1114
impl_from_reflect_value, impl_reflect_value, FromType, Reflect, ReflectDeserialize,
@@ -44,20 +47,23 @@ pub struct ReflectComponentFns {
4447
/// Function pointer implementing [`ReflectComponent::insert()`].
4548
pub insert: fn(&mut World, Entity, &dyn Reflect),
4649
/// Function pointer implementing [`ReflectComponent::apply()`].
47-
pub apply: fn(EntityMut, &dyn Reflect),
50+
pub apply: fn(&mut EntityMut, &dyn Reflect),
4851
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
4952
pub apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
5053
/// Function pointer implementing [`ReflectComponent::remove()`].
51-
pub remove: fn(EntityMut),
54+
pub remove: fn(&mut EntityMut),
5255
/// Function pointer implementing [`ReflectComponent::contains()`].
53-
pub contains: fn(EntityRef) -> bool,
56+
pub contains: fn(&EntityRef) -> bool,
5457
/// Function pointer implementing [`ReflectComponent::reflect()`].
55-
pub reflect: fn(EntityRef) -> Option<&dyn Reflect>,
58+
pub reflect: for<'a> fn(&'a EntityRef) -> Option<&'a dyn Reflect>,
5659
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
60+
pub reflect_mut: for<'a> fn(&'a mut EntityMut) -> Option<Mut<'a, dyn Reflect>>,
61+
/// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`].
5762
///
5863
/// # Safety
59-
/// The function may only be called with an [`UnsafeWorldCell`] that can be used to mutably access the relevant component on the given entity.
60-
pub reflect_mut: unsafe fn(UnsafeWorldCell<'_>, Entity) -> Option<Mut<'_, dyn Reflect>>,
64+
/// The function may only be called with an [`UnsafeWorldCellEntityRef`] that can be used to mutably access the relevant component on the given entity.
65+
pub reflect_unchecked_mut:
66+
unsafe fn(UnsafeWorldCellEntityRef<'_>) -> Option<Mut<'_, dyn Reflect>>,
6167
/// Function pointer implementing [`ReflectComponent::copy()`].
6268
pub copy: fn(&World, &mut World, Entity, Entity),
6369
}
@@ -88,7 +94,7 @@ impl ReflectComponent {
8894
/// # Panics
8995
///
9096
/// Panics if there is no [`Component`] of the given type.
91-
pub fn apply(&self, entity: EntityMut, component: &dyn Reflect) {
97+
pub fn apply(&self, entity: &mut EntityMut, component: &dyn Reflect) {
9298
(self.0.apply)(entity, component);
9399
}
94100

@@ -106,42 +112,37 @@ impl ReflectComponent {
106112
/// # Panics
107113
///
108114
/// Panics if there is no [`Component`] of the given type.
109-
pub fn remove(&self, entity: EntityMut) {
115+
pub fn remove(&self, entity: &mut EntityMut) {
110116
(self.0.remove)(entity);
111117
}
112118

113119
/// Returns whether entity contains this [`Component`]
114-
pub fn contains(&self, entity: EntityRef) -> bool {
120+
pub fn contains(&self, entity: &EntityRef) -> bool {
115121
(self.0.contains)(entity)
116122
}
117123

118124
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
119-
pub fn reflect<'a>(&self, entity: EntityRef<'a>) -> Option<&'a dyn Reflect> {
125+
pub fn reflect<'a>(&self, entity: &'a EntityRef<'a>) -> Option<&'a dyn Reflect> {
120126
(self.0.reflect)(entity)
121127
}
122128

123129
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
124-
pub fn reflect_mut<'a>(
125-
&self,
126-
world: &'a mut World,
127-
entity: Entity,
128-
) -> Option<Mut<'a, dyn Reflect>> {
130+
pub fn reflect_mut<'a>(&self, entity: &'a mut EntityMut<'a>) -> Option<Mut<'a, dyn Reflect>> {
129131
// SAFETY: unique world access
130-
unsafe { (self.0.reflect_mut)(world.as_unsafe_world_cell(), entity) }
132+
(self.0.reflect_mut)(entity)
131133
}
132134

133135
/// # Safety
134136
/// This method does not prevent you from having two mutable pointers to the same data,
135137
/// violating Rust's aliasing rules. To avoid this:
136-
/// * Only call this method with a [`UnsafeWorldCell`] that may be used to mutably access the component on the entity `entity`
138+
/// * Only call this method with a [`UnsafeWorldCellEntityRef`] that may be used to mutably access the component on the entity `entity`
137139
/// * Don't call this method more than once in the same scope for a given [`Component`].
138140
pub unsafe fn reflect_unchecked_mut<'a>(
139141
&self,
140-
world: UnsafeWorldCell<'a>,
141-
entity: Entity,
142+
entity: UnsafeWorldCellEntityRef<'a>,
142143
) -> Option<Mut<'a, dyn Reflect>> {
143144
// SAFETY: safety requirements deferred to caller
144-
(self.0.reflect_mut)(world, entity)
145+
(self.0.reflect_unchecked_mut)(entity)
145146
}
146147

147148
/// Gets the value of this [`Component`] type from entity from `source_world` and [applies](Self::apply()) it to the value of this [`Component`] type in entity in `destination_world`.
@@ -188,7 +189,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
188189
component.apply(reflected_component);
189190
world.entity_mut(entity).insert(component);
190191
},
191-
apply: |mut entity, reflected_component| {
192+
apply: |entity, reflected_component| {
192193
let mut component = entity.get_mut::<C>().unwrap();
193194
component.apply(reflected_component);
194195
},
@@ -201,7 +202,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
201202
world.entity_mut(entity).insert(component);
202203
}
203204
},
204-
remove: |mut entity| {
205+
remove: |entity| {
205206
entity.remove::<C>();
206207
},
207208
contains: |entity| entity.contains::<C>(),
@@ -214,12 +215,17 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
214215
.insert(destination_component);
215216
},
216217
reflect: |entity| entity.get::<C>().map(|c| c as &dyn Reflect),
217-
reflect_mut: |world, entity| {
218-
// SAFETY: reflect_mut is an unsafe function pointer used by
219-
// 1. `reflect_unchecked_mut` which must be called with an UnsafeWorldCell with access to the the component `C` on the `entity`, and
220-
// 2. `reflect_mut`, which has mutable world access
218+
reflect_mut: |entity| {
219+
entity.get_mut::<C>().map(|c| Mut {
220+
value: c.value as &mut dyn Reflect,
221+
ticks: c.ticks,
222+
})
223+
},
224+
reflect_unchecked_mut: |entity| {
225+
// SAFETY: reflect_unchecked_mut is an unsafe function pointer used by
226+
// `reflect_unchecked_mut` which must be called with an UnsafeWorldCellEntityReff with access to the the component `C` on the `entity`
221227
unsafe {
222-
world.get_entity(entity)?.get_mut::<C>().map(|c| Mut {
228+
entity.get_mut::<C>().map(|c| Mut {
223229
value: c.value as &mut dyn Reflect,
224230
ticks: c.ticks,
225231
})

crates/bevy_scene/src/dynamic_scene_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'w> DynamicSceneBuilder<'w> {
132132
.get_info(component_id)
133133
.and_then(|info| type_registry.get(info.type_id().unwrap()))
134134
.and_then(|registration| registration.data::<ReflectComponent>())
135-
.and_then(|reflect_component| reflect_component.reflect(entity));
135+
.and_then(|reflect_component| reflect_component.reflect(&entity));
136136

137137
if let Some(reflect_component) = reflect_component {
138138
entry.components.push(reflect_component.clone_value());

0 commit comments

Comments
 (0)