futures::future

Trait Future

Source
pub trait Future {
    type Item;
    type Error;

    // Required method
    fn poll(
        &mut self,
        cx: &mut Context<'_>,
    ) -> Result<Async<Self::Item>, Self::Error>;
}
Expand description

A future represents an asychronous computation that may fail.

A future is like a Result value that may not have finished computing yet. This kind of “asynchronous value” makes it possible for a thread to continue doing useful work while it waits for the value to become available.

The ergonomics and implementation of the Future trait are very similar to the Iterator trait in that there is just one method you need to implement, but you get a whole lot of others for free as a result. These other methods allow you to chain together large computations based on futures, which will automatically handle asynchrony for you.

§The poll method

The core method of future, poll, attempts to resolve the future into a final value. This method does not block if the value is not ready. Instead, the current task is scheduled to be woken up when it’s possible to make further progress by polling again. The wake up is performed using cx.waker(), a handle for waking up the current task.

When using a future, you generally won’t call poll directly, but instead use combinators to build up asynchronous computations. A complete computation can then be spawned onto an executor as a new, independent task that will automatically be polled to completion.

§Combinators

Like iterators, futures provide a large number of combinators to work with futures to express computations in a much more natural method than scheduling a number of callbacks. As with iterators, the combinators are zero-cost: they compile away. You can find the combinators in the future-util crate.

Required Associated Types§

Source

type Item

A successful value

Source

type Error

An error

Required Methods§

Source

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<Self::Item>, Self::Error>

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available.

§Return value

This function returns:

  • Ok(Async::Pending) if the future is not ready yet
  • Ok(Async::Ready(val)) with the result val of this future if it finished successfully.
  • Err(err) if the future is finished but resolved to an error err.

Once a future has finished, clients should not poll it again.

When a future is not ready yet, poll returns Async::Pending. The future will also register the interest of the current task in the value being produced. For example, if the future represents the availability of data on a socket, then the task is recorded so that when data arrives, it is woken up (via cx.waker(). Once a task has been woken up, it should attempt to poll the future again, which may or may not produce a final value.

Note that if Pending is returned it only means that the current task (represented by the argument cx) will receive a notification. Tasks from previous calls to poll will not receive notifications.

§Runtime characteristics

Futures alone are inert; they must be actively polled to make progress, meaning that each time the current task is woken up, it should actively re-poll pending futures that it still has an interest in. Usually this is done by building up a large computation as a single future (using combinators), then spawning that future as a task onto an executor. Executors ensure that each task is polled every time a future internal to that task is ready to make progress.

The poll function is not called repeatedly in a tight loop for futures, but only whenever the future itself is ready, as signaled via cx.waker(). If you’re familiar with the poll(2) or select(2) syscalls on Unix it’s worth noting that futures typically do not suffer the same problems of “all wakeups must poll all events”; they are more like epoll(4).

An implementation of poll should strive to return quickly, and must never block. Returning quickly prevents unnecessarily clogging up threads or event loops. If it is known ahead of time that a call to poll may end up taking awhile, the work should be offloaded to a thread pool (or something similar) to ensure that poll can return quickly.

§Errors

This future may have failed to finish the computation, in which case the Err variant will be returned with an appropriate payload of an error.

§Panics

Once a future has completed (returned Ready or Err from poll), then any future calls to poll may panic, block forever, or otherwise cause bad behavior. The Future trait itself provides no guarantees about the behavior of poll after a future has completed.

Callers who may call poll too many times may want to consider using the fuse adaptor which defines the behavior of poll, but comes with a little bit of extra cost.

Implementations on Foreign Types§

Source§

impl<'a, F> Future for &'a mut F
where F: Future + ?Sized,

Source§

type Item = <F as Future>::Item

Source§

type Error = <F as Future>::Error

Source§

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<<&'a mut F as Future>::Item>, <&'a mut F as Future>::Error>

Source§

impl<A, E, F> Future for Recover<A, E, F>
where A: Future, F: FnOnce(<A as Future>::Error) -> <A as Future>::Item,

Source§

type Item = <A as Future>::Item

Source§

type Error = E

Source§

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<<A as Future>::Item>, E>

Source§

impl<A, F> Future for InspectErr<A, F>
where A: Future, F: FnOnce(&<A as Future>::Error),

Source§

type Item = <A as Future>::Item

Source§

type Error = <A as Future>::Error

Source§

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<<A as Future>::Item>, <A as Future>::Error>

Source§

impl<F> Future for Box<F>
where F: Future + ?Sized,

Source§

type Item = <F as Future>::Item

Source§

type Error = <F as Future>::Error

Source§

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<<Box<F> as Future>::Item>, <Box<F> as Future>::Error>

Source§

impl<F> Future for AssertUnwindSafe<F>
where F: Future,

Source§

type Item = <F as Future>::Item

Source§

type Error = <F as Future>::Error

Source§

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<<F as Future>::Item>, <F as Future>::Error>

Source§

impl<F, E> Future for WithExecutor<F, E>
where F: Future, E: Executor,

Source§

type Item = <F as Future>::Item

Source§

type Error = <F as Future>::Error

Source§

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<<F as Future>::Item>, <F as Future>::Error>

Source§

impl<S, U, F> Future for ForEachConcurrent<S, U, F>
where S: Stream, F: FnMut(<S as Stream>::Item) -> U, U: IntoFuture<Item = (), Error = <S as Stream>::Error>,

Source§

type Item = S

Source§

type Error = <S as Stream>::Error

Source§

fn poll( &mut self, cx: &mut Context<'_>, ) -> Result<Async<S>, <S as Stream>::Error>

Implementors§

Source§

impl Future for Never

Source§

impl<A> Future for futures::io::Close<A>
where A: AsyncWrite,

Source§

impl<A> Future for futures::io::Flush<A>
where A: AsyncWrite,

Source§

impl<A> Future for ReadToEnd<A>
where A: AsyncRead,

Source§

impl<A> Future for Flatten<A>
where A: Future, <A as Future>::Item: IntoFuture, <<A as Future>::Item as IntoFuture>::Error: From<<A as Future>::Error>,

Source§

type Item = <<A as Future>::Item as IntoFuture>::Item

Source§

type Error = <<A as Future>::Item as IntoFuture>::Error

Source§

impl<A> Future for Fuse<A>
where A: Future,

Source§

type Item = <A as Future>::Item

Source§

type Error = <A as Future>::Error

Source§

impl<A> Future for SelectAll<A>
where A: Future,

Source§

type Item = (<A as Future>::Item, usize, Vec<A>)

Source§

type Error = (<A as Future>::Error, usize, Vec<A>)

Source§

impl<A> Future for SelectOk<A>
where A: Future,

Source§

type Item = (<A as Future>::Item, Vec<A>)

Source§

type Error = <A as Future>::Error

Source§

impl<A, B> Future for Either<A, B>
where A: Future, B: Future<Item = <A as Future>::Item, Error = <A as Future>::Error>,

Source§

type Item = <A as Future>::Item

Source§

type Error = <A as Future>::Error

Source§

impl<A, B> Future for Join<A, B>
where A: Future, B: Future<Error = <A as Future>::Error>,

Source§

type Item = (<A as Future>::Item, <B as Future>::Item)

Source§

type Error = <A as Future>::Error

Source§

impl<A, B> Future for Select<A, B>
where A: Future, B: Future,

Source§

type Item = Either<(<A as Future>::Item, B), (<B as Future>::Item, A)>

Source§

type Error = Either<(<A as Future>::Error, B), (<B as Future>::Error, A)>

Source§

impl<A, B, C> Future for Join3<A, B, C>
where A: Future, B: Future<Error = <A as Future>::Error>, C: Future<Error = <A as Future>::Error>,

Source§

type Item = (<A as Future>::Item, <B as Future>::Item, <C as Future>::Item)

Source§

type Error = <A as Future>::Error

Source§

impl<A, B, C, D> Future for Join4<A, B, C, D>
where A: Future, B: Future<Error = <A as Future>::Error>, C: Future<Error = <A as Future>::Error>, D: Future<Error = <A as Future>::Error>,

Source§

type Item = (<A as Future>::Item, <B as Future>::Item, <C as Future>::Item, <D as Future>::Item)

Source§

type Error = <A as Future>::Error

Source§

impl<A, B, C, D, E> Future for Join5<A, B, C, D, E>
where A: Future, B: Future<Error = <A as Future>::Error>, C: Future<Error = <A as Future>::Error>, D: Future<Error = <A as Future>::Error>, E: Future<Error = <A as Future>::Error>,

Source§

type Item = (<A as Future>::Item, <B as Future>::Item, <C as Future>::Item, <D as Future>::Item, <E as Future>::Item)

Source§

type Error = <A as Future>::Error

Source§

impl<A, B, F> Future for AndThen<A, B, F>
where A: Future, B: IntoFuture<Error = <A as Future>::Error>, F: FnOnce(<A as Future>::Item) -> B,

Source§

impl<A, B, F> Future for OrElse<A, B, F>
where A: Future, B: IntoFuture<Item = <A as Future>::Item>, F: FnOnce(<A as Future>::Error) -> B,

Source§

impl<A, B, F> Future for Then<A, B, F>
where A: Future, B: IntoFuture, F: FnOnce(Result<<A as Future>::Item, <A as Future>::Error>) -> B,

Source§

impl<A, E> Future for ErrInto<A, E>
where A: Future, <A as Future>::Error: Into<E>,

Source§

type Item = <A as Future>::Item

Source§

type Error = E

Source§

impl<A, F> Future for Inspect<A, F>
where A: Future, F: FnOnce(&<A as Future>::Item),

Source§

type Item = <A as Future>::Item

Source§

type Error = <A as Future>::Error

Source§

impl<A, T> Future for ReadExact<A, T>
where A: AsyncRead, T: AsMut<[u8]>,

Source§

impl<A, T> Future for WriteAll<A, T>
where A: AsyncWrite, T: AsRef<[u8]>,

Source§

impl<F> Future for Spawn<F>
where F: Future<Item = (), Error = Never> + Send + 'static,

Source§

impl<F> Future for SpawnWithHandle<F>
where F: Future + Send + 'static, <F as Future>::Item: Send, <F as Future>::Error: Send,

Source§

impl<F> Future for CatchUnwind<F>
where F: Future + UnwindSafe,

Source§

type Item = Result<<F as Future>::Item, <F as Future>::Error>

Source§

type Error = Box<dyn Any + Send>

Source§

impl<F> Future for JoinAll<F>
where F: Future,

Source§

type Item = Vec<<F as Future>::Item>

Source§

type Error = <F as Future>::Error

Source§

impl<F> Future for Shared<F>
where F: Future,

Source§

impl<F, T, E> Future for FutureOption<F>
where F: Future<Item = T, Error = E>,

Source§

impl<R, F> Future for Lazy<R, F>
where F: FnOnce(&mut Context<'_>) -> R, R: IntoFuture,

Source§

impl<R, T> Future for Read<R, T>
where R: AsyncRead, T: AsMut<[u8]>,

Source§

type Item = (R, T, usize)

Source§

type Error = Error

Source§

impl<R, W> Future for CopyInto<R, W>
where R: AsyncRead, W: AsyncWrite,

Source§

type Item = (u64, R, W)

Source§

type Error = Error

Source§

impl<S> Future for futures::sink::Close<S>
where S: Sink,

Source§

type Item = S

Source§

type Error = <S as Sink>::SinkError

Source§

impl<S> Future for futures::sink::Flush<S>
where S: Sink,

Source§

type Item = S

Source§

type Error = <S as Sink>::SinkError

Source§

impl<S> Future for Send<S>
where S: Sink,

Source§

type Item = S

Source§

type Error = <S as Sink>::SinkError

Source§

impl<S> Future for Concat<S>
where S: Stream, <S as Stream>::Item: Extend<<<S as Stream>::Item as IntoIterator>::Item> + IntoIterator + Default,

Source§

type Item = <S as Stream>::Item

Source§

type Error = <S as Stream>::Error

Source§

impl<S> Future for StreamFuture<S>
where S: Stream,

Source§

type Item = (Option<<S as Stream>::Item>, S)

Source§

type Error = (<S as Stream>::Error, S)

Source§

impl<S, C> Future for Collect<S, C>
where S: Stream, C: Default + Extend<<S as Stream>::Item>,

Source§

type Item = C

Source§

type Error = <S as Stream>::Error

Source§

impl<S, Fut, T, F> Future for Fold<S, Fut, T, F>
where S: Stream, F: FnMut(T, <S as Stream>::Item) -> Fut, Fut: IntoFuture<Item = T, Error = <S as Stream>::Error>,

Source§

type Item = T

Source§

type Error = <S as Stream>::Error

Source§

impl<S, T, A, F> Future for LoopFn<A, F>
where F: FnMut(S) -> A, A: IntoFuture<Item = Loop<T, S>>,

Source§

impl<S, U, F> Future for ForEach<S, U, F>
where S: Stream, F: FnMut(<S as Stream>::Item) -> U, U: IntoFuture<Item = (), Error = <S as Stream>::Error>,

Source§

type Item = S

Source§

type Error = <S as Stream>::Error

Source§

impl<T> Future for Receiver<T>

Source§

impl<T, E> Future for JoinHandle<T, E>
where T: Send + 'static, E: Send + 'static,

Source§

type Item = T

Source§

type Error = E

Source§

impl<T, E> Future for Empty<T, E>

Source§

type Item = T

Source§

type Error = E

Source§

impl<T, E> Future for FutureResult<T, E>

Source§

type Item = T

Source§

type Error = E

Source§

impl<T, E, F> Future for PollFn<F>
where F: FnMut(&mut Context<'_>) -> Result<Async<T>, E>,

Source§

type Item = T

Source§

type Error = E

Source§

impl<T, U> Future for SendAll<T, U>
where T: Sink, U: Stream<Item = <T as Sink>::SinkItem>, <T as Sink>::SinkError: From<<U as Stream>::Error>,

Source§

impl<T, U> Future for Forward<T, U>
where U: Sink<SinkItem = <T as Stream>::Item>, T: Stream, <T as Stream>::Error: From<<U as Sink>::SinkError>,

Source§

impl<U, A, F> Future for Map<A, F>
where A: Future, F: FnOnce(<A as Future>::Item) -> U,

Source§

type Item = U

Source§

type Error = <A as Future>::Error

Source§

impl<U, A, F> Future for MapErr<A, F>
where A: Future, F: FnOnce(<A as Future>::Error) -> U,

Source§

type Item = <A as Future>::Item

Source§

type Error = U