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:
parent
5ed8e0639a
commit
cdcb773e9b
@ -2811,6 +2811,22 @@ impl<'w> EntityWorldMut<'w> {
|
|||||||
.entity_get_spawned_or_despawned_by(self.entity)
|
.entity_get_spawned_or_despawned_by(self.entity)
|
||||||
.map(|location| location.unwrap())
|
.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.
|
/// A view into a single entity and component in a world, which may either be vacant or occupied.
|
||||||
|
Loading…
Reference in New Issue
Block a user