Trait StreamExt

Source
pub trait StreamExt: Stream
where Self: 'static,
{ // Provided methods fn stop_after<F>(self, f: F) -> StopAfter<Self, F> where F: FnMut(&Self::Item) -> bool, Self: Sized { ... } fn use_callbacks<C>(self, callback: C) -> impl Future<Output = ()> where Self: Sized + Send + Sync, C: Callback<Item = Self::Item> + 'static { ... } }
Expand description

An extension trait for Stream that provides a variety of convenient combinator functions.

Provided Methods§

Source

fn stop_after<F>(self, f: F) -> StopAfter<Self, F>
where F: FnMut(&Self::Item) -> bool, Self: Sized,

Takes elements from this stream until the provided predicate resolves to true.

This function operates similarly to Iterator::take_while, extracting elements from the stream until the predicate f evaluates to false. Unlike Iterator::take_while, this function also returns the last evaluated element for which the predicate was true, marking the stream as done afterwards. Once an element causes the predicate to return false, the stream will consistently return that it is finished.

§Examples

Basic usage:

use tokio_stream::{self as stream, StreamExt as _};
use azure_speech::StreamExt;

#[tokio::main]
async fn main() {
     
let mut stream = stream::iter(1..=5).stop_after(|&x| x >= 3);

    assert_eq!(Some(1), stream.next().await);
    assert_eq!(Some(2), stream.next().await);
    assert_eq!(Some(3), stream.next().await);
    // Since 4 > 3, the stream is now considered done
    assert_eq!(None, stream.next().await);
}

This function is particularly useful when you need to process elements of a stream up to a certain point, and then stop processing, including the element that caused the stop condition.

Source

fn use_callbacks<C>(self, callback: C) -> impl Future<Output = ()>
where Self: Sized + Send + Sync, C: Callback<Item = Self::Item> + 'static,

Calls the provided callback for each item in the stream.

Implementors§

Source§

impl<St> StreamExt for St
where St: Stream + ?Sized + 'static,