@@ -2,7 +2,7 @@ use crate::components::{Children, Parent};
2
2
use bevy_ecs:: {
3
3
entity:: Entity ,
4
4
system:: { Command , EntityCommands } ,
5
- world:: World ,
5
+ world:: { EntityMut , World } ,
6
6
} ;
7
7
use bevy_utils:: tracing:: debug;
8
8
@@ -44,17 +44,29 @@ impl Command for DespawnRecursive {
44
44
45
45
pub trait DespawnRecursiveExt {
46
46
/// Despawns the provided entity and its children.
47
- fn despawn_recursive ( & mut self ) ;
47
+ fn despawn_recursive ( self ) ;
48
48
}
49
49
50
50
impl < ' w , ' s , ' a > DespawnRecursiveExt for EntityCommands < ' w , ' s , ' a > {
51
51
/// Despawns the provided entity and its children.
52
- fn despawn_recursive ( & mut self ) {
52
+ fn despawn_recursive ( mut self ) {
53
53
let entity = self . id ( ) ;
54
54
self . commands ( ) . add ( DespawnRecursive { entity } ) ;
55
55
}
56
56
}
57
57
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
+
58
70
#[ cfg( test) ]
59
71
mod tests {
60
72
use bevy_ecs:: {
0 commit comments