@@ -4,22 +4,20 @@ minutes: 5
44
55# Async Traits
66
7- Async methods in traits are were stabilized only recently, in the 1.75 release.
8- This required support for using return-position ` impl Trait ` (RPIT) in traits,
9- as the desugaring for ` async fn ` includes ` -> impl Future<Output = ...> ` .
7+ Async methods in traits are were stabilized in the 1.75 release. This required
8+ support for using return-position ` impl Trait ` in traits, as the desugaring for
9+ ` async fn ` includes ` -> impl Future<Output = ...> ` .
1010
11- However, even with the native support today there are some pitfalls around
12- ` async fn ` and RPIT in traits :
11+ However, even with the native support, there are some pitfalls around
12+ ` async fn ` :
1313
14- - Return-position impl Trait captures all in-scope lifetimes (so some patterns
15- of borrowing cannot be expressed)
14+ - Return-position ` impl Trait ` captures all in-scope lifetimes (so some patterns
15+ of borrowing cannot be expressed).
1616
17- - Traits whose methods use return-position ` impl trait ` or ` async ` are not ` dyn `
18- compatible.
17+ - Async traits cannot be used with [ trait objects] (` dyn Trait ` support).
1918
20- If we do need ` dyn ` support, the crate
21- [ async_trait] ( https://docs.rs/async-trait/latest/async_trait/ ) provides a
22- workaround through a macro, with some caveats:
19+ The [ async_trait] crate provides a workaround for ` dyn ` support through a macro,
20+ with some caveats:
2321
2422``` rust,editable,compile_fail
2523use async_trait::async_trait;
@@ -47,11 +45,11 @@ async fn run_all_sleepers_multiple_times(
4745 n_times: usize,
4846) {
4947 for _ in 0..n_times {
50- println!("running all sleepers..");
48+ println!("Running all sleepers. ..");
5149 for sleeper in &sleepers {
5250 let start = Instant::now();
5351 sleeper.sleep().await;
54- println!("slept for {}ms", start.elapsed().as_millis());
52+ println!("Slept for {} ms", start.elapsed().as_millis());
5553 }
5654 }
5755}
@@ -71,13 +69,21 @@ async fn main() {
7169- ` async_trait ` is easy to use, but note that it's using heap allocations to
7270 achieve this. This heap allocation has performance overhead.
7371
74- - The challenges in language support for ` async trait ` are deep Rust and
75- probably not worth describing in-depth. Niko Matsakis did a good job of
76- explaining them in
77- [ this post] ( https://smallcultfollowing.com/babysteps/blog/2019/10/26/async-fn-in-traits-are-hard/ )
78- if you are interested in digging deeper.
72+ - The challenges in language support for ` async trait ` are too deep to describe
73+ in-depth in this class. See [ this blog post] by Niko Matsakis if you are
74+ interested in digging deeper. See also these keywords:
75+
76+ - [ RPIT] : short for
77+ [ return-position ` impl Trait ` ] ( ../../generics/impl-trait.md ) .
78+ - [ RPITIT] : short for return-position ` impl Trait ` in trait (RPIT in trait).
7979
8080- Try creating a new sleeper struct that will sleep for a random amount of time
81- and adding it to the Vec.
81+ and adding it to the ` Vec ` .
8282
8383</details >
84+
85+ [ async_trait ] : https://docs.rs/async-trait/
86+ [ trait objects ] : ../../smart-pointers/trait-objects.md
87+ [ this blog post ] : https://smallcultfollowing.com/babysteps/blog/2019/10/26/async-fn-in-traits-are-hard/
88+ [ RPIT ] : https://doc.rust-lang.org/reference/types/impl-trait.html#abstract-return-types
89+ [ RPITIT ] : https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html
0 commit comments