Skip to content

Commit eef023c

Browse files
Rollup merge of rust-lang#119222 - eholk:into-async-iterator, r=compiler-errors,dtolnay
Add `IntoAsyncIterator` This introduces the `IntoAsyncIterator` trait and uses it in the desugaring of the unstable `for await` loop syntax. This is mostly added for symmetry with `Iterator` and `IntoIterator`. r? `@compiler-errors` cc `@rust-lang/libs-api,` `@rust-lang/wg-async`
2 parents ae0a6e8 + aaa3e76 commit eef023c

File tree

10 files changed

+93
-14
lines changed

10 files changed

+93
-14
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
18141814
arena_vec![self; head],
18151815
)
18161816
}
1817-
ForLoopKind::ForAwait => self.arena.alloc(head),
1817+
// ` unsafe { Pin::new_unchecked(&mut into_async_iter(<head>)) }`
1818+
ForLoopKind::ForAwait => {
1819+
// `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
1820+
let iter = self.expr_call_lang_item_fn(
1821+
head_span,
1822+
hir::LangItem::IntoAsyncIterIntoIter,
1823+
arena_vec![self; head],
1824+
);
1825+
let iter = self.expr_mut_addr_of(head_span, iter);
1826+
// `Pin::new_unchecked(...)`
1827+
let iter = self.arena.alloc(self.expr_call_lang_item_fn_mut(
1828+
head_span,
1829+
hir::LangItem::PinNewUnchecked,
1830+
arena_vec![self; iter],
1831+
));
1832+
// `unsafe { ... }`
1833+
let iter = self.arena.alloc(self.expr_unsafe(iter));
1834+
iter
1835+
}
18181836
};
18191837

18201838
let match_expr = self.arena.alloc(self.expr_match(

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ language_item_table! {
308308
FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
309309

310310
AsyncIteratorPollNext, sym::async_iterator_poll_next, async_iterator_poll_next, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
311+
IntoAsyncIterIntoIter, sym::into_async_iter_into_iter, into_async_iter_into_iter, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
311312

312313
Option, sym::Option, option_type, Target::Enum, GenericRequirement::None;
313314
OptionSome, sym::Some, option_some_variant, Target::Variant, GenericRequirement::None;

library/core/src/async_iter/async_iter.rs

+23
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,26 @@ impl<T> Poll<Option<T>> {
135135
#[lang = "AsyncGenFinished"]
136136
pub const FINISHED: Self = Poll::Ready(None);
137137
}
138+
139+
/// Convert something into an async iterator
140+
#[unstable(feature = "async_iterator", issue = "79024")]
141+
pub trait IntoAsyncIterator {
142+
/// The type of the item yielded by the iterator
143+
type Item;
144+
/// The type of the resulting iterator
145+
type IntoAsyncIter: AsyncIterator<Item = Self::Item>;
146+
147+
/// Converts `self` into an async iterator
148+
#[cfg_attr(not(bootstrap), lang = "into_async_iter_into_iter")]
149+
fn into_async_iter(self) -> Self::IntoAsyncIter;
150+
}
151+
152+
#[unstable(feature = "async_iterator", issue = "79024")]
153+
impl<I: AsyncIterator> IntoAsyncIterator for I {
154+
type Item = I::Item;
155+
type IntoAsyncIter = I;
156+
157+
fn into_async_iter(self) -> Self::IntoAsyncIter {
158+
self
159+
}
160+
}

library/core/src/async_iter/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@
124124
mod async_iter;
125125
mod from_iter;
126126

127-
pub use async_iter::AsyncIterator;
127+
pub use async_iter::{AsyncIterator, IntoAsyncIterator};
128128
pub use from_iter::{from_iter, FromIter};

library/core/tests/async_iter/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use core::async_iter::{self, AsyncIterator, IntoAsyncIterator};
2+
use core::pin::pin;
3+
use core::task::Poll;
4+
5+
#[test]
6+
fn into_async_iter() {
7+
let async_iter = async_iter::from_iter(0..3);
8+
let mut async_iter = pin!(async_iter.into_async_iter());
9+
10+
let waker = core::task::Waker::noop();
11+
let mut cx = &mut core::task::Context::from_waker(&waker);
12+
13+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(0)));
14+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));
15+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(2)));
16+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(None));
17+
}

library/core/tests/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#![feature(array_windows)]
55
#![feature(ascii_char)]
66
#![feature(ascii_char_variants)]
7+
#![feature(async_iter_from_iter)]
8+
#![feature(async_iterator)]
79
#![feature(bigint_helper_methods)]
810
#![feature(cell_update)]
911
#![feature(const_align_offset)]
@@ -55,6 +57,7 @@
5557
#![feature(maybe_uninit_write_slice)]
5658
#![feature(maybe_uninit_uninit_array_transpose)]
5759
#![feature(min_specialization)]
60+
#![feature(noop_waker)]
5861
#![feature(numfmt)]
5962
#![feature(num_midpoint)]
6063
#![feature(isqrt)]
@@ -125,6 +128,7 @@ mod any;
125128
mod array;
126129
mod ascii;
127130
mod asserting;
131+
mod async_iter;
128132
mod atomic;
129133
mod bool;
130134
mod cell;

tests/ui/associated-inherent-types/issue-109071.no_gate.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ error[E0223]: ambiguous associated type
3333
--> $DIR/issue-109071.rs:15:22
3434
|
3535
LL | fn T() -> Option<Self::Item> {}
36-
| ^^^^^^^^^^ help: use fully-qualified syntax: `<Windows<T> as IntoIterator>::Item`
36+
| ^^^^^^^^^^
37+
|
38+
help: use fully-qualified syntax
39+
|
40+
LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
41+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
43+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3744

3845
error: aborting due to 4 previous errors
3946

tests/ui/async-await/for-await-consumes-iter.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,17 @@ LL | let iter = core::async_iter::from_iter(0..3);
55
| ---- move occurs because `iter` has type `FromIter<std::ops::Range<i32>>`, which does not implement the `Copy` trait
66
LL | let mut count = 0;
77
LL | for await i in iter {
8-
| -------------------
9-
| | |
10-
| | value moved here
11-
| inside of this loop
8+
| ---- `iter` moved due to this method call
129
...
1310
LL | for await i in iter {
1411
| ^^^^ value used here after move
1512
|
16-
help: consider cloning the value if the performance cost is acceptable
13+
note: `into_async_iter` takes ownership of the receiver `self`, which moves `iter`
14+
--> $SRC_DIR/core/src/async_iter/async_iter.rs:LL:COL
15+
help: you can `clone` the value and consume it, but this might not be your desired behavior
1716
|
1817
LL | for await i in iter.clone() {
1918
| ++++++++
20-
help: borrow this binding in the pattern to avoid moving the value
21-
|
22-
LL | for await i in ref iter {
23-
| +++
2419

2520
error: aborting due to 1 previous error
2621

tests/ui/suggestions/suggest-trait-in-ufcs-in-hrtb.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ error[E0223]: ambiguous associated type
22
--> $DIR/suggest-trait-in-ufcs-in-hrtb.rs:5:38
33
|
44
LL | impl<S> Foo for Bar<S> where for<'a> <&'a S>::Item: Foo {}
5-
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<&'a S as IntoIterator>::Item`
5+
| ^^^^^^^^^^^^^
6+
|
7+
help: use fully-qualified syntax
8+
|
9+
LL | impl<S> Foo for Bar<S> where for<'a> <&'a S as IntoAsyncIterator>::Item: Foo {}
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
LL | impl<S> Foo for Bar<S> where for<'a> <&'a S as IntoIterator>::Item: Foo {}
12+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
613

714
error: aborting due to 1 previous error
815

tests/ui/typeck/issue-110052.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ error[E0223]: ambiguous associated type
22
--> $DIR/issue-110052.rs:6:30
33
|
44
LL | for<'iter> dyn Validator<<&'iter I>::Item>:,
5-
| ^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<&'iter I as IntoIterator>::Item`
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
help: use fully-qualified syntax
8+
|
9+
LL | for<'iter> dyn Validator<<&'iter I as IntoAsyncIterator>::Item>:,
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
LL | for<'iter> dyn Validator<<&'iter I as IntoIterator>::Item>:,
12+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
613

714
error: aborting due to 1 previous error
815

0 commit comments

Comments
 (0)