Skip to content

Commit 09514f4

Browse files
committed
Auto merge of rust-lang#118847 - eholk:for-await, r=compiler-errors
Add support for `for await` loops 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: ```rust for await i in iter { ... } ``` this is desugared to something like: ```rust 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 rust-lang#118898 as the feature tracking issue. r? `@compiler-errors`
2 parents e6de110 + a501599 commit 09514f4

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

core/src/async_iter/async_iter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub trait AsyncIterator {
4747
/// Rust's usual rules apply: calls must never cause undefined behavior
4848
/// (memory corruption, incorrect use of `unsafe` functions, or the like),
4949
/// regardless of the async iterator's state.
50+
#[cfg_attr(not(bootstrap), lang = "async_iterator_poll_next")]
5051
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
5152

5253
/// Returns the bounds on the remaining length of the async iterator.

0 commit comments

Comments
 (0)