[−][src]Trait smol::stream::StreamExt
Extension trait for Stream
.
Provided methods
fn next(&mut self) -> NextFuture<Self>ⓘImportant traits for NextFuture<'_, T>
impl<'_, T> Future for NextFuture<'_, T> where
T: Unpin + Stream + ?Sized, type Output = Option<<T as Stream>::Item>;
where
Self: Unpin,
Important traits for NextFuture<'_, T>
impl<'_, T> Future for NextFuture<'_, T> where
T: Unpin + Stream + ?Sized, type Output = Option<<T as Stream>::Item>;
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 collect<C>(self) -> CollectFuture<Self, C>ⓘImportant traits for CollectFuture<St, C>
impl<St, C> Future for CollectFuture<St, C> where
C: Default + Extend<<St as Stream>::Item>,
St: Stream, type Output = C;
where
C: Default + Extend<Self::Item>,
Important traits for CollectFuture<St, C>
impl<St, C> Future for CollectFuture<St, C> where
C: Default + Extend<<St as Stream>::Item>,
St: Stream, type Output = C;
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>ⓘImportant traits for TryCollectFuture<St, C>
impl<T, E, St, C> Future for TryCollectFuture<St, C> where
C: Default + Extend<T>,
St: Stream<Item = Result<T, E>>, type Output = Result<C, E>;
where
C: Default + Extend<T>,
Self::Item: Result,
<Self::Item as Result>::Ok == T,
Important traits for TryCollectFuture<St, C>
impl<T, E, St, C> Future for TryCollectFuture<St, C> where
C: Default + Extend<T>,
St: Stream<Item = Result<T, E>>, type Output = Result<C, E>;
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<B, F>(self, init: B, f: F) -> FoldFuture<Self, F, B>ⓘImportant traits for FoldFuture<S, F, B>
impl<S, F, B> Future for FoldFuture<S, F, B> where
F: FnMut(B, <S as Stream>::Item) -> B,
S: Stream, type Output = B;
where
F: FnMut(B, Self::Item) -> B,
Important traits for FoldFuture<S, F, B>
impl<S, F, B> Future for FoldFuture<S, F, B> where
F: FnMut(B, <S as Stream>::Item) -> B,
S: Stream, type Output = B;
F: FnMut(B, Self::Item) -> B,
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
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,
Important 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>;
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>>ⓘ where
Self: Send + 'static,
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>>ⓘ where
Self: 'static,
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()];