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

45 lines
1.1 KiB
Rust
Raw Normal View History

2018-11-20 09:09:06 -06:00
#![feature(
async_await,
await_macro,
)]
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;
let y = await!(async { *y + *z });
let a = 10;
let a = &a;
*x + y + *a
}
2019-02-15 02:32:54 -06:00
fn raw_waker_clone(_this: *const ()) -> RawWaker {
panic!("unimplemented");
}
fn raw_waker_wake(_this: *const ()) {
panic!("unimplemented");
}
2019-04-12 15:15:55 -05:00
fn raw_waker_wake_by_ref(_this: *const ()) {
panic!("unimplemented");
}
2019-02-15 02:32:54 -06:00
fn raw_waker_drop(_this: *const ()) {}
2018-11-20 09:09:06 -06:00
2019-04-10 10:20:54 -05:00
static RAW_WAKER: RawWakerVTable = RawWakerVTable::new(
raw_waker_clone,
raw_waker_wake,
2019-04-12 15:15:55 -05:00
raw_waker_wake_by_ref,
2019-04-10 10:20:54 -05:00
raw_waker_drop,
);
2018-11-20 09:09:06 -06:00
2019-02-15 02:32:54 -06:00
fn main() {
2018-11-20 09:09:06 -06:00
let x = 5;
let mut fut = foo(&x, 7);
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);
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&mut context), Poll::Ready(31));
2018-11-20 09:09:06 -06:00
}