From 3b339024f0f7213f0b0cf59b119b7e1bc99de2e9 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 23 Nov 2021 11:54:06 +0100 Subject: [PATCH] stream: impl Extend for StreamMap (#4272) ## Motivation This allows `StreamMap` to be used with [`futures::stream::StreamExt::collect`][collect]. My use case is something like this: ```rust let stream_map: StreamMap<_, _> = things .into_iter() .map(|thing| make_stream(thing)) // iterator of futures .collect::>() // stream of streams .collect::>() // combine all the inner streams into one .await; async fn make_stream(thing: Thing) -> impl Stream { ... } ``` [collect]: https://docs.rs/futures/0.3.17/futures/stream/trait.StreamExt.html#method.collect ## Solution Add `Extend` impl that delegates to the inner `Vec`. --- tokio-stream/src/stream_map.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tokio-stream/src/stream_map.rs b/tokio-stream/src/stream_map.rs index 80a521ee1..215980474 100644 --- a/tokio-stream/src/stream_map.rs +++ b/tokio-stream/src/stream_map.rs @@ -585,6 +585,15 @@ where } } +impl Extend<(K, V)> for StreamMap { + fn extend(&mut self, iter: T) + where + T: IntoIterator, + { + self.entries.extend(iter); + } +} + mod rand { use std::cell::Cell;