Skip to content

Commit 820feea

Browse files
committed
docs, match resource_scope param ordering
1 parent 52b5b27 commit 820feea

File tree

1 file changed

+49
-3
lines changed
  • crates/bevy_ecs/src/world

1 file changed

+49
-3
lines changed

crates/bevy_ecs/src/world/mod.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,10 +1474,41 @@ impl World {
14741474
self.entity_mut(disabled.entity)
14751475
}
14761476

1477+
/// Temporarily disables the requested entity from this [`World`], runs
1478+
/// custom user code with a mutable reference to the entity's component of
1479+
/// type `C`, then re-enables the entity before returning.
1480+
///
1481+
/// This enables safe simultaneous mutable access to both a component of an
1482+
/// entity and the rest of the [`World`].
1483+
///
1484+
/// # Panics
1485+
///
1486+
/// Panics if the resource does not exist.
1487+
/// Use [`try_component_scope`](Self::try_component_scope) instead if you want to handle this case.
1488+
/// Panics if the world is replaced during the execution of the closure.
1489+
///
1490+
/// # Example
1491+
/// ```
1492+
/// use bevy_ecs::prelude::*;
1493+
/// #[derive(Component)]
1494+
/// struct A(u32);
1495+
/// #[derive(Component)]
1496+
/// struct B(u32);
1497+
/// let mut world = World::new();
1498+
/// let scoped_entity = world.spawn(A(1)).id();
1499+
/// let entity = world.spawn(B(1)).id();
1500+
///
1501+
/// world.component_scope(scoped_entity, |world, _, a: &mut A| {
1502+
/// assert!(world.get_mut::<A>(scoped_entity).is_none());
1503+
/// let b = world.get_mut::<B>(entity).unwrap();
1504+
/// a.0 += b.0;
1505+
/// });
1506+
/// assert_eq!(world.get::<A>(scoped_entity).unwrap().0, 2);
1507+
/// ```
14771508
pub fn component_scope<
14781509
C: Component<Mutability = Mutable>,
14791510
R,
1480-
F: FnOnce(Entity, &mut C, &mut World) -> R,
1511+
F: FnOnce(&mut World, Entity, &mut C) -> R,
14811512
>(
14821513
&mut self,
14831514
entity: Entity,
@@ -1491,17 +1522,26 @@ impl World {
14911522
});
14921523
}
14931524

1525+
/// Temporarily disables the requested entity from this [`World`], runs
1526+
/// custom user code with a mutable reference to the entity's component of
1527+
/// type `C`, then re-enables the entity before returning.
1528+
///
1529+
/// This enables safe simultaneous mutable access to both a component of an
1530+
/// entity and the rest of the [`World`].
1531+
///
1532+
/// See also [`component_scope`](Self::component_scope).
14941533
pub fn try_component_scope<
14951534
C: Component<Mutability = Mutable>,
14961535
R,
1497-
F: FnOnce(Entity, &mut C, &mut World) -> R,
1536+
F: FnOnce(&mut World, Entity, &mut C) -> R,
14981537
>(
14991538
&mut self,
15001539
entity: Entity,
15011540
f: F,
15021541
) -> Option<R> {
15031542
self.flush();
15041543

1544+
let world_id = self.id();
15051545
let mut entity_mut = self.get_entity_mut(entity).ok()?;
15061546
let mut component = {
15071547
let component = entity_mut.get_mut::<C>()?;
@@ -1511,7 +1551,13 @@ impl World {
15111551

15121552
let disabled = entity_mut.disable();
15131553

1514-
let out = f(entity, &mut component, self);
1554+
let out = f(self, entity, &mut component);
1555+
1556+
assert_eq!(
1557+
self.id(),
1558+
world_id,
1559+
"World was replaced during component scope"
1560+
);
15151561

15161562
let mut entity_mut = self.enable(disabled);
15171563

0 commit comments

Comments
 (0)