[−][src]Trait tokio::stream::StreamExt
feature="stream"
only.An extension trait for Stream
s that provides a variety of convenient
combinator functions.
Provided methods
fn next(&mut self) -> Next<Self> where
Self: Unpin,
Self: Unpin,
feature="stream"
only.Creates a future that resolves to the next item in the stream.
Equivalent to:
async fn next(&mut self) -> Option<Self::Item>;
Note that because next
doesn't take ownership over the stream,
the Stream
type must be Unpin
. If you want to use next
with a
!Unpin
stream, you'll first have to pin the stream. This can
be done by boxing the stream using Box::pin
or
pinning it to the stack using the pin_mut!
macro from the pin_utils
crate.
Examples
use tokio::stream::{self, StreamExt}; let mut stream = stream::iter(1..=3); assert_eq!(stream.next().await, Some(1)); assert_eq!(stream.next().await, Some(2)); assert_eq!(stream.next().await, Some(3)); assert_eq!(stream.next().await, None);
fn map<T, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> T,
Self: Sized,
F: FnMut(Self::Item) -> T,
Self: Sized,
feature="stream"
only.Maps this stream's items to a different type, returning a new stream of the resulting type.
The provided closure is executed over all elements of this stream as
they are made available. It is executed inline with calls to
poll_next
.
Note that this function consumes the stream passed into it and returns a
wrapped version of it, similar to the existing map
methods in the
standard library.
Examples
use tokio::stream::{self, StreamExt}; let stream = stream::iter(1..=3); let mut stream = stream.map(|x| x + 3); assert_eq!(stream.next().await, Some(4)); assert_eq!(stream.next().await, Some(5)); assert_eq!(stream.next().await, Some(6));