Trait TryStreamExt
trait TryStreamExt: TryStream
Adapters specific to Result-returning streams
Provided Methods
fn err_into<E>(self: Self) -> ErrInto<Self, E> where Self: Sized, <Self as >::Error: Into<E>Wraps the current stream in a new stream which converts the error type into the one provided.
Examples
# block_onfn map_ok<T, F>(self: Self, f: F) -> MapOk<Self, F> where Self: Sized, F: FnMut(<Self as >::Ok) -> TWraps the current stream in a new stream which maps the success value using the provided closure.
Examples
# block_onfn map_err<E, F>(self: Self, f: F) -> MapErr<Self, F> where Self: Sized, F: FnMut(<Self as >::Error) -> EWraps the current stream in a new stream which maps the error value using the provided closure.
Examples
# block_onfn and_then<Fut, F>(self: Self, f: F) -> AndThen<Self, Fut, F> where F: FnMut(<Self as >::Ok) -> Fut, Fut: TryFuture<Error = <Self as >::Error>, Self: SizedChain on a computation for when a value is ready, passing the successful results to the provided closure
f.This function can be used to run a unit of work when the next successful value on a stream is ready. The closure provided will be yielded a value when ready, and the returned future will then be run to completion to produce the next value on this stream.
Any errors produced by this stream will not be passed to the closure, and will be passed through.
The returned value of the closure must implement the
TryFuturetrait and can represent some more work to be done before the composed stream is finished.Note that this function consumes the receiving stream and returns a wrapped version of it.
To process the entire stream and return a single future representing success or error, use
try_for_eachinstead.Examples
use mpsc; use future; use TryStreamExt; let = ; let rx = rx.and_then;fn or_else<Fut, F>(self: Self, f: F) -> OrElse<Self, Fut, F> where F: FnMut(<Self as >::Error) -> Fut, Fut: TryFuture<Ok = <Self as >::Ok>, Self: SizedChain on a computation for when an error happens, passing the erroneous result to the provided closure
f.This function can be used to run a unit of work and attempt to recover from an error if one happens. The closure provided will be yielded an error when one appears, and the returned future will then be run to completion to produce the next value on this stream.
Any successful values produced by this stream will not be passed to the closure, and will be passed through.
The returned value of the closure must implement the
TryFuturetrait and can represent some more work to be done before the composed stream is finished.Note that this function consumes the receiving stream and returns a wrapped version of it.
fn inspect_ok<F>(self: Self, f: F) -> InspectOk<Self, F> where F: FnMut(&<Self as >::Ok), Self: SizedDo something with the success value of this stream, afterwards passing it on.
This is similar to the
StreamExt::inspectmethod where it allows easily inspecting the success value as it passes through the stream, for example to debug what's going on.fn inspect_err<F>(self: Self, f: F) -> InspectErr<Self, F> where F: FnMut(&<Self as >::Error), Self: SizedDo something with the error value of this stream, afterwards passing it on.
This is similar to the
StreamExt::inspectmethod where it allows easily inspecting the error value as it passes through the stream, for example to debug what's going on.fn into_stream(self: Self) -> IntoStream<Self> where Self: SizedWraps a
TryStreaminto a type that implementsStreamTryStreams currently do not implement theStreamtrait because of limitations of the compiler.Examples
use ; # type T = i32; # type E = ; take_stream;fn try_next(self: &mut Self) -> TryNext<'_, Self> where Self: UnpinCreates 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::nextcombinator, but returns aResult<Option<T>, E>rather than anOption<Result<T, E>>, making for easy use with the?operator.Examples
# block_onfn try_for_each<Fut, F>(self: Self, f: F) -> TryForEach<Self, Fut, F> where F: FnMut(<Self as >::Ok) -> Fut, Fut: TryFuture<Ok = (), Error = <Self as >::Error>, Self: SizedAttempts 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
Futurewhere theOutputtype isResult<(), Self::Error>. If any of the intermediate futures or the stream returns an error, this future will return immediately with an error.Examples
# block_onfn try_skip_while<Fut, F>(self: Self, f: F) -> TrySkipWhile<Self, Fut, F> where F: FnMut(&<Self as >::Ok) -> Fut, Fut: TryFuture<Ok = bool, Error = <Self as >::Error>, Self: SizedSkip elements on this stream while the provided asynchronous predicate resolves to
true.This function is similar to
StreamExt::skip_whilebut exits early if an error occurs.Examples
# block_onfn try_take_while<Fut, F>(self: Self, f: F) -> TryTakeWhile<Self, Fut, F> where F: FnMut(&<Self as >::Ok) -> Fut, Fut: TryFuture<Ok = bool, Error = <Self as >::Error>, Self: SizedTake elements on this stream while the provided asynchronous predicate resolves to
true.This function is similar to
StreamExt::take_whilebut exits early if an error occurs.Examples
# block_onfn try_for_each_concurrent<Fut, F, impl Into<Option<usize>>: Into<Option<usize>>>(self: Self, limit: impl Into<Option<usize>>, f: F) -> TryForEachConcurrent<Self, Fut, F> where F: FnMut(<Self as >::Ok) -> Fut, Fut: Future<Output = Result<(), <Self as >::Error>>, Self: SizedAttempts to run this stream to completion, executing the provided asynchronous closure for each element on the stream concurrently as elements become available, exiting as soon as an error occurs.
This is similar to
StreamExt::for_each_concurrent, but will resolve to an error immediately if the underlying stream or the provided closure return an error.This method is only available when the
stdorallocfeature of this library is activated, and it is activated by default.Examples
# block_onfn try_collect<C: Default + Extend<<Self as >::Ok>>(self: Self) -> TryCollect<Self, C> where Self: SizedAttempt to transform a stream into a collection, returning a future representing the result of that computation.
This combinator will collect all successful results of this stream and collect them into the specified collection type. 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.
Examples
# block_onfn try_chunks(self: Self, capacity: usize) -> TryChunks<Self> where Self: SizedAn adaptor for chunking up successful items of the stream inside a vector.
This combinator will attempt to pull successful items from this stream and buffer them into a local vector. At most
capacityitems will get buffered before they're yielded from the returned stream.Note that the vectors returned from this iterator may not always have
capacityelements. If the underlying stream ended and only a partial vector was created, it'll be returned. Additionally if an error happens from the underlying stream then the currently buffered items will be yielded.This method is only available when the
stdorallocfeature of this library is activated, and it is activated by default.This function is similar to
StreamExt::chunksbut exits early if an error occurs.Examples
# block_onPanics
This method will panic if
capacityis zero.fn try_ready_chunks(self: Self, capacity: usize) -> TryReadyChunks<Self> where Self: SizedAn adaptor for chunking up successful, ready items of the stream inside a vector.
This combinator will attempt to pull successful items from this stream and buffer them into a local vector. At most
capacityitems will get buffered before they're yielded from the returned stream. If the underlying stream returnsPoll::Pending, and the collected chunk is not empty, it will be immediately returned.Note that the vectors returned from this iterator may not always have
capacityelements. If the underlying stream ended and only a partial vector was created, it'll be returned. Additionally if an error happens from the underlying stream then the currently buffered items will be yielded.This method is only available when the
stdorallocfeature of this library is activated, and it is activated by default.This function is similar to
StreamExt::ready_chunksbut exits early if an error occurs.Examples
# block_onPanics
This method will panic if
capacityis zero.fn try_filter<Fut, F>(self: Self, f: F) -> TryFilter<Self, Fut, F> where Fut: Future<Output = bool>, F: FnMut(&<Self as >::Ok) -> Fut, Self: SizedAttempt to filter the values produced by this stream according to the provided asynchronous closure.
As values of this stream are made available, the provided predicate
fwill be run on them. If the predicate returns aFuturewhich resolves totrue, then the stream will yield the value, but if the predicate return aFuturewhich resolves tofalse, then the value will be discarded and the next value will be produced.All errors are passed through without filtering in this combinator.
Note that this function consumes the stream passed into it and returns a wrapped version of it, similar to the existing
filtermethods in the standard library.Examples
# block_onfn try_filter_map<Fut, F, T>(self: Self, f: F) -> TryFilterMap<Self, Fut, F> where Fut: TryFuture<Ok = Option<T>, Error = <Self as >::Error>, F: FnMut(<Self as >::Ok) -> Fut, Self: SizedAttempt to filter the values produced by this stream while simultaneously mapping them to a different type according to the provided asynchronous closure.
As values of this stream are made available, the provided function will be run on them. If the future returned by the predicate
fresolves toSome(item)then the stream will yield the valueitem, but if it resolves toNonethen the next value will be produced.All errors are passed through without filtering in this combinator.
Note that this function consumes the stream passed into it and returns a wrapped version of it, similar to the existing
filter_mapmethods in the standard library.Examples
# block_onfn try_flatten_unordered<impl Into<Option<usize>>: Into<Option<usize>>>(self: Self, limit: impl Into<Option<usize>>) -> TryFlattenUnordered<Self> where <Self as >::Ok: TryStream + Unpin, <<Self as >::Ok as TryStream>::Error: From<<Self as >::Error>, Self: SizedFlattens a stream of streams into just one continuous stream. Produced streams will be polled concurrently and any errors will be passed through without looking at them. If the underlying base stream returns an error, it will be immediately propagated.
The only argument is an optional limit on the number of concurrently polled streams. If this limit is not
None, no more thanlimitstreams will be polled at the same time. Thelimitargument is of typeInto<Option<usize>>, and so can be provided as eitherNone,Some(10), or just10. Note: a limit of zero is interpreted as no limit at all, and will have the same result as passing inNone.Examples
# block_on;fn try_flatten(self: Self) -> TryFlatten<Self> where <Self as >::Ok: TryStream, <<Self as >::Ok as TryStream>::Error: From<<Self as >::Error>, Self: SizedFlattens a stream of streams into just one continuous stream.
If this stream's elements are themselves streams then this combinator will flatten out the entire stream to one long chain of elements. Any errors are passed through without looking at them, but otherwise each individual stream will get exhausted before moving on to the next.
Examples
# block_on;fn try_fold<T, Fut, F>(self: Self, init: T, f: F) -> TryFold<Self, Fut, T, F> where F: FnMut(T, <Self as >::Ok) -> Fut, Fut: TryFuture<Ok = T, Error = <Self as >::Error>, Self: SizedAttempt to execute an accumulating asynchronous computation over a stream, collecting all the values into one final result.
This combinator will accumulate all values returned by this stream according to the closure provided. The initial state is also provided to this method and then is returned again by each execution of the closure. Once the entire stream has been exhausted the returned future will resolve to this value.
This method is similar to
fold, but will exit early if an error is encountered in either the stream or the provided closure.Examples
# block_onfn try_concat(self: Self) -> TryConcat<Self> where Self: Sized, <Self as >::Ok: Extend<<<Self as TryStream>::Ok as IntoIterator>::Item> + IntoIterator + DefaultAttempt to concatenate all items of a stream into a single extendable destination, returning a future representing the end result.
This combinator will extend the first item with the contents of all the subsequent successful results of the stream. If the stream is empty, the default value will be returned.
Works with all collections that implement the
Extendtrait.This method is similar to
concat, but will exit early if an error is encountered in the stream.Examples
# block_on;fn try_buffer_unordered(self: Self, n: usize) -> TryBufferUnordered<Self> where <Self as >::Ok: TryFuture<Error = <Self as >::Error>, Self: SizedAttempt to execute several futures from a stream concurrently (unordered).
This stream's
Oktype must be aTryFuturewith anErrortype that matches the stream'sErrortype.This adaptor will buffer up to
nfutures and then return their outputs in the order in which they complete. If the underlying stream returns an error, it will be immediately propagated.The returned stream will be a stream of results, each containing either an error or a future's output. An error can be produced either by the underlying stream itself or by one of the futures it yielded.
This method is only available when the
stdorallocfeature of this library is activated, and it is activated by default.Examples
Results are returned in the order of completion:
# block_on.unwrap;Errors from the underlying stream itself are propagated:
# block_on.unwrap;fn try_buffered(self: Self, n: usize) -> TryBuffered<Self> where <Self as >::Ok: TryFuture<Error = <Self as >::Error>, Self: SizedAttempt to execute several futures from a stream concurrently.
This stream's
Oktype must be aTryFuturewith anErrortype that matches the stream'sErrortype.This adaptor will buffer up to
nfutures and then return their outputs in the same order as the underlying stream. If the underlying stream returns an error, it will be immediately propagated.The returned stream will be a stream of results, each containing either an error or a future's output. An error can be produced either by the underlying stream itself or by one of the futures it yielded.
This method is only available when the
stdorallocfeature of this library is activated, and it is activated by default.Examples
Results are returned in the order of addition:
# block_on.unwrap;Errors from the underlying stream itself are propagated:
# block_on.unwrap;fn try_poll_next_unpin(self: &mut Self, cx: &mut Context<'_>) -> Poll<Option<Result<<Self as >::Ok, <Self as >::Error>>> where Self: UnpinA convenience method for calling
TryStream::try_poll_nextonUnpinstream types.fn into_async_read(self: Self) -> IntoAsyncRead<Self> where Self: Sized + TryStreamExt<Error = Error>, <Self as >::Ok: AsRef<[u8]>Adapter that converts this stream into an
AsyncBufRead.This method is only available when the
stdfeature of this library is activated, and it is activated by default.Examples
# block_onfn try_all<Fut, F>(self: Self, f: F) -> TryAll<Self, Fut, F> where Self: Sized, F: FnMut(<Self as >::Ok) -> Fut, Fut: Future<Output = bool>Attempt to execute a predicate over an asynchronous stream and evaluate if all items satisfy the predicate. Exits early if an
Erris encountered or if anOkitem is found that does not satisfy the predicate.Examples
# block_on;fn try_any<Fut, F>(self: Self, f: F) -> TryAny<Self, Fut, F> where Self: Sized, F: FnMut(<Self as >::Ok) -> Fut, Fut: Future<Output = bool>Attempt to execute a predicate over an asynchronous stream and evaluate if any items satisfy the predicate. Exits early if an
Erris encountered or if anOkitem is found that satisfies the predicate.Examples
# block_on;
Implementors
impl<S> TryStreamExt for SkipWhile<St, Fut, F>impl<S> TryStreamExt for AndThen<St, Fut, F>impl<S> TryStreamExt for TryBuffered<St>impl<S> TryStreamExt for Buffer<Si, Item>impl<S> TryStreamExt for TryUnfold<T, F, Fut>impl<S> TryStreamExt for Chain<St1, St2>impl<S> TryStreamExt for Buffered<St>impl<S> TryStreamExt for IntoStream<F>impl<S> TryStreamExt for InspectOk<St, F>impl<S> TryStreamExt for Skip<St>impl<S> TryStreamExt for FuturesOrdered<T>impl<S> TryStreamExt for Once<Fut>impl<S> TryStreamExt for TryFlattenUnordered<St>impl<S> TryStreamExt for TryReadyChunks<St>impl<S> TryStreamExt for TryBufferUnordered<St>impl<S> TryStreamExt for Abortable<T>impl<S> TryStreamExt for Map<St, F>impl<S> TryStreamExt for PollFn<F>impl<S> TryStreamExt for FilterMap<St, Fut, F>impl<S> TryStreamExt for SelectAll<St>impl<S> TryStreamExt for Pending<T>impl<S> TryStreamExt for MapOk<St, F>impl<S> TryStreamExt for Select<St1, St2>impl<S> TryStreamExt for Cycle<St>impl<S> TryStreamExt for IntoStream<St>impl<S> TryStreamExt for Then<St, Fut, F>impl<S> TryStreamExt for FlattenStream<F>impl<S> TryStreamExt for TryFlatten<St>impl<S> TryStreamExt for TryFilterMap<St, Fut, F>impl<S> TryStreamExt for Inspect<St, F>impl<S> TryStreamExt for With<Si, Item, U, Fut, F>impl<S> TryStreamExt for SinkMapErr<Si, F>impl<S> TryStreamExt for SinkErrInto<Si, Item, E>impl<S> TryStreamExt for WithFlatMap<Si, Item, U, St, F>impl<S> TryStreamExt for SelectWithStrategy<St1, St2, Clos, State>impl<S> TryStreamExt for Flatten<St>impl<S> TryStreamExt for Either<A, B>impl<S> TryStreamExt for BufferUnordered<St>impl<S> TryStreamExt for MapErr<St, F>impl<S> TryStreamExt for Unfold<T, F, Fut>impl<S> TryStreamExt for Iter<I>impl<S> TryStreamExt for TryTakeWhile<St, Fut, F>impl<S> TryStreamExt for TryChunks<St>impl<S> TryStreamExt for FlattenSink<Fut, Si>impl<S> TryStreamExt for FlatMapUnordered<St, U, F>impl<S> TryStreamExt for SplitStream<S>impl<S> TryStreamExt for Filter<St, Fut, F>impl<S> TryStreamExt for Peekable<St>impl<S> TryStreamExt for TakeWhile<St, Fut, F>impl<S> TryStreamExt for ErrInto<St, E>impl<S> TryStreamExt for TakeUntil<St, Fut>impl<S> TryStreamExt for FuturesUnordered<Fut>impl<S> TryStreamExt for TrySkipWhile<St, Fut, F>impl<S> TryStreamExt for FlatMap<St, U, F>impl<S> TryStreamExt for TryFilter<St, Fut, F>impl<S> TryStreamExt for Lines<R>impl<S> TryStreamExt for Empty<T>impl<S> TryStreamExt for CatchUnwind<St>impl<S> TryStreamExt for RepeatWith<F>impl<S> TryStreamExt for Scan<St, S, Fut, F>impl<S> TryStreamExt for Fuse<St>impl<S> TryStreamExt for TryFlattenStream<Fut>impl<S: ?Sized + TryStream> TryStreamExt for Simpl<S> TryStreamExt for InspectErr<St, F>impl<S> TryStreamExt for Repeat<T>impl<S> TryStreamExt for OrElse<St, Fut, F>impl<S> TryStreamExt for Take<St>