Skip to content
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

tokio_stream::StreamMap extend() - do not allow duplicate keys #5586

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 27 additions & 3 deletions tokio-stream/src/stream_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,36 @@ where
}
}

impl<K, V> Extend<(K, V)> for StreamMap<K, V> {
fn extend<T>(&mut self, iter: T)
impl<K, V> StreamMap<K, V> {
/// 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 - <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'
///
/// # Examples
///
/// ```
/// // Issue #4774 - https://github.com/tokio-rs/tokio/issues/4774
/// let mut map = tokio_stream::StreamMap::new();
/// map.insert("key", tokio_stream::pending::<u8>());
/// map.extend(vec![ ("key", tokio_stream::pending::<u8>()) ]);
///
/// assert_eq!(map.len(), 1);
/// ```
pub fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = (K, V)>,
K: Eq + Hash,
{
self.entries.extend(iter);
for (key, value) in iter.into_iter() {
self.insert(key, value);
}
}
}

Expand Down