Skip to content

Commit f586377

Browse files
committed
Use entity references everywhere
Fix Nit
1 parent 1648e06 commit f586377

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

crates/bevy_ecs/src/reflect.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ pub struct ReflectComponent(ReflectComponentFns);
4545
#[derive(Clone)]
4646
pub struct ReflectComponentFns {
4747
/// Function pointer implementing [`ReflectComponent::insert()`].
48-
pub insert: fn(&mut World, Entity, &dyn Reflect),
48+
pub insert: fn(&mut EntityMut, &dyn Reflect),
4949
/// Function pointer implementing [`ReflectComponent::apply()`].
5050
pub apply: fn(&mut EntityMut, &dyn Reflect),
5151
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
52-
pub apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
52+
pub apply_or_insert: fn(&mut EntityMut, &dyn Reflect),
5353
/// Function pointer implementing [`ReflectComponent::remove()`].
5454
pub remove: fn(&mut EntityMut),
5555
/// Function pointer implementing [`ReflectComponent::contains()`].
5656
pub contains: fn(EntityRef) -> bool,
5757
/// Function pointer implementing [`ReflectComponent::reflect()`].
5858
pub reflect: fn(EntityRef) -> Option<&dyn Reflect>,
5959
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
60-
pub reflect_mut: for<'a> fn(&'a mut EntityMut) -> Option<Mut<'a, dyn Reflect>>,
60+
pub reflect_mut: for<'a> fn(&'a mut EntityMut<'_>) -> Option<Mut<'a, dyn Reflect>>,
6161
/// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`].
6262
///
6363
/// # Safety
@@ -81,12 +81,8 @@ impl ReflectComponentFns {
8181

8282
impl ReflectComponent {
8383
/// Insert a reflected [`Component`] into the entity like [`insert()`](crate::world::EntityMut::insert).
84-
///
85-
/// # Panics
86-
///
87-
/// Panics if there is no such entity.
88-
pub fn insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
89-
(self.0.insert)(world, entity, component);
84+
pub fn insert(&self, entity: &mut EntityMut, component: &dyn Reflect) {
85+
(self.0.insert)(entity, component);
9086
}
9187

9288
/// Uses reflection to set the value of this [`Component`] type in the entity to the given value.
@@ -99,12 +95,8 @@ impl ReflectComponent {
9995
}
10096

10197
/// 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.
102-
///
103-
/// # Panics
104-
///
105-
/// Panics if the `entity` does not exist.
106-
pub fn apply_or_insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
107-
(self.0.apply_or_insert)(world, entity, component);
98+
pub fn apply_or_insert(&self, entity: &mut EntityMut, component: &dyn Reflect) {
99+
(self.0.apply_or_insert)(entity, component);
108100
}
109101

110102
/// Removes this [`Component`] type from the entity. Does nothing if it doesn't exist.
@@ -184,22 +176,22 @@ impl ReflectComponent {
184176
impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
185177
fn from_type() -> Self {
186178
ReflectComponent(ReflectComponentFns {
187-
insert: |world, entity, reflected_component| {
188-
let mut component = C::from_world(world);
179+
insert: |entity, reflected_component| {
180+
let mut component = entity.world_scope(|world| C::from_world(world));
189181
component.apply(reflected_component);
190-
world.entity_mut(entity).insert(component);
182+
entity.insert(component);
191183
},
192184
apply: |entity, reflected_component| {
193185
let mut component = entity.get_mut::<C>().unwrap();
194186
component.apply(reflected_component);
195187
},
196-
apply_or_insert: |world, entity, reflected_component| {
197-
if let Some(mut component) = world.get_mut::<C>(entity) {
188+
apply_or_insert: |entity, reflected_component| {
189+
if let Some(mut component) = entity.get_mut::<C>() {
198190
component.apply(reflected_component);
199191
} else {
200-
let mut component = C::from_world(world);
192+
let mut component = entity.world_scope(|world| C::from_world(world));
201193
component.apply(reflected_component);
202-
world.entity_mut(entity).insert(component);
194+
entity.insert(component);
203195
}
204196
},
205197
remove: |entity| {

crates/bevy_scene/src/dynamic_scene.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl DynamicScene {
7171
let entity = *entity_map
7272
.entry(bevy_ecs::entity::Entity::from_raw(scene_entity.entity))
7373
.or_insert_with(|| world.spawn_empty().id());
74+
let entity_mut = &mut world.entity_mut(entity);
7475

7576
// Apply/ add each component to the given entity.
7677
for component in &scene_entity.components {
@@ -89,7 +90,7 @@ impl DynamicScene {
8990
// If the entity already has the given component attached,
9091
// just apply the (possibly) new value, otherwise add the
9192
// component to the entity.
92-
reflect_component.apply_or_insert(world, entity, &**component);
93+
reflect_component.apply_or_insert(entity_mut, &**component);
9394
}
9495
}
9596

0 commit comments

Comments
 (0)