liveBook · Manning

Poll

As explained in Rust Web Development

Future::poll is the trait method a runtime calls to advance a future's computation.

Future::poll advances a future’s computation and reports whether it is still pending or ready. A runtime’s executor calls it; creating a future alone does not start its work. section 1.3 section 2.2.4

A future is polled as its work progresses:

  • The executor calls poll to check whether the future is ready. section 2.2.4
  • The result is Pending while work remains; Ready carries the future’s output, which may be an error. section 2.2.2
  • For network work, the runtime can use Mio and the kernel; when the socket signals completion, a reactor wakes the executor to continue polling. section 2.2.5

An awaited HTTP request

In an async function, awaiting an HTTP request hands its future to the runtime. The executor polls it; after network work completes, the result becomes available to the awaiting code. section 1.3 section 2.2.5

Listing 2.23 Rust’s Future trait
// Docs: https://doc.rust-lang.org/std/future/trait.Future.html
// Source code: 
// https:/ /doc.rust-lang.org/src/core/future/future.rs.html#36-104

pub trait Future {
   type Output;
   fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) 
       -> Poll<Self::Output>;
}

The Future trait signature shows poll’s inputs and its Poll<Self::Output> return type. section 2.2.4

How Rust Web Development covers it

How deep it goes
Explains Pending and Ready outcomes and the executor’s role, while deferring the Future type’s full internals.
Examples
Uses an awaited HTTP request to illustrate executor polling and network completion.
What it assumes
Assumes familiarity with async functions and await, while briefly defining Future and its readiness states.
What you can do after
Understand how an executor polls a future and how network completion resumes its computation.
How it connects
Connects Future and async/await to runtimes, executors, reactors, and Mio.

Also explained in

Compare how 3 books explain poll →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Web Development. The section links point to the parts of the book it draws on.