-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
ref(develop): Update Rust async trait section #11950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,26 +50,26 @@ During migration you may need normal functions which return futures, for their s | |
|
|
||
| ### Async Traits | ||
|
|
||
| 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/)). | ||
| In this case, functions should return `-> Pin<Box<dyn Future<Output = ...> + Send>>`: | ||
| Support for async in **traits** has [landed in Rust](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html) | ||
| and should generally be preferred now. | ||
|
|
||
| ```rust | ||
| trait Database { | ||
| fn get_user(&self) -> Pin<Box<dyn Future<Output = User> + Send + '_>>; | ||
| pub trait Database { | ||
| fn get_user(&self) -> impl Future<Output = User> + Send; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wanna add a callout here why we should be explicit with Personally, I'd add a recommendation to use associated types where possible (especially for Iterators!) as this is extremely useful for composition in the absence of generators, but for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I kept this message, hoping it would address this clearly enough:
This requires explicit associated types until this restriction is lifted. I think for most of Sentry's (application) code this would be overkill. I can add another note that explains that as well. |
||
| } | ||
|
|
||
| impl Database for MyDB { | ||
| fn get_user(&self) -> Pin<Box<dyn Future<Output = User> + Send + '_>> { | ||
| Box::pin(async { | ||
| // ... | ||
| }) | ||
| } | ||
| impl Database for MyDatabase { | ||
| async fn get_user(&self) -> User { | ||
| todo!() | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Note that the returned future type is `Send`, to ensure that it can run on a multi-threaded runtime. | ||
|
|
||
| This corresponds to what the [async-trait crate](https://crates.io/crates/async-trait) does. | ||
| When you need dynamic dispatch or have to support Rust versions older than 1.75 consider using the | ||
| [`async-trait`](https://docs.rs/async-trait/) crate. | ||
|
|
||
|
|
||
| ### Avoid `.unwrap()` | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.