Add Events<T>::retain
implementation through existing drain with send_batch
#19486
+10
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Solution
I added
Events<T>::retain(predicate: Fn(&Event) -> bool)
tobevy_ecs/src/event/collections.rs
. It uses the existingEvents::drain
along withIterator::partition
to filter the events into two vectors with the passed predicate. The ‘kept’ events are resent back into the queue withEvents::send_batch()
as it is more efficient than using send individually.A more optimized solution would modify the queue in place without breaking the cursor position of any instanced
EventReaders
but I am unsure if that is possible without simply replacing the 'filtered' events with a 'None' or similar. Any input or criticism would be appreciated!Testing
I did some testing to validate the logic would work, but might need some help with whether I should include examples or tests for this.