From 0734e6059168e419cd226ed691679e665c7bc887 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Wed, 22 Sep 2021 15:58:42 +0300 Subject: [PATCH 1/4] Add despawn_recursive to EntityMut --- crates/bevy_transform/src/hierarchy/hierarchy.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index b8e1bf45329d1..f68945740f1ec 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; @@ -55,6 +55,17 @@ impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { } } +impl<'w> DespawnRecursiveExt for EntityMut<'w> { + /// Despawns the provided entity and its children. + fn despawn_recursive(&mut self) { + let entity = self.id(); + unsafe { + despawn_with_children_recursive(self.world_mut(), entity); + self.update_location(); + } + } +} + #[cfg(test)] mod tests { use bevy_ecs::{ From a9626a6a376f039840cc12d7417a2da6d9a7ffa9 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Thu, 23 Sep 2021 23:06:22 +0300 Subject: [PATCH 2/4] Update DespawnRecursiveExt to take self --- crates/bevy_transform/src/hierarchy/hierarchy.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index f68945740f1ec..181198164797c 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy.rs @@ -44,12 +44,12 @@ 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(self) { let entity = self.id(); self.commands().add(DespawnRecursive { entity }); } @@ -57,11 +57,10 @@ impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { impl<'w> DespawnRecursiveExt for EntityMut<'w> { /// Despawns the provided entity and its children. - fn despawn_recursive(&mut self) { + fn despawn_recursive(self) { let entity = self.id(); unsafe { despawn_with_children_recursive(self.world_mut(), entity); - self.update_location(); } } } From bab9473f27a8ce6e306f006d07703eaeb4d2b665 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Fri, 24 Sep 2021 12:17:00 +0300 Subject: [PATCH 3/4] minor fix --- crates/bevy_transform/src/hierarchy/hierarchy.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index 181198164797c..91728ccc42f77 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy.rs @@ -49,7 +49,7 @@ pub trait DespawnRecursiveExt { impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { /// Despawns the provided entity and its children. - fn despawn_recursive(self) { + fn despawn_recursive(mut self) { let entity = self.id(); self.commands().add(DespawnRecursive { entity }); } @@ -57,7 +57,7 @@ impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { impl<'w> DespawnRecursiveExt for EntityMut<'w> { /// Despawns the provided entity and its children. - fn despawn_recursive(self) { + fn despawn_recursive(mut self) { let entity = self.id(); unsafe { despawn_with_children_recursive(self.world_mut(), entity); From 7140fb8b2ba898be8cc8abfecb503fe18ff6f6c1 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Tue, 28 Sep 2021 20:14:15 +0300 Subject: [PATCH 4/4] Add safety rationale --- crates/bevy_transform/src/hierarchy/hierarchy.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index 91728ccc42f77..6701387fe953b 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy.rs @@ -59,6 +59,8 @@ 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); }