How `#[async_trait]` works internally
masterThe #[async_trait] macro transforms async fn methods into regular functions that return a pinned, boxed, thread-safe future: Pin<Box<dyn Future + Send + 'async_trait>>.
This transformation allows the trait to be used with dynamic dispatch (dyn Trait), as the return type is now a concrete, sized type (the Pin<Box<...>> wrapper) rather than an opaque, compiler-generated future type.
// Example of how the macro expands an implementation:
impl Advertisement for AutoplayingVideo {
fn run<'async_trait>(
&'async_trait self,
) -> Pin<Box<dyn std::future::Future<Output = ()> + Send + 'async_trait>>
where
Self: Sync + 'async_trait,
{
Box::pin(async move {
/* the original method body */
})
}
}