Skip to content

Add support for for await loops#118847

Merged
bors merged 3 commits intorust-lang:masterfrom
eholk:for-await
Dec 22, 2023
Merged

Add support for for await loops#118847
bors merged 3 commits intorust-lang:masterfrom
eholk:for-await

Conversation

@eholk
Copy link
Contributor

@eholk eholk commented Dec 12, 2023

This adds support for for await loops. This includes parsing, desugaring in AST->HIR lowering, and adding some support functions to the library.

Given a loop like:

for await i in iter {
    ...
}

this is desugared to something like:

let mut iter = iter.into_async_iter();
while let Some(i) = loop {
    match core::pin::Pin::new(&mut iter).poll_next(cx) {
        Poll::Ready(i) => break i,
        Poll::Pending => yield,
    }
} {
    ...
}

This PR also adds a basic IntoAsyncIterator trait. This is partly for symmetry with the way Iterator and IntoIterator work. The other reason is that for async iterators it's helpful to have a place apart from the data structure being iterated over to store state. IntoAsyncIterator gives us a good place to do this.

I've gated this feature behind async_for_loop and opened #118898 as the feature tracking issue.

r? @compiler-errors

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. F-async_for_loop `#![feature(async_for_loop)]` merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. WG-async Working group: Async & await

Projects

None yet

Development

Successfully merging this pull request may close these issues.