From ac9c74bed6c73b2a7f62979648276dd4b2af947c Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Mon, 8 Aug 2022 22:59:18 +0000 Subject: [PATCH] Add into_world_mut to EntityMut (#5586) # Objective Provide a safe API to access an `EntityMut`'s `World`. ## Solution * Add `EntityMut::into_world_mut` for safe access to the entity's world. --- ## Changelog * Add `EntityMut::into_world_mut` for safe access to the entity's world. --- crates/bevy_ecs/src/world/entity_ref.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 7d1051bba1988..2b6314536c6ef 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -495,15 +495,25 @@ impl<'w> EntityMut<'w> { self.world } + /// Returns this `EntityMut`'s world. + /// + /// See [`EntityMut::into_world_mut`] for a safe alternative. + /// /// # Safety /// Caller must not modify the world in a way that changes the current entity's location /// If the caller _does_ do something that could change the location, `self.update_location()` - /// must be called before using any other methods in [`EntityMut`] + /// must be called before using any other methods on this [`EntityMut`]. #[inline] pub unsafe fn world_mut(&mut self) -> &mut World { self.world } + /// Return this `EntityMut`'s [`World`], consuming itself. + #[inline] + pub fn into_world_mut(self) -> &'w mut World { + self.world + } + /// Updates the internal entity location to match the current location in the internal /// [`World`]. This is only needed if the user called [`EntityMut::world`], which enables the /// location to change.