liveBook · Manning

Poll

As explained in Rust Servers, Services, and Apps

Poll is a Rust result type that indicates whether a polled asynchronous computation is ready with a value or still pending.

`Poll<T>` is the result of calling `Future::poll`: `Poll::Pending` means the future cannot produce a value yet; `Poll::Ready(value)` carries its completed output. The Tokio executor calls `poll` to drive a future toward completion. section 10.3 section 10.4

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

The `Future` trait signature shows where `Poll<Self::Output>` fits: `poll` receives a pinned future and a task context, then reports pending or ready. section 10.3

A custom future must connect its result to a wakeup:

  • Return `Poll::Pending` when the output is unavailable. Register or retain a waker so the task can be polled again when that changes. section 10.4
  • Return `Poll::Ready(value)` once the output is available. Waking on every pending poll without making progress instead causes repeated polling. section 10.4

Expiration timer

An `AsyncTimer` future stores an expiration time. Its `poll` returns `Poll::Pending` while the deadline is ahead, arranging for a sleeping thread to wake the task. After that wakeup, the executor polls again; the expired timer returns `Poll::Ready` with a `String`. Other tasks can run while the timer is pending. section 10.5

How Rust Servers, Services, and Apps covers it

How deep it goes
Works through the poll signature, both variants, wakeups, and a custom timer; defers detailed Pin mechanics.
Examples
Uses a custom future that stays pending, a repeated-wakeup experiment, and an expiration timer that becomes ready.
What it assumes
Assumes familiarity with Rust functions and Tokio tasks, introduces Future polling, and treats Pin's mechanics as deferred.
What you can do after
Build it from scratch by implementing a Future that returns Pending until its timer expires, then Ready with a String.
How it connects
Connects Future, Pin, Context, Waker, async/await, and the Tokio executor through the polling cycle.

Also explained in

Compare how 2 books explain poll →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.