forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c77058
commit 11375c8
Showing
5 changed files
with
148 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0658]: gen blocks are experimental | ||
--> $DIR/async_gen_fn.rs:4:1 | ||
| | ||
LL | async gen fn foo() {} | ||
| ^^^^^^^^^ | ||
| | ||
= note: see issue #117078 <https://github.com/rust-lang/rust/issues/117078> for more information | ||
= help: add `#![feature(gen_blocks)]` to the crate attributes to enable | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error[E0670]: `async fn` is not permitted in Rust 2015 | ||
--> $DIR/async_gen_fn.rs:4:1 | ||
| | ||
LL | async gen fn foo() {} | ||
| ^^^^^ to use `async fn`, switch to Rust 2018 or later | ||
| | ||
= help: pass `--edition 2021` to `rustc` | ||
= note: for more on editions, read https://doc.rust-lang.org/edition-guide | ||
|
||
error: expected one of `extern`, `fn`, or `unsafe`, found `gen` | ||
--> $DIR/async_gen_fn.rs:4:7 | ||
| | ||
LL | async gen fn foo() {} | ||
| ^^^ expected one of `extern`, `fn`, or `unsafe` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0670`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
// edition: 2024 | ||
// compile-flags: -Zunstable-options | ||
// check-pass | ||
// revisions: e2024 none | ||
//[e2024] compile-flags: --edition 2024 -Zunstable-options | ||
|
||
#![feature(gen_blocks, async_iterator)] | ||
|
||
async fn bar() {} | ||
|
||
async gen fn foo() { | ||
yield bar().await; | ||
} | ||
async gen fn foo() {} | ||
//[none]~^ ERROR: `async fn` is not permitted in Rust 2015 | ||
//[none]~| ERROR: expected one of `extern`, `fn`, or `unsafe`, found `gen` | ||
//[e2024]~^^^ ERROR: gen blocks are experimental | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// edition: 2024 | ||
// compile-flags: -Zunstable-options | ||
// 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); | ||
} | ||
|
||
// ------------------------------------------------------------------------- // | ||
// Implementation Details Below... | ||
|
||
use std::pin::Pin; | ||
use std::task::*; | ||
use std::async_iter::AsyncIterator; | ||
use std::future::Future; | ||
|
||
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) | ||
} | ||
} | ||
|
||
pub fn noop_waker() -> Waker { | ||
let raw = RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE); | ||
|
||
// SAFETY: the contracts for RawWaker and RawWakerVTable are upheld | ||
unsafe { Waker::from_raw(raw) } | ||
} | ||
|
||
const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop); | ||
|
||
unsafe fn noop_clone(_p: *const ()) -> RawWaker { | ||
RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE) | ||
} | ||
|
||
unsafe fn noop(_p: *const ()) {} | ||
|
||
fn main() { | ||
let mut fut = async_main(); | ||
|
||
// Poll loop, just to test the future... | ||
let waker = noop_waker(); | ||
let ctx = &mut Context::from_waker(&waker); | ||
|
||
loop { | ||
match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } { | ||
Poll::Pending => {} | ||
Poll::Ready(()) => break, | ||
} | ||
} | ||
} |