Async trait boundaries
Quick answer
An async method in a trait lets implementations provide asynchronous behavior behind a shared interface. The returned future's bounds are part of the interface design.
Choose static or dynamic dispatch
Native async trait methods are available on the baseline, but traits with dispatchable async methods are not dyn-compatible. For static dispatch, use a generic bound. If callers need Send futures, an explicit fn ... -> impl Future<Output = T> + Send can express that contract. For dynamic dispatch, an explicitly boxed future with an appropriate lifetime is one option; boxing and virtual dispatch have costs.
use std::{future::Future, task::{Context, Poll, Waker}};trait Source { async fn value(&self) -> u32; }struct Fixed;impl Source for Fixed { async fn value(&self) -> u32 { 12 } }async fn read<S: Source>(s: &S) -> u32 { s.value().await }fn main() { let s = Fixed; let mut f = std::pin::pin!(read(&s)); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(12)); println!("12");}Expected stdout (not executed):
12The generic caller uses the concrete implementation.
use std::{future::Future, pin::Pin, task::{Context, Poll, Waker}};trait Source { fn value(&self) -> Pin<Box<dyn Future<Output=u32> + '_>>; }struct Fixed;impl Source for Fixed { fn value(&self) -> Pin<Box<dyn Future<Output=u32> + '_>> { Box::pin(async { 12 }) }}fn main() { let s: Box<dyn Source> = Box::new(Fixed); let mut f = s.value(); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(12)); println!("12");}Expected stdout (not executed):
12The interface erases the future type but retains the borrow lifetime.
Common mistake
Do not assume an async trait method automatically promises a Send future.
Try it yourself
Add Send to the boxed future's bounds and explain which implementations might be rejected.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Async trait boundaries — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A trait with a dispatchable native async method is needed behind dyn. What issue must be addressed?
Async trait boundaries — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{future::Future, task::{Context, Poll, Waker}}; trait Source { async fn value(&self) -> u32; } struct Fixed; impl Source for Fixed { async fn value(&self) -> u32 { 12 } } async fn read<S: Source>(s: &S) -> u32 { s.value().await } fn main() { let s = Fixed; let mut f = std::pin::pin!(read(&s)); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(12)); println!("12"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Async trait boundaries — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{future::Future, pin::Pin, task::{Context, Poll, Waker}}; trait Source { fn value(&self) -> Pin<Box<dyn Future<Output=u32> + '_>>; } struct Fixed; impl Source for Fixed { fn value(&self) -> Pin<Box<dyn Future<Output=u32> + '_>> { Box::pin(async { 12 }) } } fn main() { let s: Box<dyn Source> = Box::new(Fixed); let mut f = s.value(); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(12)); println!("12"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.