From c20795017296168e03401ec8abf535304e43f144 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Tue, 28 Sep 2021 20:35:11 +0000 Subject: [PATCH] Add despawn_recursive to EntityMut (#2855) --- .../bevy_transform/src/hierarchy/hierarchy.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index b8e1bf4532..6701387fe9 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy.rs @@ -2,7 +2,7 @@ use crate::components::{Children, Parent}; use bevy_ecs::{ entity::Entity, system::{Command, EntityCommands}, - world::World, + world::{EntityMut, World}, }; use bevy_utils::tracing::debug; @@ -44,17 +44,29 @@ impl Command for DespawnRecursive { pub trait DespawnRecursiveExt { /// Despawns the provided entity and its children. - fn despawn_recursive(&mut self); + fn despawn_recursive(self); } impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { /// Despawns the provided entity and its children. - fn despawn_recursive(&mut self) { + fn despawn_recursive(mut self) { let entity = self.id(); self.commands().add(DespawnRecursive { entity }); } } +impl<'w> DespawnRecursiveExt for EntityMut<'w> { + /// Despawns the provided entity and its children. + fn despawn_recursive(mut self) { + let entity = self.id(); + // SAFE: EntityMut is consumed so even though the location is no longer + // valid, it cannot be accessed again with the invalid location. + unsafe { + despawn_with_children_recursive(self.world_mut(), entity); + } + } +} + #[cfg(test)] mod tests { use bevy_ecs::{