From c8439d03270f4009a82914b69a59cb505df222cc Mon Sep 17 00:00:00 2001 From: Alexander Indenbaum Date: Wed, 29 Mar 2023 15:13:31 +0300 Subject: [PATCH] A naive polynomial time complexity implementation that might be unworkable. However, this implementation does not allow duplicate keys and would overwrite existing ones similar to from_iter() behaviour. Initially introduced: PR #4272 - https://github.com/tokio-rs/tokio/pull/4272 Note: can not implement Extend trait, since this method has stricter requirements than trait, as Extend does not require Key to be 'Eq + Hash' Issue #4774 - https://github.com/tokio-rs/tokio/issues/4774 --- tokio-stream/src/stream_map.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/tokio-stream/src/stream_map.rs b/tokio-stream/src/stream_map.rs index ddd14d586d6..2e741b546f1 100644 --- a/tokio-stream/src/stream_map.rs +++ b/tokio-stream/src/stream_map.rs @@ -585,12 +585,36 @@ where } } -impl Extend<(K, V)> for StreamMap { - fn extend(&mut self, iter: T) +impl StreamMap { + /// Extends the StreamMap with an iterable container like vector. + /// + /// A naive polynomial time complexity implementation that might be unworkable. + /// However, this implementation does not allow duplicate keys and would overwrite + /// existing ones similar to from_iter() behaviour. + /// + /// Initially introduced: PR #4272 - + /// + /// Note: can not implement Extend trait, since this method has stricter + /// requirements than trait, as Extend does not require Key to be 'Eq + Hash' + /// + /// # Examples + /// + /// ``` + /// // Issue #4774 - https://github.com/tokio-rs/tokio/issues/4774 + /// let mut map = tokio_stream::StreamMap::new(); + /// map.insert("key", tokio_stream::pending::()); + /// map.extend(vec![ ("key", tokio_stream::pending::()) ]); + /// + /// assert_eq!(map.len(), 1); + /// ``` + pub fn extend(&mut self, iter: T) where T: IntoIterator, + K: Eq + Hash, { - self.entries.extend(iter); + for (key, value) in iter.into_iter() { + self.insert(key, value); + } } }