Skip to content

Commit c207950

Browse files
Add despawn_recursive to EntityMut (#2855)
1 parent 10cbe05 commit c207950

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

crates/bevy_transform/src/hierarchy/hierarchy.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::components::{Children, Parent};
22
use bevy_ecs::{
33
entity::Entity,
44
system::{Command, EntityCommands},
5-
world::World,
5+
world::{EntityMut, World},
66
};
77
use bevy_utils::tracing::debug;
88

@@ -44,17 +44,29 @@ impl Command for DespawnRecursive {
4444

4545
pub trait DespawnRecursiveExt {
4646
/// Despawns the provided entity and its children.
47-
fn despawn_recursive(&mut self);
47+
fn despawn_recursive(self);
4848
}
4949

5050
impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> {
5151
/// Despawns the provided entity and its children.
52-
fn despawn_recursive(&mut self) {
52+
fn despawn_recursive(mut self) {
5353
let entity = self.id();
5454
self.commands().add(DespawnRecursive { entity });
5555
}
5656
}
5757

58+
impl<'w> DespawnRecursiveExt for EntityMut<'w> {
59+
/// Despawns the provided entity and its children.
60+
fn despawn_recursive(mut self) {
61+
let entity = self.id();
62+
// SAFE: EntityMut is consumed so even though the location is no longer
63+
// valid, it cannot be accessed again with the invalid location.
64+
unsafe {
65+
despawn_with_children_recursive(self.world_mut(), entity);
66+
}
67+
}
68+
}
69+
5870
#[cfg(test)]
5971
mod tests {
6072
use bevy_ecs::{

0 commit comments

Comments
 (0)