Skip to content

Commit

Permalink
stream: add iter and iter_mut methods to StreamMap (#2890)
Browse files Browse the repository at this point in the history
  • Loading branch information
mankinskin committed Sep 29, 2020
1 parent c6fc35a commit 3403be5
Showing 1 changed file with 49 additions and 3 deletions.
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

0 comments on commit 3403be5

Please sign in to comment.