Skip to content

Commit 201a3f9

Browse files
committed
Add EntityMut::world_scope
1 parent 4453650 commit 201a3f9

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

crates/bevy_ecs/src/world/entity_ref.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl<'w> EntityMut<'w> {
526526

527527
/// Returns this `EntityMut`'s world.
528528
///
529-
/// See [`EntityMut::into_world_mut`] for a safe alternative.
529+
/// See [`EntityMut::world_scope`] or [`EntityMut::into_world_mut`] for a safe alternative.
530530
///
531531
/// # Safety
532532
/// Caller must not modify the world in a way that changes the current entity's location
@@ -543,6 +543,12 @@ impl<'w> EntityMut<'w> {
543543
self.world
544544
}
545545

546+
/// Gives mutable access to this `EntityMut`'s [`World`] in a temporary scope.
547+
pub fn world_scope(&mut self, f: impl FnOnce(&mut World)) {
548+
f(self.world);
549+
self.update_location();
550+
}
551+
546552
/// Updates the internal entity location to match the current location in the internal
547553
/// [`World`]. This is only needed if the user called [`EntityMut::world`], which enables the
548554
/// location to change.

crates/bevy_hierarchy/src/child_builder.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -456,29 +456,23 @@ pub trait BuildWorldChildren {
456456

457457
impl<'w> BuildWorldChildren for EntityMut<'w> {
458458
fn with_children(&mut self, spawn_children: impl FnOnce(&mut WorldChildBuilder)) -> &mut Self {
459-
{
460-
let entity = self.id();
459+
let entity = self.id();
460+
self.world_scope(|world| {
461461
let mut builder = WorldChildBuilder {
462462
current_entity: None,
463463
parent_entities: vec![entity],
464-
// SAFETY: The EntityLocation is updated before any other methods are called on self.
465-
world: unsafe { self.world_mut() },
464+
world,
466465
};
467-
468466
spawn_children(&mut builder);
469-
}
470-
self.update_location();
467+
});
471468
self
472469
}
473470

474471
fn push_children(&mut self, children: &[Entity]) -> &mut Self {
475472
let parent = self.id();
476-
{
477-
// SAFETY: The EntityLocation is updated before any other methods are called on self.
478-
let world = unsafe { self.world_mut() };
473+
self.world_scope(|world| {
479474
update_old_parents(world, parent, children);
480-
self.update_location();
481-
}
475+
});
482476
if let Some(mut children_component) = self.get_mut::<Children>() {
483477
children_component
484478
.0
@@ -492,13 +486,9 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {
492486

493487
fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {
494488
let parent = self.id();
495-
{
496-
// SAFETY: The EntityLocation is updated before any other methods are called on self.
497-
let world = unsafe { self.world_mut() };
489+
self.world_scope(|world| {
498490
update_old_parents(world, parent, children);
499-
self.update_location();
500-
}
501-
491+
});
502492
if let Some(mut children_component) = self.get_mut::<Children>() {
503493
children_component
504494
.0
@@ -512,10 +502,9 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {
512502

513503
fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
514504
let parent = self.id();
515-
// SAFETY: The EntityLocation is updated before any other methods are called on self.
516-
let world = unsafe { self.world_mut() };
517-
remove_children(parent, children, world);
518-
self.update_location();
505+
self.world_scope(|world| {
506+
remove_children(parent, children, world);
507+
});
519508
self
520509
}
521510
}

0 commit comments

Comments
 (0)