Rust Async Working Group
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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: "Design meeting 2024-04-18: Async iteration/generation" tags: ["WG-async", "design-meeting", "minutes"] date: 2024-04-18 discussion: https://rust-lang.zulipchat.com/#narrow/stream/187312-wg-async/topic/Design.20meeting.202024-04-18 url: https://hackmd.io/Ed0DtZ2KRGuBa0wCjNet9g --- # Discussion ## Attendance - People: TC, eholk, Daria, Yosh ## Meeting roles - Minutes, driver: TC ## Last week TC: Our minutes from last week on this subject are here: https://hackmd.io/bYaPiCdqR3WIyjKhFWzdtQ ## Current state: Low level desugaring TC: On the basis of recent discussion, it seems we've reached agreement on the low-level desugaring. E.g.: ```rust pub trait AsyncGen { type Item; fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Option<Self::Item>; } pub trait IntoAsyncGen { type Item; type IntoAsyncGen: AsyncGen<Item = Self::Item>; fn into_async_gen(self) -> Self::IntoAsyncGen; } ``` Yosh: At the lowest level, I agree this is what we need to pull it forward. TC: That leaves these known open questions: - Whether or not to allow the traits to be named. - whether or not we stabilize the methods on `AsyncGen` or `IntoAsyncGen`. ## Reconsidering lowering question Yosh: Since last week Oli showed that the presented applies to Futures generally. So I'm reconsidering the lowering question. TC: That argument was: (Pasting arguments from Oli who is not in this call.) Oli: How would the for loop look with a simple initial desugaring to a `loop` and a `match` ```rust let iter =...; // unclear if needs to be an async operation let mut actual_iter = iter.into_async_foo(); loop { let output = actual_iter.something_next().await; // ???? how to get output? match output { Some(x) => { println!("{x}"); } None => break, } } ``` Minor issue: method call syntax is problematic due to being able to invoke random other things of the same name ```rust let iter =...; // unclear if needs to be an async operation let mut actual_iter = IntoAsyncFoo::into_async_foo(iter); loop { let output = AsyncFoo::something_next(&mut actual_iter).await; match output { Some(x) => { println!("{x}"); } None => break, } } ``` problem: HIR cannot support `await`. So we need to desugar the entire `for..await` block in one step: ```rust let iter =...; // unclear if needs to be an async operation let mut actual_iter = IntoAsyncFoo::into_async_foo(iter); loop { let output_future = AsyncFoo::something_next(&mut actual_iter); // can access `current_context` here, because we're in a HIR desugaring context // async desugaring copied from: https://doc.rust-lang.org/stable/reference/expressions/await-expr.html let output = match output_future { mut pinned => loop { let mut pin = unsafe { Pin::new_unchecked(&mut pinned) }; match Future::poll(Pin::borrow(&mut pin), &mut current_context) { Poll::Ready(r) => break r, Poll::Pending => yield Poll::Pending, } } }; match output { Some(x) => { println!("{x}"); } None => break, } } ``` needs dyn star, always has double references, optimizer has problems with large `output_future` and complex `Future::poll` methods (can't optimize out). The following is already fairly optimal by default, so the optimizer can concentrate on cleaning up the loop just like it does with sync for loops. ```rust let iter =...; // unclear if needs to be an async operation let mut actual_iter = IntoAsyncFoo::into_async_foo(iter); loop { // can access `current_context` here, because we're in a HIR desugaring context // async desugaring copied from: https://doc.rust-lang.org/stable/reference/expressions/await-expr.html let output = match output_future { mut pinned => loop { let mut pin = unsafe { Pin::new_unchecked(&mut actual_iter) }; match AsyncFoo::poll_next(Pin::borrow(&mut pin), &mut current_context) { Poll::Ready(r) => break r, Poll::Pending => yield Poll::Pending, } } }; match output { Some(x) => { println!("{x}"); } None => break, } } ``` eholk: The compiler struggles to optimize futures, and that's just true. It's a compiler limitation. You could imagine adding some heuristics or special optimizations that try to do better in these cases. Yosh: It feels like since this is already a problem, we'd not be adding any new problems. It'd just take time and effort. eholk: It's possible that putting more weight on this problem would cause it to solve it sooner. Yosh: Oli's estimate was someone full time for two years. eholk: Oli's estimate seems in the right ballpark to me. Yosh: Apparently pnkfelix tried awhile back and decided this was hard. TC: There's always risk in betting on our ability to write a sufficiently smart compiler. Any estimate on the scale of two years of full time work has the potential for many hidden landmines. If we were so certain on how to do this as to be sure there are no landmines, then the estimate would probably be a lot shorter than two years. Yosh: I think the right call here is to ask Oli to clarify; I don't think we should pessimize what he said either. Yosh: Two things I'm unsure of: 1. Should the lack of optimization of futures in the general sense dictate the shape of what our generator blocks desugar into forever after? If this is a shared problem, do we expect to eventually resolve this? If we do not, is this worse than what is the norm for all other async functions? 2. What happens when you go from AFIT `async fn next` to a `dyn AsyncIterator`. Can we escape the double deref / double indirect call? Yosh: These are the only two convincing arguments in favor of `poll_next`. ## Meta (We had a long meta discussion related to how to make this issue less stressful for everyone. We agreed to repurpose the design meeting slot next week for an open discussion.)

    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