Skip to content

Commit 0792a9a

Browse files
committed
EntityMut reflect_mut
1 parent 0f9fc12 commit 0792a9a

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

crates/bevy_ecs/src/reflect.rs

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ 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(EntityMut, &dyn Reflect),
47+
pub apply: fn(&mut 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(EntityMut),
51+
pub remove: fn(&mut EntityMut),
5252
/// Function pointer implementing [`ReflectComponent::contains()`].
53-
pub contains: fn(EntityRef) -> bool,
53+
pub contains: fn(&EntityRef) -> bool,
5454
/// Function pointer implementing [`ReflectComponent::reflect()`].
55-
pub reflect: fn(EntityRef) -> Option<&dyn Reflect>,
55+
pub reflect: for<'a> fn(&'a EntityRef) -> Option<&'a dyn Reflect>,
5656
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
57-
pub reflect_mut: unsafe fn(&World, Entity) -> Option<Mut<dyn Reflect>>,
57+
pub reflect_mut: for<'a> fn(&'a mut EntityMut) -> Option<Mut<'a, dyn Reflect>>,
5858
/// Function pointer implementing [`ReflectComponent::copy()`].
5959
pub copy: fn(&World, &mut World, Entity, Entity),
6060
}
@@ -85,7 +85,7 @@ impl ReflectComponent {
8585
/// # Panics
8686
///
8787
/// Panics if there is no [`Component`] of the given type.
88-
pub fn apply(&self, entity: EntityMut, component: &dyn Reflect) {
88+
pub fn apply(&self, entity: &mut EntityMut, component: &dyn Reflect) {
8989
(self.0.apply)(entity, component);
9090
}
9191

@@ -103,42 +103,23 @@ impl ReflectComponent {
103103
/// # Panics
104104
///
105105
/// Panics if there is no [`Component`] of the given type.
106-
pub fn remove(&self, entity: EntityMut) {
106+
pub fn remove(&self, entity: &mut EntityMut) {
107107
(self.0.remove)(entity);
108108
}
109109

110110
/// Returns whether entity contains this [`Component`]
111-
pub fn contains(&self, entity: EntityRef) -> bool {
111+
pub fn contains(&self, entity: &EntityRef) -> bool {
112112
(self.0.contains)(entity)
113113
}
114114

115115
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
116-
pub fn reflect<'a>(&self, entity: EntityRef<'a>) -> Option<&'a dyn Reflect> {
116+
pub fn reflect<'a>(&self, entity: &'a EntityRef<'a>) -> Option<&'a dyn Reflect> {
117117
(self.0.reflect)(entity)
118118
}
119119

120120
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
121-
pub fn reflect_mut<'a>(
122-
&self,
123-
world: &'a mut World,
124-
entity: Entity,
125-
) -> Option<Mut<'a, dyn Reflect>> {
126-
// SAFETY: unique world access
127-
unsafe { (self.0.reflect_mut)(world, entity) }
128-
}
129-
130-
/// # Safety
131-
/// This method does not prevent you from having two mutable pointers to the same data,
132-
/// violating Rust's aliasing rules. To avoid this:
133-
/// * Only call this method in an exclusive system to avoid sharing across threads (or use a
134-
/// scheduler that enforces safe memory access).
135-
/// * Don't call this method more than once in the same scope for a given [`Component`].
136-
pub unsafe fn reflect_unchecked_mut<'a>(
137-
&self,
138-
world: &'a World,
139-
entity: Entity,
140-
) -> Option<Mut<'a, dyn Reflect>> {
141-
(self.0.reflect_mut)(world, entity)
121+
pub fn reflect_mut<'a>(&self, entity: &'a mut EntityMut<'a>) -> Option<Mut<'a, dyn Reflect>> {
122+
(self.0.reflect_mut)(entity)
142123
}
143124

144125
/// 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`.
@@ -185,7 +166,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
185166
component.apply(reflected_component);
186167
world.entity_mut(entity).insert(component);
187168
},
188-
apply: |mut entity, reflected_component| {
169+
apply: |entity, reflected_component| {
189170
let mut component = entity.get_mut::<C>().unwrap();
190171
component.apply(reflected_component);
191172
},
@@ -198,7 +179,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
198179
world.entity_mut(entity).insert(component);
199180
}
200181
},
201-
remove: |mut entity| {
182+
remove: |entity| {
202183
entity.remove::<C>();
203184
},
204185
contains: |entity| entity.contains::<C>(),
@@ -211,18 +192,11 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
211192
.insert(destination_component);
212193
},
213194
reflect: |entity| entity.get::<C>().map(|c| c as &dyn Reflect),
214-
reflect_mut: |world, entity| {
215-
// SAFETY: reflect_mut is an unsafe function pointer used by `reflect_unchecked_mut` which promises to never
216-
// produce aliasing mutable references, and reflect_mut, which has mutable world access
217-
unsafe {
218-
world
219-
.get_entity(entity)?
220-
.get_unchecked_mut::<C>(world.last_change_tick(), world.read_change_tick())
221-
.map(|c| Mut {
222-
value: c.value as &mut dyn Reflect,
223-
ticks: c.ticks,
224-
})
225-
}
195+
reflect_mut: |entity| {
196+
entity.get_mut::<C>().map(|c| Mut {
197+
value: c.value as &mut dyn Reflect,
198+
ticks: c.ticks,
199+
})
226200
},
227201
})
228202
}

0 commit comments

Comments
 (0)