From b5023d4c3707e7b08ba38dd59e09d4a3a13c6fd6 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 27 Jan 2023 12:06:01 -0500 Subject: [PATCH 1/4] return a value from `EntityMut::world_scope` --- crates/bevy_ecs/src/world/entity_ref.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 7eff73711881a..c68eb915632b6 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -540,9 +540,10 @@ impl<'w> EntityMut<'w> { } /// Gives mutable access to this `EntityMut`'s [`World`] in a temporary scope. - pub fn world_scope(&mut self, f: impl FnOnce(&mut World)) { - f(self.world); + pub fn world_scope(&mut self, f: impl FnOnce(&mut World) -> U) -> U { + let val = f(self.world); self.update_location(); + val } /// Updates the internal entity location to match the current location in the internal From 754c18156a5596c8ed7787ce4b86800d2ea76584 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 27 Jan 2023 12:11:54 -0500 Subject: [PATCH 2/4] add an example --- crates/bevy_ecs/src/world/entity_ref.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index c68eb915632b6..a9144f0817197 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -540,6 +540,28 @@ impl<'w> EntityMut<'w> { } /// Gives mutable access to this `EntityMut`'s [`World`] in a temporary scope. + /// This is a safe alternative to using [`Self::world_mut`]. + /// + /// # Examples + /// + /// ``` + /// # use bevy_ecs::prelude::*; + /// #[derive(Resource, Clone, Copy)] + /// struct R(u32); + /// + /// # let mut world = World::new(); + /// # world.init_resource::(); + /// # let mut entity = world.spawn_empty(); + /// // This closure gives us temporary access to the world. + /// let val = entity.world_scope(|world: &mut World| { + /// // Mutate the world while we have access to it. + /// let mut r = world.resource_mut::(); + /// r.0 += 1; + /// + /// // Return a value from the world before giving it back to the `EntityMut`. + /// *r + /// }); + /// ``` pub fn world_scope(&mut self, f: impl FnOnce(&mut World) -> U) -> U { let val = f(self.world); self.update_location(); From 09c7e5d7bcff6fdfabe68293b51319134f989282 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 27 Jan 2023 12:13:02 -0500 Subject: [PATCH 3/4] add a test case to the example --- crates/bevy_ecs/src/world/entity_ref.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index a9144f0817197..38bd3e1e7f02d 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -553,7 +553,7 @@ impl<'w> EntityMut<'w> { /// # world.init_resource::(); /// # let mut entity = world.spawn_empty(); /// // This closure gives us temporary access to the world. - /// let val = entity.world_scope(|world: &mut World| { + /// let new_r = entity.world_scope(|world: &mut World| { /// // Mutate the world while we have access to it. /// let mut r = world.resource_mut::(); /// r.0 += 1; @@ -561,6 +561,7 @@ impl<'w> EntityMut<'w> { /// // Return a value from the world before giving it back to the `EntityMut`. /// *r /// }); + /// # assert_eq!(new_r.0, 1); /// ``` pub fn world_scope(&mut self, f: impl FnOnce(&mut World) -> U) -> U { let val = f(self.world); From 57fee7a02601569150e3d8dc7b50ac517b2991f1 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 27 Jan 2023 12:17:12 -0500 Subject: [PATCH 4/4] derive `Default` --- crates/bevy_ecs/src/world/entity_ref.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 38bd3e1e7f02d..f08bf63dcd1d6 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -546,7 +546,7 @@ impl<'w> EntityMut<'w> { /// /// ``` /// # use bevy_ecs::prelude::*; - /// #[derive(Resource, Clone, Copy)] + /// #[derive(Resource, Default, Clone, Copy)] /// struct R(u32); /// /// # let mut world = World::new();