pub struct StreamMap<K, V> { /* private fields */ }Expand description
Merge many streams, tagged by key.
Yields (key, item) in arrival order. Insert and remove at any time.
Polling starts at a random entry to reduce fixed-order bias. This does not
guarantee a maximum wait time for any stream.
Unlike tokio_stream::StreamMap, keyed ops are O(1) via a hash index.
Polling still scans entries, same as upstream.
§Example
use keyed_stream_map::StreamMap;
use tokio_stream::{self as stream, StreamExt};
let mut map = StreamMap::new();
map.insert("a", stream::iter(vec![1]));
map.insert("b", stream::iter(vec![2]));
assert_eq!(map.len(), 2);
assert_eq!(map.get(&"a").is_some(), true);
while let Some((k, v)) = map.next().await {
println!("{k}: {v}");
}Implementations§
Source§impl<K, V> StreamMap<K, V>
impl<K, V> StreamMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Empty map. Allocates on first insert.
use keyed_stream_map::StreamMap;
use tokio_stream::Pending;
let map: StreamMap<&str, Pending<()>> = StreamMap::new();
assert!(map.is_empty());Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Empty map that can hold capacity entries without reallocating.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
Iterate over keys and mutable references to their streams.
Keys are immutable because the map keeps a separate index of them.
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
All streams, mutably.
Sourcepub fn insert(&mut self, k: K, stream: V) -> Option<V>
pub fn insert(&mut self, k: K, stream: V) -> Option<V>
Insert a stream. Returns the old stream when the key existed.
Replace keeps the entry position, so polling order is stable.
Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Remove by key. Returns the stream when present.
Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
True when the key has a stream.
Trait Implementations§
Source§impl<K, V> Extend<(K, V)> for StreamMap<K, V>
impl<K, V> Extend<(K, V)> for StreamMap<K, V>
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)