Skip to main content

StreamMap

Struct StreamMap 

Source
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>

Source

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());
Source

pub fn with_capacity(capacity: usize) -> Self

Empty map that can hold capacity entries without reallocating.

Source

pub fn iter(&self) -> impl Iterator<Item = &(K, V)>

Iterate over (key, stream) pairs.

Source

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.

Source

pub fn keys(&self) -> impl Iterator<Item = &K>

All keys.

Source

pub fn values(&self) -> impl Iterator<Item = &V>

All streams.

Source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>

All streams, mutably.

Source

pub fn capacity(&self) -> usize

Room for more entries without reallocating. Lower bound.

Source

pub fn len(&self) -> usize

Count of streams in the map.

Source

pub fn is_empty(&self) -> bool

True when no streams are stored.

Source

pub fn clear(&mut self)

Drop all streams. Keeps allocated memory.

Source

pub fn insert(&mut self, k: K, stream: V) -> Option<V>
where K: Hash + Eq + Clone,

Insert a stream. Returns the old stream when the key existed.

Replace keeps the entry position, so polling order is stable.

Source

pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q> + Hash + Eq + Clone, Q: Hash + Eq + ?Sized,

Remove by key. Returns the stream when present.

Source

pub fn contains_key<Q>(&self, k: &Q) -> bool
where K: Borrow<Q> + Hash + Eq, Q: Hash + Eq + ?Sized,

True when the key has a stream.

Source

pub fn get<Q>(&self, k: &Q) -> Option<&V>
where K: Borrow<Q> + Hash + Eq, Q: Hash + Eq + ?Sized,

Reference to the stream for k, if present.

Source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q> + Hash + Eq, Q: Hash + Eq + ?Sized,

Mutable reference to the stream for k, if present.

Source§

impl<K, V> StreamMap<K, V>
where K: Clone + Unpin, V: Stream + Unpin,

Source

pub async fn next_many( &mut self, buffer: &mut Vec<(K, V::Item)>, limit: usize, ) -> usize
where K: Hash + Eq,

Pull up to limit items into buffer. Returns count appended.

Returns 0 only when the map is empty or limit is 0. Cancel safe: dropping the future loses nothing.

Source

pub fn poll_next_many( &mut self, cx: &mut Context<'_>, buffer: &mut Vec<(K, V::Item)>, limit: usize, ) -> Poll<usize>
where K: Hash + Eq,

Poll version of StreamMap::next_many.

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for StreamMap<K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for StreamMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for StreamMap<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V> Extend<(K, V)> for StreamMap<K, V>
where K: Hash + Eq + Clone,

Source§

fn extend<T>(&mut self, iter: T)
where T: IntoIterator<Item = (K, V)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V> FromIterator<(K, V)> for StreamMap<K, V>
where K: Hash + Eq + Clone,

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<K, V> Stream for StreamMap<K, V>
where K: Clone + Unpin + Hash + Eq, V: Stream + Unpin,

Source§

type Item = (K, <V as Stream>::Item)

Values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for StreamMap<K, V>

§

impl<K, V> RefUnwindSafe for StreamMap<K, V>

§

impl<K, V> Send for StreamMap<K, V>
where Vec<(K, V)>: Send, HashMap<K, usize>: Send,

§

impl<K, V> Sync for StreamMap<K, V>
where Vec<(K, V)>: Sync, HashMap<K, usize>: Sync,

§

impl<K, V> Unpin for StreamMap<K, V>

§

impl<K, V> UnsafeUnpin for StreamMap<K, V>

§

impl<K, V> UnwindSafe for StreamMap<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.