Niko Matsakis
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: "RPITIT is ready, async is not (but it's getting there)" tags: T-lang, blog-post date: 2023-09-21 url: https://hackmd.io/xy3aQ8XHTJePmjcky0WC_w --- # New version: https://hackmd.io/0LAdFZ07Rf2oCxGDC79Nxw --- # Impl trait in traits is ready, async fn is not (but it's getting close) We here at wg-async are super excited to announce major progress towards our goal of enabling the use of `async fn` in traits. The next Rust release will include stable support for `-> impl Trait` notation in traits. It will also include *limited* support for `async fn`. Async functions are still missing some crucial features that most users need. Those features are our next priority. In the meantime, if you use async fn in traits, you'll get a lint warning to make sure you are aware of the limitations. ## Returning `impl Trait` Ever since [RFC #1522], Rust has allowed users to write `impl Trait` as the return type of (some) functions. This means that the function returns "some type that implements `Trait`". This is commonly used to return iterators, closures, or other types that are complex or even impossible to write explicitly: [RFC #1522]: https://rust-lang.github.io/rfcs/1522-conservative-impl-trait.html ```rust /// Given a list of players, return an iterator /// over their names. fn player_names( players: &[Player] ) -> impl Iterator<Item = &String> { players .iter() .map(|p| &p.name) } ``` Starting in Rust 1.XX, you can now use return-position `impl Trait` in traits and trait impls. For example, you could use this to write a trait that returns an item: ```rust trait Container { fn items(&self, url: Url) -> impl Iterator<Item = Widget>; } ``` Implementing the trait works how you might expect: ```rust impl Container for MyContainer { fn items(&self, url: Url) -> impl Iterator<Item = Widget> { self.items.iter().cloned() } } ``` <!--tmandry note: I want the post to get to the point of "what do I do" quickly, so I don't think showing the desugaring here helps.--> <!-- Under the hood, a return position `impl Trait` in a trait desugars to an anonymous [generic associated type][gat]: [gat]: XXX ```rust trait Container { type $items<'a>: Iterator<Item = Widget>; fn items(&self, url: Url) -> Self::$items<'a>; } impl Container for MyContainer { type $items<'a> = /* inferred by the compiler */; fn items(&self, url: Url) -> Self::$items<'a> { self.items.iter().cloned() } } ``` --> ## Async fn in traits work, but aren't ready for prime time So what does all of this have to do with async functions? Well, async functions are "just sugar" for a function that returns `-> impl Future`. So, since that is now permitted in traits, we also permit you to write traits that use `async fn`... ```rust trait HttpService { // Desugars to: // fn fetch(&self, url: Url) -> impl Future<Output = HtmlBody> async fn fetch(&self, url: Url) -> HtmlBody; } ``` ...but if you do that, you'll find that you get a warning: ``` XXX ``` The error message is asking you to make a choice: **Do you want your trait to work primarily with multithreaded work-stealing executors?** We expect that for a majority of users today, the answer will be yes. If you do, rewrite the signature using `impl Future + Send` . If you only expect your trait to be used on single-threaded executors, you can elide the `+ Send` bound or silence the warning with `#[allow]`. ```rust trait HttpService { fn fetch(&self, url: Url) -> impl Future<Output = HtmlBody> + Send; } ``` If you want your trait to be generic over both, the only way *today* is to make two copies of your trait: One with `+ Send` bounds and one without. We've published a proc macro to make all of this easier: ```rust #[make_variant(HttpService: Send)] trait LocalHttpService { async fn fetch(&self, url: Url) -> HtmlBody; } ``` This creates two traits, `HttpService` and `SendHttpService`, one for each use case. Our hope is that in the future, `HttpService` will be the only trait people need, with `SendHttpService` being an additional convenience for users. Read on for a more thorough explanation of the problem. <!--tmandry: is there an earlier post we can link to?--> ## The "send bound" problem Remember earlier that we talked about how impl Traits desugar to an anonymous associated type? Because that associated type is *anonymous*, it means that you can't name it in where-clauses, which in turn means that you cannot (yet) place additional constraints on it. For many traits, this isn't a problem. But for async functions in particular, it can become a problem very quickly when you are using work-stealing executors (which is the most common configuration in practice). This is because work-stealing executors require not just a `Future` but a `Future` that is also `Send`. Because this is so common, we call the general problem of not being able to write bounds on the associated type the "send bound problem". Let's explain the send bound problem with an example. Imagine that we are using [tokio][] and we wish to write some code that accepts any `HttpService` and spawns a new task using it: [tokio]: https://tokio.rs ```rust fn spawn_task<H: HttpService>(h: H) { tokio::spawn(async move { ... let body = h.fetch(some_url).await; // ERROR ... }); } ``` This code will not compile: ``` XXX ``` The problem here is that, when we call `h.fetch`, we get back an `impl Future` -- but tokio requires a `impl Future + Send`, and so the compiler reports an error. Interestingly, this problem only occurs in a *generic* context -- if we call the `HttpService` methods with a known type, everything works fine: ```rust struct MyService { ... } impl HttpService for MyService { ... } fn spawn_task_not_generic(h: MyService) { tokio::spawn(async move { ... let body = h.fetch(some_url).await; // OK ... }); } ``` Why is this? It's because in this case, the compiler knows *precisely* which impl you are using, and so it can check that the actual future is `Send`; in contrast, in the generic case, the compiler has to use the bounds declared in the trait, which don't include `Send`. What we would *like* to have is some way to write a where-clause that specifies that you need not only an `HttpServer` but an `HttpServer` that returns a `Send` future. There are several proposals for how we might achieve this, but we don't yet have consensus on which is best. **The "send bound problem" is the main reason that we don't believe async functions in traits are ready for prime time.** Now that the core stabilization is complete, we are planning to focus on selecting, RFC'ing, and stabilizing a solution for "send bounds" ASAP. ## The other big limitation: dynamic dispatch ## The fine print For those who really want to know the nitty gritty, there are a bunch of other interesting deatils that we worked through to reach this point. ### Mixing async fn and impl Trait ### Revealing and semver When you use `-> impl Trait` ### Impl trait capture rules in traits vs elsewhere We've also improved on the capture rules for `-> impl Trait` in traits. In inherent impls you might often write something like this: ```rust! impl Foo { fn foo(&self) -> impl Future<Output = i32> + '_; } ``` That `+ '_` says that the returned future captures a reference to `self`. This can be a nuisance on its own, but it gets [really complicated][capture-rules] when there are multiple lifetimes involved. [capture-rules]: https://hackmd.io/sFaSIMJOQcuwCdnUvCxtuQ?view For _return-position `impl Trait` in traits_ we've adopted updated capture rules that we hope to roll out to language more broadly in the next edition. This means `+ '_` is **not** required in traits or trait impls. ## Recommendations ### When *should* you use return-position impl Trait in traits? ### When *should* you use async fn in traits? ## Conclusion: what's to come? This next release marks a big milestone, but it's not the end of the road. Now that the rudiments are stabilized, we are turning our focus to solving the send bound problem and, after that, native support for dynamic dispatch. Once that is resolved, we will remove the warning on the use of async fn, because we feel it is ready for widespread use. Of course, we're never satisfied, and [send bound problem]: https://smallcultfollowing.com/babysteps/blog/2023/02/01/async-trait-send-bounds-part-1-intro/

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully