From e362b44b664d476615ce6b2bd9866463954ddfb6 Mon Sep 17 00:00:00 2001 From: Frances Wingerter <91758128+fw-immunant@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:37:48 +0000 Subject: [PATCH] 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. --- src/async/pitfalls/async-traits.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/async/pitfalls/async-traits.md b/src/async/pitfalls/async-traits.md index 1b5d670a..a9f0b31a 100644 --- a/src/async/pitfalls/async-traits.md +++ b/src/async/pitfalls/async-traits.md @@ -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`. -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;