Trait FutureExt
trait FutureExt: Future
An extension trait for Futures that provides a variety of convenient
adapters.
Provided Methods
fn map<U, F>(self: Self, f: F) -> Map<Self, F> where F: FnOnce(<Self as >::Output) -> U, Self: SizedMap this future's output to a different type, returning a new future of the resulting type.
This function is similar to the
Option::maporIterator::mapwhere it will change the type of the underlying future. This is useful to chain along a computation once a future has been resolved.Note that this function consumes the receiving future and returns a wrapped version of it, similar to the existing
mapmethods in the standard library.Examples
# block_on;fn map_into<U>(self: Self) -> MapInto<Self, U> where <Self as >::Output: Into<U>, Self: SizedMap this future's output to a different type, returning a new future of the resulting type.
This function is equivalent to calling
map(Into::into)but allows naming the return type.fn then<Fut, F>(self: Self, f: F) -> Then<Self, Fut, F> where F: FnOnce(<Self as >::Output) -> Fut, Fut: Future, Self: SizedChain on a computation for when a future finished, passing the result of the future to the provided closure
f.The returned value of the closure must implement the
Futuretrait and can represent some more work to be done before the composed future is finished.The closure
fis only run after successful completion of theselffuture.Note that this function consumes the receiving future and returns a wrapped version of it.
Examples
# block_on;fn left_future<B>(self: Self) -> Either<Self, B> where B: Future<Output = <Self as >::Output>, Self: SizedWrap this future in an
Eitherfuture, making it the left-hand variant of thatEither.This can be used in combination with the
right_futuremethod to writeifstatements that evaluate to different futures in different branches.Examples
# block_on;fn right_future<A>(self: Self) -> Either<A, Self> where A: Future<Output = <Self as >::Output>, Self: SizedWrap this future in an
Eitherfuture, making it the right-hand variant of thatEither.This can be used in combination with the
left_futuremethod to writeifstatements that evaluate to different futures in different branches.Examples
# block_on;fn into_stream(self: Self) -> IntoStream<Self> where Self: SizedConvert this future into a single element stream.
The returned stream contains single success if this future resolves to success or single error if this future resolves into error.
Examples
# block_on;fn flatten(self: Self) -> Flatten<Self> where <Self as >::Output: Future, Self: SizedFlatten the execution of this future when the output of this future is itself another future.
This can be useful when combining futures together to flatten the computation out the final result.
This method is roughly equivalent to
self.then(|x| x).Note that this function consumes the receiving future and returns a wrapped version of it.
Examples
# block_on;fn flatten_stream(self: Self) -> FlattenStream<Self> where <Self as >::Output: Stream, Self: SizedFlatten the execution of this future when the successful result of this future is a stream.
This can be useful when stream initialization is deferred, and it is convenient to work with that stream as if stream was available at the call site.
Note that this function consumes this future and returns a wrapped version of it.
Examples
# block_on;fn fuse(self: Self) -> Fuse<Self> where Self: SizedFuse a future such that
pollwill never again be called once it has completed. This method can be used to turn anyFutureinto aFusedFuture.Normally, once a future has returned
Poll::Readyfrompoll, any further calls could exhibit bad behavior such as blocking forever, panicking, never returning, etc. If it is known thatpollmay be called too often then this method can be used to ensure that it has defined semantics.If a
fused future ispolled after having returnedPoll::Readypreviously, it will returnPoll::Pending, frompollagain (and will continue to do so for all future calls topoll).This combinator will drop the underlying future as soon as it has been completed to ensure resources are reclaimed as soon as possible.
fn inspect<F>(self: Self, f: F) -> Inspect<Self, F> where F: FnOnce(&<Self as >::Output), Self: SizedDo something with the output of a future before passing it on.
When using futures, you'll often chain several of them together. While working on such code, you might want to check out what's happening at various parts in the pipeline, without consuming the intermediate value. To do that, insert a call to
inspect.Examples
# block_on;fn catch_unwind(self: Self) -> CatchUnwind<Self> where Self: Sized + UnwindSafeCatches unwinding panics while polling the future.
In general, panics within a future can propagate all the way out to the task level. This combinator makes it possible to halt unwinding within the future itself. It's most commonly used within task executors. It's not recommended to use this for error handling.
Note that this method requires the
UnwindSafebound from the standard library. This isn't always applied automatically, and the standard library provides anAssertUnwindSafewrapper type to apply it after-the fact. To assist using this method, theFuturetrait is also implemented forAssertUnwindSafe<F>whereFimplementsFuture.This method is only available when the
stdfeature of this library is activated, and it is activated by default.Examples
# block_on;Create a cloneable handle to this future where all handles will resolve to the same result.
The
sharedcombinator method provides a method to convert any future into a cloneable future. It enables a future to be polled by multiple threads.This method is only available when the
stdfeature of this library is activated, and it is activated by default.Examples
# block_on;# block_on;fn remote_handle(self: Self) -> (Remote<Self>, RemoteHandle<<Self as >::Output>) where Self: SizedTurn this future into a future that yields
()on completion and sends its output to another future on a separate task.This can be used with spawning executors to easily retrieve the result of a future executing on a separate task or thread.
This method is only available when the
stdfeature of this library is activated, and it is activated by default.fn boxed<'a>(self: Self) -> BoxFuture<'a, <Self as >::Output> where Self: Sized + Send + 'aWrap the future in a Box, pinning it.
This method is only available when the
stdorallocfeature of this library is activated, and it is activated by default.fn boxed_local<'a>(self: Self) -> LocalBoxFuture<'a, <Self as >::Output> where Self: Sized + 'aWrap the future in a Box, pinning it.
Similar to
boxed, but without theSendrequirement.This method is only available when the
stdorallocfeature of this library is activated, and it is activated by default.fn unit_error(self: Self) -> UnitError<Self> where Self: SizedTurns a
Future<Output = T>into aTryFuture<Ok = T, Error = ()>.fn never_error(self: Self) -> NeverError<Self> where Self: SizedTurns a
Future<Output = T>into aTryFuture<Ok = T, Error = Never>.fn poll_unpin(self: &mut Self, cx: &mut Context<'_>) -> Poll<<Self as >::Output> where Self: UnpinA convenience for calling
Future::pollonUnpinfuture types.fn now_or_never(self: Self) -> Option<<Self as >::Output> where Self: SizedEvaluates and consumes the future, returning the resulting output if the future is ready after the first call to
Future::poll.If
pollinstead returnsPoll::Pending,Noneis returned.This method is useful in cases where immediacy is more important than waiting for a result. It is also convenient for quickly obtaining the value of a future that is known to always resolve immediately.
Examples
# use *; use ; let future_ready = ready; let future_pending = ; assert_eq!; assert_eq!;In cases where it is absolutely known that a future should always resolve immediately and never return
Poll::Pending, this method can be combined withexpect():# use ; let future_ready = ready; assert_eq!;
Implementors
impl<T> FutureExt for Remote<Fut>impl<T> FutureExt for OrElse<Fut1, Fut2, F>impl<T> FutureExt for Fuse<Fut>impl<T> FutureExt for Map<Fut, F>impl<T> FutureExt for IntoFuture<Fut>impl<T> FutureExt for All<St, Fut, F>impl<T> FutureExt for Join5<Fut1, Fut2, Fut3, Fut4, Fut5>impl<T> FutureExt for NextIfEq<'a, St, T>impl<T> FutureExt for ReadExact<'a, R>impl<T> FutureExt for Flatten<F>impl<T> FutureExt for Copy<'a, R, W>impl<T> FutureExt for Ready<T>impl<T> FutureExt for ReadVectored<'a, R>impl<T> FutureExt for MapOkOrElse<Fut, F, G>impl<T> FutureExt for TryFold<St, Fut, T, F>impl<T> FutureExt for ErrInto<Fut, E>impl<T> FutureExt for TryJoin5<Fut1, Fut2, Fut3, Fut4, Fut5>impl<T> FutureExt for Join3<Fut1, Fut2, Fut3>impl<T> FutureExt for ForEach<St, Fut, F>impl<T> FutureExt for SendAll<'a, Si, St>impl<T> FutureExt for ForEachConcurrent<St, Fut, F>impl<T> FutureExt for Read<'a, R>impl<T> FutureExt for InspectOk<Fut, F>impl<T> FutureExt for UnitError<Fut>impl<T> FutureExt for TryCollect<St, C>impl<T> FutureExt for Seek<'a, S>impl<T> FutureExt for CopyBuf<'a, R, W>impl<T> FutureExt for OptionFuture<F>impl<T> FutureExt for Collect<St, C>impl<T> FutureExt for Count<St>impl<T> FutureExt for Any<St, Fut, F>impl<T> FutureExt for TryJoin3<Fut1, Fut2, Fut3>impl<T> FutureExt for Join4<Fut1, Fut2, Fut3, Fut4>impl<T> FutureExt for Select<A, B>impl<T> FutureExt for StreamFuture<St>impl<T> FutureExt for TryMaybeDone<Fut>impl<T> FutureExt for InspectErr<Fut, F>impl<T> FutureExt for Fold<St, Fut, T, F>impl<T> FutureExt for Inspect<Fut, F>impl<T> FutureExt for CatchUnwind<Fut>impl<T> FutureExt for Timpl<T> FutureExt for WriteAll<'a, W>impl<T> FutureExt for Close<'a, Si, Item>impl<T> FutureExt for TryJoin4<Fut1, Fut2, Fut3, Fut4>impl<T> FutureExt for TryAny<St, Fut, F>impl<T> FutureExt for Abortable<T>impl<T> FutureExt for WriteVectored<'a, W>impl<T> FutureExt for Lazy<F>impl<T> FutureExt for MapOk<Fut, F>impl<T> FutureExt for NeverError<Fut>impl<T> FutureExt for TryAll<St, Fut, F>impl<T> FutureExt for TryForEach<St, Fut, F>impl<T> FutureExt for Forward<St, Si>impl<T> FutureExt for ReadToEnd<'a, R>impl<T> FutureExt for Concat<St>impl<T> FutureExt for Write<'a, W>impl<T> FutureExt for Flush<'a, Si, Item>impl<T> FutureExt for PollImmediate<T>impl<T> FutureExt for SelectNextSome<'a, St>impl<T> FutureExt for JoinAll<F>impl<T> FutureExt for Flush<'a, W>impl<T> FutureExt for Feed<'a, Si, Item>impl<T> FutureExt for TrySelect<A, B>impl<T> FutureExt for TryFlatten<Fut1, Fut2>impl<T> FutureExt for PollFn<F>impl<T> FutureExt for Join<Fut1, Fut2>impl<T> FutureExt for Next<'a, St>impl<T> FutureExt for Send<'a, Si, Item>impl<T> FutureExt for ReadToString<'a, R>impl<T> FutureExt for OkInto<Fut, E>impl<T> FutureExt for MapInto<Fut, T>impl<T> FutureExt for TryJoinAll<F>impl<T> FutureExt for Peek<'a, St>impl<T> FutureExt for Either<A, B>impl<T> FutureExt for TryNext<'a, St>impl<T> FutureExt for TryJoin<Fut1, Fut2>impl<T> FutureExt for Close<'a, W>impl<T> FutureExt for MaybeDone<Fut>impl<T> FutureExt for ReadLine<'a, R>impl<T> FutureExt for TryForEachConcurrent<St, Fut, F>impl<T> FutureExt for ReadUntil<'a, R>impl<T> FutureExt for Shared<Fut>impl<T> FutureExt for MutexLockFuture<'a, T>impl<T> FutureExt for Then<Fut1, Fut2, F>impl<T> FutureExt for PeekMut<'a, St>impl<T> FutureExt for SelectAll<Fut>impl<T> FutureExt for UnwrapOrElse<Fut, F>impl<T> FutureExt for AndThen<Fut1, Fut2, F>impl<T> FutureExt for AlwaysReady<T, F>impl<T> FutureExt for FillBuf<'a, R>impl<T> FutureExt for RemoteHandle<T>impl<T> FutureExt for NextIf<'a, St, F>impl<T> FutureExt for SelectOk<Fut>impl<T> FutureExt for SeeKRelative<'a, R>impl<T> FutureExt for OwnedMutexLockFuture<T>impl<T> FutureExt for TryConcat<St>impl<T> FutureExt for CopyBufAbortable<'a, R, W>impl<T> FutureExt for Unzip<St, FromA, FromB>impl<T> FutureExt for Pending<T>impl<T> FutureExt for MapErr<Fut, F>