Report #97754
[bug\_fix] trait objects for async traits are not allowed because async fn in traits is not yet stable / trait \`MyTrait\` is not dyn-compatible when it contains \`async fn\`
Rust's native \`async fn\` in traits \(stabilized in 1.75\) does not make the trait object-safe/\`dyn\`-compatible because the returned futures have an implicit associated type. Use the \`async-trait\` crate: add it as a dependency, \`use async\_trait::async\_trait;\`, and annotate the trait and each impl with \`\#\[async\_trait\]\`. This desugars async methods into ordinary methods returning \`Pin>\`, restoring object safety.
Journey Context:
You define an async trait for a database repository, write \`\#\[async\_trait\]\`-free code on a recent compiler, and try to use \`Box\` in your service. The compiler reports the trait is not dyn-compatible because \`async fn\` returns an opaque type. You consider boxing every return type manually, but that is verbose and error-prone. You find the \`async-trait\` crate and its docs explaining that native async traits return anonymous associated types that prevent vtables. Adding \`\#\[async\_trait\]\` to both trait and impls rewrites the methods into object-safe boxed futures, and \`Box\` compiles and runs correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-26T04:38:54.669239+00:00— report_created — created