Skip to content
CryoCryo home
Stdlibfuture

traits

import std::future::traits; · source

Future

type trait Future {
    type Output;
    poll(mut &this, cx: Context*) -> Poll<This::Output>;
}

The one trait the whole async machinery is defined against. poll is called with a Context carrying the task's Waker; it returns Poll::Pending after arranging to be woken, or Poll::Ready(v) once. Output is an associated type, and implement Future<T> for X is the positional sugar for binding it, exactly as with Iterator.

An async function produces a compiler-generated type that implements this trait; you implement it by hand only for a leaf future — one that talks to the reactor or the blocking pool directly. The contract a leaf must honour:

  • Attempt, then register. Try the operation first; only on "would block" register the waker and return Pending. The reactor arms level-triggered, so readiness that arrived between the two is still reported.
  • Own what you touch. A future is moved between polls, so it cannot hold a raw pointer into the caller's frame across a suspension. The socket futures in net take the handle and buffer by value and hand them back in their output for this reason.
  • Release on drop. A parked future's Drop must cancel its registration, because that drop is the only cancellation there is.
  • Do not poll after Ready. The library futures panic if you do; a completed future has already moved its output out.

Implementors

implement<A, B, OA, OB> trait Future<(OA, OB)> for struct Join<A, B, OA, OB>
where A: Future<OA>, B: Future<OB>   // std::future::combinator

implement<A, B, T1, T2, E> trait Future<Result<(T1, T2), E>> for struct TryJoin<A, B, T1, T2, E>
where A: Future<Result<T1, E>>, B: Future<Result<T2, E>>   // std::future::combinator

implement<A, B, OA, OB> trait Future<Selected<OA, OB>> for struct Select<A, B, OA, OB>
where A: Future<OA>, B: Future<OB>   // std::future::combinator

implement<F, O> trait Future<Result<O, Elapsed>> for struct Timeout<F, O>
where F: Future<O>   // std::future::combinator

implement<T> trait Future<T> for struct Ready<T>   // std::future::ready

implement<T> trait Future<T> for struct PendingThenReady<T>   // std::future::ready

implement trait Future for struct Sleep   // std::future::timer

implement trait Future for struct Resolve   // std::net::dns

implement trait Future for struct TcpRead   // std::net::socket::tcp

implement trait Future for struct TcpWrite   // std::net::socket::tcp

implement trait Future for struct TcpAccept   // std::net::socket::tcp

implement trait Future for struct TcpConnect   // std::net::socket::tcp

implement trait Future for struct UdpSendTo   // std::net::socket::udp

implement trait Future for struct UdpSend   // std::net::socket::udp

implement trait Future for struct UdpRecvFrom   // std::net::socket::udp

implement trait Future for struct UdpRecv   // std::net::socket::udp

implement trait Future for struct TlsHandshake   // std::net::tls::future

implement trait Future for struct TlsRead   // std::net::tls::future

implement trait Future for struct TlsWrite   // std::net::tls::future

implement trait Future for struct ChildWait   // std::process::child

implement trait Future for struct PipeDrain   // std::process::child