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