[][src]Trait smol::stream::StreamExt

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

Notable traits for NextFuture<'_, S>

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

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

Notable traits for CountFuture<S>

impl<S> Future for CountFuture<S> where
    S: Stream + ?Sized
type Output = usize;
{ ... }
fn map<T, F>(self, f: F) -> Map<Self, F>
    where
        F: FnMut(Self::Item) -> T
, { ... }
fn filter<P>(self, predicate: P) -> Filter<Self, P>
    where
        P: FnMut(&Self::Item) -> bool
, { ... }
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
    where
        F: FnMut(Self::Item) -> Option<T>
, { ... }
fn collect<C>(self) -> CollectFuture<Self, C>

Notable traits for CollectFuture<S, C>

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

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

Notable traits for TryCollectFuture<S, C>

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

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

Notable traits for FoldFuture<S, F, T>

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

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

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

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

    where
        F: FnMut(B, T) -> Result<B, E>,
        Self: Unpin,
        Self::Item: Result,
        <Self::Item as Result>::Ok == T,
        <Self::Item as Result>::Err == E
, { ... }
fn boxed(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'static + Send>>

Notable traits for Pin<P>

impl<P> Future for Pin<P> where
    P: Unpin + DerefMut,
    <P as Deref>::Target: Future
type Output = <<P as Deref>::Target as Future>::Output;

    where
        Self: Send + 'static
, { ... }
fn boxed_local(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'static>>

Notable traits for Pin<P>

impl<P> Future for Pin<P> where
    P: Unpin + DerefMut,
    <P as Deref>::Target: Future
type Output = <<P as Deref>::Target as Future>::Output;

    where
        Self: 'static
, { ... } }

Extension trait for Stream.

Provided methods

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

Notable traits for NextFuture<'_, S>

impl<'_, S> Future for NextFuture<'_, S> where
    S: Unpin + Stream + ?Sized
type Output = Option<<S as Stream>::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>

Notable traits for CountFuture<S>

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

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
    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
    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
    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>(self) -> CollectFuture<Self, C>

Notable traits for CollectFuture<S, C>

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

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>(self) -> TryCollectFuture<Self, C>

Notable traits for TryCollectFuture<S, C>

impl<T, E, S, C> Future for TryCollectFuture<S, C> where
    C: Default + Extend<T>,
    S: Stream<Item = Result<T, E>>, 
type Output = Result<C, E>;
where
    C: Default + Extend<T>,
    Self::Item: Result,
    <Self::Item as 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>

Notable traits for FoldFuture<S, F, T>

impl<S, F, T> Future for FoldFuture<S, F, T> where
    F: FnMut(T, <S as Stream>::Item) -> T,
    S: Stream
type Output = T;
where
    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>

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

impl<'a, T, E, S, F, B> Future for TryFoldFuture<'a, S, F, B> where
    F: FnMut(B, T) -> Result<B, E>,
    S: Stream + Unpin,
    <S as Stream>::Item: Result,
    <<S as Stream>::Item as Result>::Ok == T,
    <<S as Stream>::Item as Result>::Err == E, 
type Output = Result<B, E>;
where
    F: FnMut(B, T) -> Result<B, E>,
    Self: Unpin,
    Self::Item: Result,
    <Self::Item as Result>::Ok == T,
    <Self::Item as Result>::Err == 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) -> Pin<Box<dyn Stream<Item = Self::Item> + 'static + Send>>

Notable traits for Pin<P>

impl<P> Future for Pin<P> where
    P: Unpin + DerefMut,
    <P as Deref>::Target: Future
type Output = <<P as Deref>::Target as Future>::Output;
where
    Self: 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) -> Pin<Box<dyn Stream<Item = Self::Item> + 'static>>

Notable traits for Pin<P>

impl<P> Future for Pin<P> where
    P: Unpin + DerefMut,
    <P as Deref>::Target: Future
type Output = <<P as Deref>::Target as Future>::Output;
where
    Self: '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> StreamExt for S where
    S: Stream + ?Sized
[src]

Loading content...