Trait futures::stream::TryStreamExt [−][src]
pub trait TryStreamExt: TryStream { fn err_into<E>(self) -> ErrInto<Self, E>
where
Self::Error: Into<E>, { ... } fn map_ok<T, F>(self, f: F) -> MapOk<Self, F>
where
F: FnMut(Self::Ok) -> T, { ... } fn map_err<E, F>(self, f: F) -> MapErr<Self, F>
where
F: FnMut(Self::Error) -> E, { ... } fn try_next(&mut self) -> TryNext<Self>
where
Self: Unpin, { ... } fn try_for_each<Fut, F>(self, f: F) -> TryForEach<Self, Fut, F>
where
F: FnMut(Self::Ok) -> Fut,
Fut: TryFuture<Ok = (), Error = Self::Error>, { ... } fn try_collect<C>(self) -> TryCollect<Self, C>
where
C: Default + Extend<Self::Ok>, { ... } }
Adapters specific to Result
-returning streams
Provided Methods
fn err_into<E>(self) -> ErrInto<Self, E> where
Self::Error: Into<E>,
Self::Error: Into<E>,
Wraps the current stream in a new stream which converts the error type into the one provided.
Examples
#![feature(async_await, await_macro)] use futures::stream::{self, TryStreamExt}; let mut stream = stream::iter(vec![Ok(()), Err(5i32)]) .err_into::<i64>(); assert_eq!(await!(stream.try_next()), Ok(Some(()))); assert_eq!(await!(stream.try_next()), Err(5i64));
fn map_ok<T, F>(self, f: F) -> MapOk<Self, F> where
F: FnMut(Self::Ok) -> T,
F: FnMut(Self::Ok) -> T,
Wraps the current stream in a new stream which maps the success value using the provided closure.
Examples
#![feature(async_await, await_macro)] use futures::stream::{self, TryStreamExt}; let mut stream = stream::iter(vec![Ok(5), Err(0)]) .map_ok(|x| x + 2); assert_eq!(await!(stream.try_next()), Ok(Some(7))); assert_eq!(await!(stream.try_next()), Err(0));
fn map_err<E, F>(self, f: F) -> MapErr<Self, F> where
F: FnMut(Self::Error) -> E,
F: FnMut(Self::Error) -> E,
Wraps the current stream in a new stream which maps the error value using the provided closure.
Examples
#![feature(async_await, await_macro)] use futures::stream::{self, TryStreamExt}; let mut stream = stream::iter(vec![Ok(5), Err(0)]) .map_err(|x| x + 2); assert_eq!(await!(stream.try_next()), Ok(Some(5))); assert_eq!(await!(stream.try_next()), Err(2));
fn try_next(&mut self) -> TryNext<Self> where
Self: Unpin,
Self: Unpin,
Creates a future that attempts to resolve the next item in the stream. If an error is encountered before the next item, the error is returned instead.
This is similar to the Stream::next
combinator, but returns a
Result<Option<T>, E>
rather than an Option<Result<T, E>>
, making
for easy use with the ?
operator.
Examples
#![feature(async_await, await_macro)] use futures::stream::{self, TryStreamExt}; let mut stream = stream::iter(vec![Ok(()), Err(())]); assert_eq!(await!(stream.try_next()), Ok(Some(()))); assert_eq!(await!(stream.try_next()), Err(()));
fn try_for_each<Fut, F>(self, f: F) -> TryForEach<Self, Fut, F> where
F: FnMut(Self::Ok) -> Fut,
Fut: TryFuture<Ok = (), Error = Self::Error>,
F: FnMut(Self::Ok) -> Fut,
Fut: TryFuture<Ok = (), Error = Self::Error>,
Attempts to run this stream to completion, executing the provided asynchronous closure for each element on the stream.
The provided closure will be called for each item this stream produces, yielding a future. That future will then be executed to completion before moving on to the next item.
The returned value is a Future
where the
Output
type is
Result<(), Self::Error>
. If any of the intermediate
futures or the stream returns an error, this future will return
immediately with an error.
Examples
#![feature(async_await, await_macro)] use futures::future; use futures::stream::{self, TryStreamExt}; let mut x = 0i32; { let fut = stream::repeat(Ok(1)).try_for_each(|item| { x += item; future::ready(if x == 3 { Err(()) } else { Ok(()) }) }); assert_eq!(await!(fut), Err(())); } assert_eq!(x, 3);
fn try_collect<C>(self) -> TryCollect<Self, C> where
C: Default + Extend<Self::Ok>,
C: Default + Extend<Self::Ok>,
Attempt to Collect all of the values of this stream into a vector, returning a future representing the result of that computation.
This combinator will collect all successful results of this stream and
collect them into a Vec<Self::Item>
. If an error happens then all
collected elements will be dropped and the error will be returned.
The returned future will be resolved when the stream terminates.
This method is only available when the std
feature of this
library is activated, and it is activated by default.
Examples
#![feature(async_await, await_macro)] use futures::channel::mpsc; use futures::executor::block_on; use futures::stream::TryStreamExt; use std::thread; let (mut tx, rx) = mpsc::unbounded(); thread::spawn(move || { for i in (1..=5) { tx.unbounded_send(Ok(i)).unwrap(); } tx.unbounded_send(Err(6)).unwrap(); }); let output: Result<Vec<i32>, i32> = await!(rx.try_collect()); assert_eq!(output, Err(6));
Implementors
impl<S> TryStreamExt for S where
S: TryStream,