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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/bevy_ecs/src/event/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ impl<E: Event> Events<E> {
.map(|i| i.event)
}

/// Drains all events, resending any that pass the specified predicate back into the event buffer.
pub fn retain<T: Fn(&E) -> bool>(&mut self, predicate: T) -> impl Iterator<Item = E> + '_ {
// Utilize the existing drain, then partition the resulting iterator with the passed predicate
let (kept, drained): (Vec<E>, Vec<E>) = self.drain().partition(predicate);
// Resend any retained events as a batch
self.send_batch(kept);
// Return the drained events as an iterator matching Events::drain
drained.into_iter()
}

/// Iterates over events that happened since the last "update" call.
/// WARNING: You probably don't want to use this call. In most cases you should use an
/// [`EventReader`]. You should only use this if you know you only need to consume events
Expand Down