Skip to content

Commit e7f0faa

Browse files
committed
Add IntoAsyncIterator
1 parent e2a3c9b commit e7f0faa

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

library/core/src/async_iter/async_iter.rs

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

library/core/src/async_iter/mod.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use core::async_iter::{self, AsyncIterator, IntoAsyncIterator};
2+
use core::pin::pin;
3+
use core::task::Poll;
4+
5+
mod noop_waker {
6+
use core::task::{RawWaker, RawWakerVTable, Waker};
7+
8+
const VTABLE: RawWakerVTable =
9+
RawWakerVTable::new(|_| RawWaker::new(core::ptr::null(), &VTABLE), |_| (), |_| (), |_| ());
10+
11+
pub(super) const NOOP_WAKER: Waker =
12+
unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
13+
}
14+
15+
#[test]
16+
fn into_async_iter() {
17+
let async_iter = async_iter::from_iter(0..3);
18+
let mut async_iter = pin!(async_iter.into_async_iter());
19+
20+
let waker = noop_waker::NOOP_WAKER;
21+
let mut cx = &mut core::task::Context::from_waker(&waker);
22+
23+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(0)));
24+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));
25+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(2)));
26+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(None));
27+
}

library/core/tests/lib.rs

Lines changed: 3 additions & 0 deletions
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)]
@@ -126,6 +128,7 @@ mod any;
126128
mod array;
127129
mod ascii;
128130
mod asserting;
131+
mod async_iter;
129132
mod atomic;
130133
mod bool;
131134
mod cell;

0 commit comments

Comments
 (0)