Skip to content

Commit 7656022

Browse files
committed
Use entity references everywhere
1 parent 260d9da commit 7656022

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

crates/bevy_ecs/src/reflect.rs

+24-30
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ pub struct ReflectComponent(ReflectComponentFns);
4242
#[derive(Clone)]
4343
pub struct ReflectComponentFns {
4444
/// Function pointer implementing [`ReflectComponent::insert()`].
45-
pub insert: fn(&mut World, Entity, &dyn Reflect),
45+
pub insert: fn(&mut EntityMut, &dyn Reflect),
4646
/// Function pointer implementing [`ReflectComponent::apply()`].
4747
pub apply: fn(&mut EntityMut, &dyn Reflect),
4848
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
49-
pub apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
49+
pub apply_or_insert: fn(&mut EntityMut, &dyn Reflect),
5050
/// Function pointer implementing [`ReflectComponent::remove()`].
5151
pub remove: fn(&mut EntityMut),
5252
/// Function pointer implementing [`ReflectComponent::contains()`].
@@ -56,7 +56,7 @@ pub struct ReflectComponentFns {
5656
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
5757
pub reflect_mut: for<'a> fn(&'a mut EntityMut<'a>) -> Option<Mut<'a, dyn Reflect>>,
5858
/// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`].
59-
pub reflect_unchecked_mut: unsafe fn(&World, Entity) -> Option<Mut<dyn Reflect>>,
59+
pub reflect_unchecked_mut: unsafe fn(EntityRef) -> Option<Mut<dyn Reflect>>,
6060
/// Function pointer implementing [`ReflectComponent::copy()`].
6161
pub copy: fn(&World, &mut World, Entity, Entity),
6262
}
@@ -74,12 +74,8 @@ impl ReflectComponentFns {
7474

7575
impl ReflectComponent {
7676
/// Insert a reflected [`Component`] into the entity like [`insert()`](crate::world::EntityMut::insert).
77-
///
78-
/// # Panics
79-
///
80-
/// Panics if there is no such entity.
81-
pub fn insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
82-
(self.0.insert)(world, entity, component);
77+
pub fn insert(&self, entity: &mut EntityMut, component: &dyn Reflect) {
78+
(self.0.insert)(entity, component);
8379
}
8480

8581
/// Uses reflection to set the value of this [`Component`] type in the entity to the given value.
@@ -92,12 +88,8 @@ impl ReflectComponent {
9288
}
9389

9490
/// 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.
95-
///
96-
/// # Panics
97-
///
98-
/// Panics if the `entity` does not exist.
99-
pub fn apply_or_insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
100-
(self.0.apply_or_insert)(world, entity, component);
91+
pub fn apply_or_insert(&self, entity: &mut EntityMut, component: &dyn Reflect) {
92+
(self.0.apply_or_insert)(entity, component);
10193
}
10294

10395
/// Removes this [`Component`] type from the entity. Does nothing if it doesn't exist.
@@ -132,10 +124,9 @@ impl ReflectComponent {
132124
/// * Don't call this method more than once in the same scope for a given [`Component`].
133125
pub unsafe fn reflect_unchecked_mut<'a>(
134126
&self,
135-
world: &'a World,
136-
entity: Entity,
127+
entity: EntityRef<'a>,
137128
) -> Option<Mut<'a, dyn Reflect>> {
138-
(self.0.reflect_unchecked_mut)(world, entity)
129+
(self.0.reflect_unchecked_mut)(entity)
139130
}
140131

141132
/// 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`.
@@ -177,22 +168,22 @@ impl ReflectComponent {
177168
impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
178169
fn from_type() -> Self {
179170
ReflectComponent(ReflectComponentFns {
180-
insert: |world, entity, reflected_component| {
181-
let mut component = C::from_world(world);
171+
insert: |entity, reflected_component| {
172+
let mut component = entity.world_scope(|world| C::from_world(world));
182173
component.apply(reflected_component);
183-
world.entity_mut(entity).insert(component);
174+
entity.insert(component);
184175
},
185176
apply: |entity, reflected_component| {
186177
let mut component = entity.get_mut::<C>().unwrap();
187178
component.apply(reflected_component);
188179
},
189-
apply_or_insert: |world, entity, reflected_component| {
190-
if let Some(mut component) = world.get_mut::<C>(entity) {
180+
apply_or_insert: |entity, reflected_component| {
181+
if let Some(mut component) = entity.get_mut::<C>() {
191182
component.apply(reflected_component);
192183
} else {
193-
let mut component = C::from_world(world);
184+
let mut component = entity.world_scope(|world| C::from_world(world));
194185
component.apply(reflected_component);
195-
world.entity_mut(entity).insert(component);
186+
entity.insert(component);
196187
}
197188
},
198189
remove: |entity| {
@@ -214,13 +205,16 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
214205
ticks: c.ticks,
215206
})
216207
},
217-
reflect_unchecked_mut: |world, entity| {
208+
reflect_unchecked_mut: |entity| {
218209
// SAFETY: reflect_mut is an unsafe function pointer used by `reflect_unchecked_mut` which promises to never
219-
// produce aliasing mutable references, and reflect_mut, which has mutable world access
210+
// produce aliasing mutable references, and reflect_mut, which has mutable world access4
211+
let world = entity.world();
212+
let last_change_tick = world.last_change_tick();
213+
let read_change_tick = world.read_change_tick();
214+
220215
unsafe {
221-
world
222-
.get_entity(entity)?
223-
.get_unchecked_mut::<C>(world.last_change_tick(), world.read_change_tick())
216+
entity
217+
.get_unchecked_mut::<C>(last_change_tick, read_change_tick)
224218
.map(|c| Mut {
225219
value: c.value as &mut dyn Reflect,
226220
ticks: c.ticks,

crates/bevy_ecs/src/world/entity_ref.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,10 @@ impl<'w> EntityMut<'w> {
639639
}
640640

641641
/// Gives mutable access to this `EntityMut`'s [`World`] in a temporary scope.
642-
pub fn world_scope(&mut self, f: impl FnOnce(&mut World)) {
643-
f(self.world);
642+
pub fn world_scope<T>(&mut self, f: impl FnOnce(&mut World) -> T) -> T {
643+
let result = f(self.world);
644644
self.update_location();
645+
result
645646
}
646647

647648
/// Updates the internal entity location to match the current location in the internal

0 commit comments

Comments
 (0)