From 8dac5558a878b38b16557968d401fcccfc26dff2 Mon Sep 17 00:00:00 2001 From: GMcMichael Date: Tue, 3 Jun 2025 23:11:40 -0700 Subject: [PATCH] Retain implementation through existing drain with send_batch --- crates/bevy_ecs/src/event/collections.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/bevy_ecs/src/event/collections.rs b/crates/bevy_ecs/src/event/collections.rs index 66447b7de4..d07166a288 100644 --- a/crates/bevy_ecs/src/event/collections.rs +++ b/crates/bevy_ecs/src/event/collections.rs @@ -250,6 +250,16 @@ impl Events { .map(|i| i.event) } + /// Drains all events, resending any that pass the specified predicate back into the event buffer. + pub fn retain bool>(&mut self, predicate: T) -> impl Iterator + '_ { + // Utilize the existing drain, then partition the resulting iterator with the passed predicate + let (kept, drained): (Vec, Vec) = 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