Skip to main content

Tools

Trait Tools 

Source
pub trait Tools<Item>
where Self: Stream<Item = Item> + Send + Sized, <Self as Stream>::Item: Send,
{ // Required methods fn counts(self) -> impl Future<Output = HashMap<Item, usize>> + Send where <Self as Stream>::Item: Eq + Hash; fn counts_by<K, F>( self, f: F, ) -> impl Future<Output = HashMap<K, usize>> + Send where F: Fn(Item) -> K + Send, K: Eq + Hash + Send; fn counts_by_with_cap<const CAP: usize, K, F>( self, f: F, ) -> impl Future<Output = HashMap<K, usize>> + Send where F: Fn(Item) -> K + Send, K: Eq + Hash + Send; fn counts_with_cap<const CAP: usize>( self, ) -> impl Future<Output = HashMap<Item, usize>> + Send where <Self as Stream>::Item: Eq + Hash; fn sample_by<const N: usize, K, F>( self, f: F, ) -> impl Future<Output = ArrayVec<K, N>> + Send where F: Fn(Item) -> K + Send, K: Send; fn fold_default<T, F, Fut>(self, f: F) -> impl Future<Output = T> + Send where F: Fn(T, Item) -> Fut + Send, Fut: Future<Output = T> + Send, T: Default + Send; }
Expand description

Adds aggregation, sampling, and folding operations to streams.

Counting adapters consume the source into hash maps, while folding avoids an intermediate collection. Reservoir sampling retains a uniform subset of up to a fixed size in one pass.

Required Methods§

Source

fn counts(self) -> impl Future<Output = HashMap<Item, usize>> + Send
where <Self as Stream>::Item: Eq + Hash,

Counts occurrences of each distinct stream item.

The entire stream is consumed into a hash map that starts at zero capacity and grows as needed. Equal items share one incrementing counter.

§Panics

Panics if an item’s occurrence count overflows usize.

Source

fn counts_by<K, F>(self, f: F) -> impl Future<Output = HashMap<K, usize>> + Send
where F: Fn(Item) -> K + Send, K: Eq + Hash + Send,

Counts occurrences of keys derived from stream items.

f is applied once to every item before counting. The result map starts at zero capacity and grows as needed.

§Panics

Panics if a derived key’s occurrence count overflows usize.

Source

fn counts_by_with_cap<const CAP: usize, K, F>( self, f: F, ) -> impl Future<Output = HashMap<K, usize>> + Send
where F: Fn(Item) -> K + Send, K: Eq + Hash + Send,

Counts derived keys into a map with initial capacity CAP.

f is applied once to every stream item. The map initially reserves space for at least CAP distinct keys and grows as needed.

§Panics

Panics if a derived key’s occurrence count overflows usize.

Source

fn counts_with_cap<const CAP: usize>( self, ) -> impl Future<Output = HashMap<Item, usize>> + Send
where <Self as Stream>::Item: Eq + Hash,

Counts items into a map with initial capacity CAP.

Equal items share one counter as the entire stream is consumed. The map initially reserves space for at least CAP distinct items and grows as needed.

§Panics

Panics if an item’s occurrence count overflows usize.

Source

fn sample_by<const N: usize, K, F>( self, f: F, ) -> impl Future<Output = ArrayVec<K, N>> + Send
where F: Fn(Item) -> K + Send, K: Send,

Reservoir-samples up to N items uniformly without replacement.

The stream is consumed in one pass, and f is applied only when an item enters the reservoir, including entries later replaced. Rejected items do not invoke it, and derived keys may repeat.

§Panics

Panics if the number of observed stream items overflows usize.

Source

fn fold_default<T, F, Fut>(self, f: F) -> impl Future<Output = T> + Send
where F: Fn(T, Item) -> Fut + Send, Fut: Future<Output = T> + Send, T: Default + Send,

Folds the stream from T::default() with an asynchronous accumulator.

Each item is processed in source order after the previous fold future resolves. The final accumulator is returned when the stream ends.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<Item, S> Tools<Item> for S
where S: Stream<Item = Item> + Send + Sized, <Self as Stream>::Item: Send,