Auto merge of #899 - RalfJung:generators, r=RalfJung
More generator tests Cc @tmandry -- do these look reasonable for checking the new "partial init" case that was discovered?
This commit is contained in:
commit
7b3de392df
@ -1 +1 @@
|
||||
4be067558962c004b638e4c6f162d50f7c0c98b6
|
||||
5aa3d9a7b5d3a46a7f158e8881146331a6bc9243
|
||||
|
@ -1,4 +1,4 @@
|
||||
#![feature(async_await)]
|
||||
#![feature(async_await, never_type)]
|
||||
|
||||
use std::{future::Future, pin::Pin, task::Poll, ptr};
|
||||
use std::task::{Waker, RawWaker, RawWakerVTable, Context};
|
||||
@ -40,38 +40,42 @@ async fn includes_never(crash: bool, x: u32) -> u32 {
|
||||
result
|
||||
}
|
||||
|
||||
fn raw_waker_clone(_this: *const ()) -> RawWaker {
|
||||
panic!("unimplemented");
|
||||
async fn partial_init(x: u32) -> u32 {
|
||||
#[allow(unreachable_code)]
|
||||
let _x: (String, !) = (String::new(), return async { x + x }.await);
|
||||
}
|
||||
fn raw_waker_wake(_this: *const ()) {
|
||||
panic!("unimplemented");
|
||||
}
|
||||
fn raw_waker_wake_by_ref(_this: *const ()) {
|
||||
panic!("unimplemented");
|
||||
}
|
||||
fn raw_waker_drop(_this: *const ()) {}
|
||||
|
||||
static RAW_WAKER: RawWakerVTable = RawWakerVTable::new(
|
||||
raw_waker_clone,
|
||||
raw_waker_wake,
|
||||
raw_waker_wake_by_ref,
|
||||
raw_waker_drop,
|
||||
);
|
||||
fn run_fut(mut fut: impl Future<Output=u32>, output: u32) {
|
||||
fn raw_waker_clone(_this: *const ()) -> RawWaker {
|
||||
panic!("unimplemented");
|
||||
}
|
||||
fn raw_waker_wake(_this: *const ()) {
|
||||
panic!("unimplemented");
|
||||
}
|
||||
fn raw_waker_wake_by_ref(_this: *const ()) {
|
||||
panic!("unimplemented");
|
||||
}
|
||||
fn raw_waker_drop(_this: *const ()) {}
|
||||
|
||||
static RAW_WAKER: RawWakerVTable = RawWakerVTable::new(
|
||||
raw_waker_clone,
|
||||
raw_waker_wake,
|
||||
raw_waker_wake_by_ref,
|
||||
raw_waker_drop,
|
||||
);
|
||||
|
||||
let waker = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &RAW_WAKER)) };
|
||||
let mut context = Context::from_waker(&waker);
|
||||
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&mut context), Poll::Ready(output));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = 5;
|
||||
let mut fut = foo(&x, 7);
|
||||
let waker = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &RAW_WAKER)) };
|
||||
let mut context = Context::from_waker(&waker);
|
||||
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&mut context), Poll::Ready(31));
|
||||
run_fut(foo(&x, 7), 31);
|
||||
|
||||
let mut fut = build_aggregate(1, 2, 3, 4);
|
||||
let waker = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &RAW_WAKER)) };
|
||||
let mut context = Context::from_waker(&waker);
|
||||
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&mut context), Poll::Ready(10));
|
||||
run_fut(build_aggregate(1, 2, 3, 4), 10);
|
||||
|
||||
let mut fut = includes_never(false, 4);
|
||||
let waker = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &RAW_WAKER)) };
|
||||
let mut context = Context::from_waker(&waker);
|
||||
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&mut context), Poll::Ready(16));
|
||||
run_fut(includes_never(false, 4), 16);
|
||||
|
||||
run_fut(partial_init(4), 8);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#![feature(generators, generator_trait)]
|
||||
#![feature(generators, generator_trait, never_type)]
|
||||
|
||||
use std::ops::{GeneratorState, Generator};
|
||||
use std::pin::Pin;
|
||||
@ -26,6 +26,7 @@ fn never() -> Never {
|
||||
|
||||
fn main() {
|
||||
finish(1, || yield 1);
|
||||
|
||||
finish(3, || {
|
||||
let mut x = 0;
|
||||
yield 1;
|
||||
@ -35,23 +36,27 @@ fn main() {
|
||||
yield 1;
|
||||
assert_eq!(x, 2);
|
||||
});
|
||||
|
||||
finish(7*8/2, || {
|
||||
for i in 0..8 {
|
||||
yield i;
|
||||
}
|
||||
});
|
||||
|
||||
finish(1, || {
|
||||
if true {
|
||||
yield 1;
|
||||
} else {
|
||||
}
|
||||
});
|
||||
|
||||
finish(1, || {
|
||||
if false {
|
||||
} else {
|
||||
yield 1;
|
||||
}
|
||||
});
|
||||
|
||||
finish(2, || {
|
||||
if { yield 1; false } {
|
||||
yield 1;
|
||||
@ -59,6 +64,7 @@ fn main() {
|
||||
}
|
||||
yield 1;
|
||||
});
|
||||
|
||||
// also test a self-referential generator
|
||||
assert_eq!(
|
||||
finish(5, || {
|
||||
@ -71,6 +77,7 @@ fn main() {
|
||||
}),
|
||||
10
|
||||
);
|
||||
|
||||
let b = true;
|
||||
finish(1, || {
|
||||
yield 1;
|
||||
@ -80,4 +87,10 @@ fn main() {
|
||||
yield 2;
|
||||
drop(x);
|
||||
});
|
||||
|
||||
finish(3, || {
|
||||
yield 1;
|
||||
#[allow(unreachable_code)]
|
||||
let _x: (String, !) = (String::new(), { yield 2; return });
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user