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

Add iter and iter_mut methods to StreamMap #2890

Merged
merged 1 commit into from Sep 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 49 additions & 3 deletions tokio/src/stream/stream_map.rs
Expand Up @@ -163,6 +163,52 @@ pub struct StreamMap<K, V> {
}

impl<K, V> StreamMap<K, V> {
/// An iterator visiting all key-value pairs in arbitrary order.
///
/// The iterator element type is &'a (K, V).
///
/// # Examples
///
/// ```
/// use tokio::stream::{StreamMap, pending};
///
/// let mut map = StreamMap::new();
///
/// map.insert("a", pending::<i32>());
/// map.insert("b", pending());
/// map.insert("c", pending());
///
/// for (key, stream) in map.iter() {
/// println!("({}, {:?})", key, stream);
/// }
/// ```
pub fn iter(&self) -> impl Iterator<Item = &(K, V)> {
self.entries.iter()
}

/// An iterator visiting all key-value pairs mutably in arbitrary order.
///
/// The iterator element type is &'a mut (K, V).
///
/// # Examples
///
/// ```
/// use tokio::stream::{StreamMap, pending};
///
/// let mut map = StreamMap::new();
///
/// map.insert("a", pending::<i32>());
/// map.insert("b", pending());
/// map.insert("c", pending());
///
/// for (key, stream) in map.iter_mut() {
/// println!("({}, {:?})", key, stream);
/// }
/// ```
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (K, V)> {
self.entries.iter_mut()
}

/// Creates an empty `StreamMap`.
///
/// The stream map is initially created with a capacity of `0`, so it will
Expand Down Expand Up @@ -217,7 +263,7 @@ impl<K, V> StreamMap<K, V> {
/// }
/// ```
pub fn keys(&self) -> impl Iterator<Item = &K> {
self.entries.iter().map(|(k, _)| k)
self.iter().map(|(k, _)| k)
}

/// An iterator visiting all values in arbitrary order.
Expand All @@ -240,7 +286,7 @@ impl<K, V> StreamMap<K, V> {
/// }
/// ```
pub fn values(&self) -> impl Iterator<Item = &V> {
self.entries.iter().map(|(_, v)| v)
self.iter().map(|(_, v)| v)
}

/// An iterator visiting all values mutably in arbitrary order.
Expand All @@ -263,7 +309,7 @@ impl<K, V> StreamMap<K, V> {
/// }
/// ```
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
self.entries.iter_mut().map(|(_, v)| v)
self.iter_mut().map(|(_, v)| v)
}

/// Returns the number of streams the map can hold without reallocating.
Expand Down