Skip to content

Add Events<T>::retain implementation through existing drain with send_batch #19486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

GMcMichael
Copy link

Objective

  • Addresses open issue #12204 'Add Events::retain'
  • Sometimes it's useful to not drain all events from Events, but by a specific predicate.
  • There is Vec::retain and it would be convenient to have something like this in Events too.
  • Allow code reuse by unifying it into the repository

Solution

I added Events<T>::retain(predicate: Fn(&Event) -> bool) to bevy_ecs/src/event/collections.rs. It uses the existing Events::drain along with Iterator::partition to filter the events into two vectors with the passed predicate. The ‘kept’ events are resent back into the queue with Events::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.

Copy link
Contributor

github-actions bot commented Jun 4, 2025

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also expose this method on EventMutator? :) Good idea and clean implementation though.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants