Add EntityWorldMut::reborrow_scope() (#18730)

# Objective

Allow `EntityCommand` implementors to delegate to other entity commands
easily:

```rs
impl EntityCommand for Foo {
    fn apply(self, mut entity: EntityWorldMut) {
        entity.reborrow_scope(|e| StepOne.apply(e));
        entity.reborrow_scope(|e| StepTwo.apply(e));
    }
}
```
This commit is contained in:
Jonathan Chan Kwan Yin 2025-05-06 08:19:56 +08:00 committed by GitHub
parent 5ed8e0639a
commit cdcb773e9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2811,6 +2811,22 @@ impl<'w> EntityWorldMut<'w> {
.entity_get_spawned_or_despawned_by(self.entity)
.map(|location| location.unwrap())
}
/// Reborrows this entity in a temporary scope.
/// This is useful for executing a function that requires a `EntityWorldMut`
/// but you do not want to move out the entity ownership.
pub fn reborrow_scope<U>(&mut self, f: impl FnOnce(EntityWorldMut) -> U) -> U {
let Self {
entity, location, ..
} = *self;
self.world_scope(move |world| {
f(EntityWorldMut {
world,
entity,
location,
})
})
}
}
/// A view into a single entity and component in a world, which may either be vacant or occupied.