trait
AsyncFn: shared async callback bound
Use the stable parenthesized trait bound when a callback can be called through shared access and its future may borrow that access. On the baseline, direct calls to the trait’s low-level async_call method are unstable. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 13 Common mistake: Using unstable associated types or requiring dyn AsyncFn on Rust 1.85.
Formal Syntax & Structure
rust syntaxrust
F: AsyncFn(u32) -> u32Working 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"), } } use std::ops::AsyncFn; async fn twice<F: AsyncFn(u32) -> u32>(f: F) -> u32 { f(2).await + f(3).await } let square = async |x: u32| x*x; println!("{}", ready_now(twice(square)));}