1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-19 22:19:29 +02:00

concurrency: Update async trait slide (#1883)

Clarify that the basics are stable, but dyn support is still missing.
This slide was outdated and didn't explain the current state of stable
correctly.
This commit is contained in:
Frances Wingerter 2024-03-06 21:37:48 +00:00 committed by GitHub
parent 42f6e1186e
commit e362b44b66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,10 +1,21 @@
# Async Traits
Async methods in traits are not yet supported in the stable channel
([An experimental feature exists in nightly and should be stabilized in the mid term.](https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html))
Async methods in traits are were stabilized only recently, in the 1.75 release.
This required support for using return-position `impl Trait` (RPIT) in traits,
as the desugaring for `async fn` includes `-> impl Future<Output = ...>`.
The crate [async_trait](https://docs.rs/async-trait/latest/async_trait/)
provides a workaround through a macro:
However, even with the native support today there are some pitfalls around
`async fn` and RPIT in traits:
- Return-position impl Trait captures all in-scope lifetimes (so some patterns
of borrowing cannot be expressed)
- Traits whose methods use return-position `impl trait` or `async` are not `dyn`
compatible.
If we do need `dyn` support, the crate
[async_trait](https://docs.rs/async-trait/latest/async_trait/) provides a
workaround through a macro, with some caveats:
```rust,editable,compile_fail
use async_trait::async_trait;