1
- ` async fn ` s are not yet supported in traits in Rust.
1
+ ` async fn ` s are not yet supported in traits in Rust.
2
2
3
3
Erroneous code example:
4
4
@@ -10,7 +10,8 @@ trait T {
10
10
}
11
11
```
12
12
13
- ` async fn ` s return an ` impl Future ` , making the following two examples equivalent:
13
+ ` async fn ` s return an ` impl Future ` , making the following two examples
14
+ equivalent:
14
15
15
16
``` edition2018,ignore (example-of-desugaring-equivalence)
16
17
async fn foo() -> User {
@@ -23,8 +24,8 @@ fn foo(&self) -> impl Future<Output = User> + '_ {
23
24
```
24
25
25
26
But when it comes to supporting this in traits, there are [ a few implementation
26
- issues] [ async-is-hard ] . One of them is returning ` impl Trait ` in traits is not supported,
27
- as it would require [ Generic Associated Types] to be supported:
27
+ issues] [ async-is-hard ] . One of them is returning ` impl Trait ` in traits is not
28
+ supported, as it would require [ Generic Associated Types] to be supported:
28
29
29
30
``` edition2018,ignore (example-of-desugaring-equivalence)
30
31
impl MyDatabase {
@@ -40,13 +41,14 @@ impl MyDatabase {
40
41
}
41
42
```
42
43
43
- Until these issues are resolved, you can use the [ ` async-trait ` crate] , allowing you to use
44
- ` async fn ` in traits by desugaring to "boxed futures"
44
+ Until these issues are resolved, you can use the [ ` async-trait ` crate] , allowing
45
+ you to use ` async fn ` in traits by desugaring to "boxed futures"
45
46
(` Pin<Box<dyn Future + Send + 'async>> ` ).
46
47
47
- Note that using these trait methods will result in a heap allocation per-function-call. This is not
48
- a significant cost for the vast majority of applications, but should be considered when deciding
49
- whether to use this functionality in the public API of a low-level function that is expected to be
48
+ Note that using these trait methods will result in a heap allocation
49
+ per-function-call. This is not a significant cost for the vast majority of
50
+ applications, but should be considered when deciding whether to use this
51
+ functionality in the public API of a low-level function that is expected to be
50
52
called millions of times a second.
51
53
52
54
You might be interested in visiting the [ async book] for further information.
0 commit comments