[][src]Trait futures_lite::stream::StreamExt

pub trait StreamExt: Stream {
    fn next(&mut self) -> NextFuture<Self>

Important traits for NextFuture<'_, S>

impl<S: Stream + Unpin + ?Sized, '_> Future for NextFuture<'_, S> type Output = Option<S::Item>;

    where
        Self: Unpin
, { ... }
fn count(self) -> CountFuture<Self>

Important traits for CountFuture<S>

impl<S: Stream + ?Sized> Future for CountFuture<S> type Output = usize;

    where
        Self: Sized
, { ... }
fn map<T, F>(self, f: F) -> Map<Self, F>
    where
        Self: Sized,
        F: FnMut(Self::Item) -> T
, { ... }
fn filter<P>(self, predicate: P) -> Filter<Self, P>
    where
        Self: Sized,
        P: FnMut(&Self::Item) -> bool
, { ... }
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
    where
        Self: Sized,
        F: FnMut(Self::Item) -> Option<T>
, { ... }
fn collect<C: Default + Extend<Self::Item>>(self) -> CollectFuture<Self, C>

Important traits for CollectFuture<S, C>

impl<S, C> Future for CollectFuture<S, C> where
    S: Stream,
    C: Default + Extend<S::Item>, 
type Output = C;

    where
        Self: Sized
, { ... }
fn try_collect<T, C: Default + Extend<T>>(self) -> TryCollectFuture<Self, C>

Important traits for TryCollectFuture<S, C>

impl<T, E, S, C> Future for TryCollectFuture<S, C> where
    S: Stream<Item = Result<T, E>>,
    C: Default + Extend<T>, 
type Output = Result<C, E>;

    where
        Self: Sized,
        Self::Item: Result<Ok = T>
, { ... }
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>

Important traits for FoldFuture<S, F, T>

impl<S, F, T> Future for FoldFuture<S, F, T> where
    S: Stream + Sized,
    F: FnMut(T, S::Item) -> T, 
type Output = T;

    where
        Self: Sized,
        F: FnMut(T, Self::Item) -> T
, { ... }
fn try_fold<T, E, F, B>(
        &mut self,
        init: B,
        f: F
    ) -> TryFoldFuture<Self, F, B>

Important traits for TryFoldFuture<'a, S, F, B>

impl<'a, T, E, S, F, B> Future for TryFoldFuture<'a, S, F, B> where
    S: Stream + Unpin,
    S::Item: Result<Ok = T, Err = E>,
    F: FnMut(B, T) -> Result<B, E>, 
type Output = Result<B, E>;

    where
        Self: Unpin + Sized,
        Self::Item: Result<Ok = T, Err = E>,
        F: FnMut(B, T) -> Result<B, E>
, { ... }
fn boxed(self) -> Boxed<Self::Item>
    where
        Self: Sized + Send + 'static
, { ... }
fn boxed_local(self) -> BoxedLocal<Self::Item>
    where
        Self: Sized + 'static
, { ... } }

Extension trait for Stream.

Provided methods

fn next(&mut self) -> NextFuture<Self>

Important traits for NextFuture<'_, S>

impl<S: Stream + Unpin + ?Sized, '_> Future for NextFuture<'_, S> type Output = Option<S::Item>;
where
    Self: Unpin

Retrieves the next item in the stream.

Returns None when iteration is finished. Stream implementations may choose to or not to resume iteration after that.

Examples

use futures_lite::*;

let mut s = stream::iter(1..=3);

assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, Some(3));
assert_eq!(s.next().await, None);

fn count(self) -> CountFuture<Self>

Important traits for CountFuture<S>

impl<S: Stream + ?Sized> Future for CountFuture<S> type Output = usize;
where
    Self: Sized

Counts the number of elements in the stream.

Examples

use futures_lite::*;

let s1 = stream::iter(vec![0]);
let s2 = stream::iter(vec![1, 2, 3]);

assert_eq!(s1.count().await, 1);
assert_eq!(s2.count().await, 3);

fn map<T, F>(self, f: F) -> Map<Self, F> where
    Self: Sized,
    F: FnMut(Self::Item) -> T, 

Maps items of the stream to new values using a closure.

Examples

use futures_lite::*;

let s = stream::iter(vec![1, 2, 3]);
let mut s = s.map(|x| 2 * x);

assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, Some(4));
assert_eq!(s.next().await, Some(6));
assert_eq!(s.next().await, None);

fn filter<P>(self, predicate: P) -> Filter<Self, P> where
    Self: Sized,
    P: FnMut(&Self::Item) -> bool

Keeps items of the stream for which predicate returns true.

Examples

Basic usage:

use futures_lite::*;

let s = stream::iter(vec![1, 2, 3, 4]);
let mut s = s.filter(|i| i % 2 == 0);

assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, Some(4));
assert_eq!(s.next().await, None);

fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F> where
    Self: Sized,
    F: FnMut(Self::Item) -> Option<T>, 

Filters and maps items of the stream using a closure.

Examples

use futures_lite::*;

let s = stream::iter(vec!["1", "lol", "3", "NaN", "5"]);
let mut s = s.filter_map(|a| a.parse::<u32>().ok());

assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, Some(3));
assert_eq!(s.next().await, Some(5));
assert_eq!(s.next().await, None);

fn collect<C: Default + Extend<Self::Item>>(self) -> CollectFuture<Self, C>

Important traits for CollectFuture<S, C>

impl<S, C> Future for CollectFuture<S, C> where
    S: Stream,
    C: Default + Extend<S::Item>, 
type Output = C;
where
    Self: Sized

Collects all items in the stream into a collection.

Examples

use futures_lite::*;

let mut s = stream::iter(1..=3);

let items: Vec<_> = s.collect().await;
assert_eq!(items, [1, 2, 3]);

fn try_collect<T, C: Default + Extend<T>>(self) -> TryCollectFuture<Self, C>

Important traits for TryCollectFuture<S, C>

impl<T, E, S, C> Future for TryCollectFuture<S, C> where
    S: Stream<Item = Result<T, E>>,
    C: Default + Extend<T>, 
type Output = Result<C, E>;
where
    Self: Sized,
    Self::Item: Result<Ok = T>, 

Collects all items in the fallible stream into a collection.

use futures_lite::*;

let s = stream::iter(vec![Ok(1), Err(2), Ok(3)]);
let res: Result<Vec<i32>, i32> = s.try_collect().await;
assert_eq!(res, Err(2));

let s = stream::iter(vec![Ok(1), Ok(2), Ok(3)]);
let res: Result<Vec<i32>, i32> = s.try_collect().await;
assert_eq!(res, Ok(vec![1, 2, 3]));

fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>

Important traits for FoldFuture<S, F, T>

impl<S, F, T> Future for FoldFuture<S, F, T> where
    S: Stream + Sized,
    F: FnMut(T, S::Item) -> T, 
type Output = T;
where
    Self: Sized,
    F: FnMut(T, Self::Item) -> T, 

Accumulates a computation over the stream.

The computation begins with the accumulator value set to init then applies f to the accumulator and each item in the stream. The final accumulator value is returned.

Examples

use futures_lite::*;

let s = stream::iter(vec![1, 2, 3]);
let sum = s.fold(0, |acc, x| acc + x).await;

assert_eq!(sum, 6);

fn try_fold<T, E, F, B>(&mut self, init: B, f: F) -> TryFoldFuture<Self, F, B>

Important traits for TryFoldFuture<'a, S, F, B>

impl<'a, T, E, S, F, B> Future for TryFoldFuture<'a, S, F, B> where
    S: Stream + Unpin,
    S::Item: Result<Ok = T, Err = E>,
    F: FnMut(B, T) -> Result<B, E>, 
type Output = Result<B, E>;
where
    Self: Unpin + Sized,
    Self::Item: Result<Ok = T, Err = E>,
    F: FnMut(B, T) -> Result<B, E>, 

Accumulates a fallible computation over the stream.

The computation begins with the accumulator value set to init then applies f to the accumulator and each item in the stream. The final accumulator value is returned, or an error if f failed the computation.

Examples

use futures_lite::*;

let mut s = stream::iter(vec![Ok(1), Ok(2), Ok(3)]);

let sum = s.try_fold(0, |acc, v| {
    if (acc + v) % 2 == 1 {
        Ok(acc + v)
    } else {
        Err("fail")
    }
})
.await;

assert_eq!(sum, Err("fail"));

fn boxed(self) -> Boxed<Self::Item> where
    Self: Sized + Send + 'static, 

Boxes the stream and changes its type to dyn Stream<Item = T> + Send.

Examples

use futures_lite::*;

let a = stream::once(1);
let b = stream::empty();

// Streams of different types can be stored in
// the same collection when they are boxed:
let streams = vec![a.boxed(), b.boxed()];

fn boxed_local(self) -> BoxedLocal<Self::Item> where
    Self: Sized + 'static, 

Boxes the stream and changes its type to dyn Stream<Item = T>.

Examples

use futures_lite::*;

let a = stream::once(1);
let b = stream::empty();

// Streams of different types can be stored in
// the same collection when they are boxed:
let streams = vec![a.boxed_local(), b.boxed_local()];
Loading content...

Implementors

impl<S: ?Sized> StreamExt for S where
    S: Stream
[src]

Loading content...