From 22d7d9e6e649844a125b12c286e3055b0ec33324 Mon Sep 17 00:00:00 2001 From: Slixe Date: Tue, 25 Feb 2025 17:36:19 +0100 Subject: [PATCH 1/2] futures ordered: add `clear` fn --- futures-util/src/stream/futures_ordered.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/futures-util/src/stream/futures_ordered.rs b/futures-util/src/stream/futures_ordered.rs index 2cc144e81..8ebec4c60 100644 --- a/futures-util/src/stream/futures_ordered.rs +++ b/futures-util/src/stream/futures_ordered.rs @@ -169,6 +169,14 @@ impl FuturesOrdered { self.next_outgoing_index -= 1; self.in_progress_queue.push(wrapped); } + + /// Clear the whole `FuturesOrdered` queue. + /// + /// This function clears the pending futures and the queued outputs + /// to make it fully empty. + pub fn clear(&mut self) { + *self = Self::new(); + } } impl Default for FuturesOrdered { From e3512fd762ea6b65cacb6f8fa2cb85b12a1b4a84 Mon Sep 17 00:00:00 2001 From: Slixe Date: Tue, 25 Feb 2025 19:51:52 +0100 Subject: [PATCH 2/2] futures-util: clear fn reuse allocations --- futures-util/src/stream/futures_ordered.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/futures-util/src/stream/futures_ordered.rs b/futures-util/src/stream/futures_ordered.rs index 8ebec4c60..460b35267 100644 --- a/futures-util/src/stream/futures_ordered.rs +++ b/futures-util/src/stream/futures_ordered.rs @@ -175,7 +175,10 @@ impl FuturesOrdered { /// This function clears the pending futures and the queued outputs /// to make it fully empty. pub fn clear(&mut self) { - *self = Self::new(); + self.in_progress_queue.clear(); + self.queued_outputs.clear(); + self.next_incoming_index = 0; + self.next_outgoing_index = 0; } }