Skip to content

Commit ed2eaa1

Browse files
Dav1ddeLms24
authored andcommitted
ref(develop): Update Rust async trait section (#11950)
1 parent 3e934db commit ed2eaa1

File tree

1 file changed

+11
-11
lines changed
  • develop-docs/engineering-practices

1 file changed

+11
-11
lines changed

develop-docs/engineering-practices/rust.mdx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@ During migration you may need normal functions which return futures, for their s
5050

5151
### Async Traits
5252

53-
In **traits** you can not yet use `async fn` ([see this blog post](https://smallcultfollowing.com/babysteps/blog/2019/10/26/async-fn-in-traits-are-hard/)).
54-
In this case, functions should return `-> Pin<Box<dyn Future<Output = ...> + Send>>`:
53+
Support for async in **traits** has [landed in Rust](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html)
54+
and should generally be preferred now.
5555

5656
```rust
57-
trait Database {
58-
fn get_user(&self) -> Pin<Box<dyn Future<Output = User> + Send + '_>>;
57+
pub trait Database {
58+
fn get_user(&self) -> impl Future<Output = User> + Send;
5959
}
6060

61-
impl Database for MyDB {
62-
fn get_user(&self) -> Pin<Box<dyn Future<Output = User> + Send + '_>> {
63-
Box::pin(async {
64-
// ...
65-
})
66-
}
61+
impl Database for MyDatabase {
62+
async fn get_user(&self) -> User {
63+
todo!()
64+
}
6765
}
6866
```
6967

7068
Note that the returned future type is `Send`, to ensure that it can run on a multi-threaded runtime.
7169

72-
This corresponds to what the [async-trait crate](https://crates.io/crates/async-trait) does.
70+
When you need dynamic dispatch or have to support Rust versions older than 1.75 consider using the
71+
[`async-trait`](https://docs.rs/async-trait/) crate.
72+
7373

7474
### Avoid `.unwrap()`
7575

0 commit comments

Comments
 (0)