Merge pull request #536 from RalfJung/self-referential-generator

test self-referential generator
This commit is contained in:
Ralf Jung 2018-11-26 11:52:44 +01:00 committed by GitHub
commit 6f9ee8b9e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 14 deletions

View File

@ -0,0 +1,35 @@
#![feature(
async_await,
await_macro,
futures_api,
pin,
)]
use std::{future::Future, pin::Pin, task::Poll};
// See if we can run a basic `async fn`
pub async fn foo(x: &u32, y: u32) -> u32 {
let y = &y;
let z = 9;
let z = &z;
let y = await!(async { *y + *z });
let a = 10;
let a = &a;
*x + y + *a
}
fn main() {
use std::{sync::Arc, task::{Wake, local_waker}};
struct NoWake;
impl Wake for NoWake {
fn wake(_arc_self: &Arc<Self>) {
panic!();
}
}
let lw = unsafe { local_waker(Arc::new(NoWake)) };
let x = 5;
let mut fut = foo(&x, 7);
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&lw), Poll::Ready(31));
}

View File

@ -13,11 +13,11 @@
use std::ops::{GeneratorState, Generator};
fn finish<T>(mut amt: usize, mut t: T) -> T::Return
where T: Generator<Yield = ()>
where T: Generator<Yield = usize>
{
loop {
match unsafe { t.resume() } {
GeneratorState::Yielded(()) => amt -= 1,
GeneratorState::Yielded(y) => amt -= y,
GeneratorState::Complete(ret) => {
assert_eq!(amt, 0);
return ret
@ -28,38 +28,50 @@ fn finish<T>(mut amt: usize, mut t: T) -> T::Return
}
fn main() {
finish(1, || yield);
finish(1, || yield 1);
finish(3, || {
let mut x = 0;
yield;
yield 1;
x += 1;
yield;
yield 1;
x += 1;
yield;
yield 1;
assert_eq!(x, 2);
});
finish(8, || {
for _ in 0..8 {
yield;
finish(7*8/2, || {
for i in 0..8 {
yield i;
}
});
finish(1, || {
if true {
yield;
yield 1;
} else {
}
});
finish(1, || {
if false {
} else {
yield;
yield 1;
}
});
finish(2, || {
if { yield; false } {
yield;
if { yield 1; false } {
yield 1;
panic!()
}
yield
yield 1;
});
// also test a self-referential generator
assert_eq!(
finish(5, || {
let mut x = Box::new(5);
let y = &mut *x;
*y = 5;
yield *y;
*y = 10;
*x
}),
10
);
}