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.
This commit is contained in:
Nicola Papale 2022-08-08 22:59:18 +00:00
parent a9634c7344
commit 397b6df023

View file

@ -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.