trait-concept

Native async trait methods

Rust 1.75 introduced async methods in traits. A private trait with generic dispatch can expose an async method directly. A dispatchable native async method prevents the trait from being dyn-compatible on the 1.85 baseline. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 8 Common mistake: Assuming native async methods are automatically callable on dyn Lookup.

Formal Syntax & Structure

rust syntaxrust
trait Lookup { async fn count(&self) -> u32; }

Working Example

Example Snippetrust
fn main() {    use std::{future::Future, task::{Context, Poll, Waker}};    fn ready_now<F: Future>(f: F) -> F::Output {        let mut f = std::pin::pin!(f);        let mut cx = Context::from_waker(Waker::noop());        match f.as_mut().poll(&mut cx) {            Poll::Ready(value) => value,            Poll::Pending => panic!("fixture expected immediate readiness"),        }    }    trait Lookup { async fn count(&self) -> u32; }    struct Memory(u32);    impl Lookup for Memory { async fn count(&self) -> u32 { self.0 } }    async fn get<T: Lookup>(source: &T) -> u32 { source.count().await }    println!("{}", ready_now(get(&Memory(8))));}
Rust Reference