-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathasync_gen_fn_iter.rs
82 lines (64 loc) · 1.77 KB
/
async_gen_fn_iter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//@ edition: 2024
//@ run-pass
#![feature(gen_blocks, async_iterator)]
// make sure that a ridiculously simple async gen fn works as an iterator.
async fn pause() {
// this doesn't actually do anything, lol
}
async fn one() -> i32 {
1
}
async fn two() -> i32 {
2
}
async gen fn foo() -> i32 {
yield one().await;
pause().await;
yield two().await;
pause().await;
yield 3;
pause().await;
}
async fn async_main() {
let mut iter = std::pin::pin!(foo());
assert_eq!(iter.next().await, Some(1));
assert_eq!(iter.as_mut().next().await, Some(2));
assert_eq!(iter.as_mut().next().await, Some(3));
assert_eq!(iter.as_mut().next().await, None);
// Test that the iterator is fused and does not panic
assert_eq!(iter.as_mut().next().await, None);
assert_eq!(iter.as_mut().next().await, None);
}
// ------------------------------------------------------------------------- //
// Implementation Details Below...
use std::pin::{Pin, pin};
use std::task::*;
use std::async_iter::AsyncIterator;
trait AsyncIterExt {
fn next(&mut self) -> Next<'_, Self>;
}
impl<T> AsyncIterExt for T {
fn next(&mut self) -> Next<'_, Self> {
Next { s: self }
}
}
struct Next<'s, S: ?Sized> {
s: &'s mut S,
}
impl<'s, S: AsyncIterator> Future for Next<'s, S> where S: Unpin {
type Output = Option<S::Item>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
Pin::new(&mut *self.s).poll_next(cx)
}
}
fn main() {
let mut fut = pin!(async_main());
// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());
loop {
match fut.as_mut().poll(ctx) {
Poll::Pending => {}
Poll::Ready(()) => break,
}
}
}