rust/tests/run-pass/async-fn.rs

83 lines
1.9 KiB
Rust
Raw Normal View History

2019-08-21 02:07:27 -05:00
#![feature(never_type)]
2018-11-20 09:09:06 -06:00
2019-02-15 02:32:54 -06:00
use std::{future::Future, pin::Pin, task::Poll, ptr};
2019-04-10 10:20:54 -05:00
use std::task::{Waker, RawWaker, RawWakerVTable, Context};
2018-11-20 09:09:06 -06:00
// 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;
2019-07-31 02:42:38 -05:00
let y = async { *y + *z }.await;
2018-11-20 09:09:06 -06:00
let a = 10;
let a = &a;
*x + y + *a
}
async fn add(x: u32, y: u32) -> u32 {
2019-09-11 10:13:32 -05:00
let a = async { x + y };
a.await
}
async fn build_aggregate(a: u32, b: u32, c: u32, d: u32) -> u32 {
let x = (add(a, b).await, add(c, d).await);
x.0 + x.1
}
enum Never {}
fn never() -> Never {
panic!()
}
async fn includes_never(crash: bool, x: u32) -> u32 {
let mut result = async { x * x }.await;
if !crash {
return result;
}
#[allow(unused)]
let bad = never();
result *= async { x + x }.await;
drop(bad);
result
}
async fn partial_init(x: u32) -> u32 {
#[allow(unreachable_code)]
let _x: (String, !) = (String::new(), return async { x + x }.await);
}
2019-08-09 03:26:48 -05:00
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 ()) {}
2018-11-20 09:09:06 -06:00
2019-08-09 03:26:48 -05:00
static RAW_WAKER: RawWakerVTable = RawWakerVTable::new(
raw_waker_clone,
raw_waker_wake,
raw_waker_wake_by_ref,
raw_waker_drop,
);
2018-11-20 09:09:06 -06:00
2019-04-12 15:15:55 -05:00
let waker = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &RAW_WAKER)) };
2019-04-10 10:20:54 -05:00
let mut context = Context::from_waker(&waker);
2019-08-09 03:26:48 -05:00
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&mut context), Poll::Ready(output));
}
2019-08-09 03:26:48 -05:00
fn main() {
let x = 5;
run_fut(foo(&x, 7), 31);
2019-08-09 03:26:48 -05:00
run_fut(build_aggregate(1, 2, 3, 4), 10);
run_fut(includes_never(false, 4), 16);
run_fut(partial_init(4), 8);
2018-11-20 09:09:06 -06:00
}