Sometimes it's useful to allow components to be inserted/removed and entities to be spawned/despawned in the course of iteration. This is impossible using a conventional interface, but could be achieved with a sufficiently clever interface in the style of Vec::retain. For example:
fn modify<Q: Query, T>(
&mut self,
process: impl for<'a> FnMut(<Q::Fetch as Fetch<'a>>::Item) -> T,
apply: impl FnMut(&mut ModifiableWorld, Entity, T),
)
where the two functions are called, one after the other, for each entity matching Q, and ModifiableWorld is a proxy object that adjusts the iteration state to account for entities being added/removed from the archetype currently being iterated.
The implementation is likely to be a source of significant complexity, and it does not enable anything you can't already do by making two passes and storing intermediate data in a Vec. That said, it would be neat.
Sometimes it's useful to allow components to be inserted/removed and entities to be spawned/despawned in the course of iteration. This is impossible using a conventional interface, but could be achieved with a sufficiently clever interface in the style of
Vec::retain. For example:where the two functions are called, one after the other, for each entity matching
Q, andModifiableWorldis a proxy object that adjusts the iteration state to account for entities being added/removed from the archetype currently being iterated.The implementation is likely to be a source of significant complexity, and it does not enable anything you can't already do by making two passes and storing intermediate data in a
Vec. That said, it would be neat.