Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add despawn_recursive to EntityMut #2855

Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions crates/bevy_transform/src/hierarchy/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -44,17 +44,27 @@ 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();
unsafe {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a // SAFE: rationale comment above it explaining why this is safe in relationship to the requirements of self.world_mut() (ex: EntityMut is consumed so even though the location is no longer valid, it cannot be accessed again with the invalid location).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just copy that rationale in.

despawn_with_children_recursive(self.world_mut(), entity);
TheRawMeatball marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[cfg(test)]
mod tests {
use bevy_ecs::{
Expand Down