1 #![feature(never_type)]
3 use std::future::Future;
5 // See if we can run a basic `async fn`
6 pub async fn foo(x: &u32, y: u32) -> u32 {
10 let y = async { *y + *z }.await;
16 async fn add(x: u32, y: u32) -> u32 {
17 let a = async { x + y };
21 async fn build_aggregate(a: u32, b: u32, c: u32, d: u32) -> u32 {
22 let x = (add(a, b).await, add(c, d).await);
31 async fn includes_never(crash: bool, x: u32) -> u32 {
32 let result = async { x * x }.await;
38 result *= async { x + x }.await;
43 async fn partial_init(x: u32) -> u32 {
44 #[allow(unreachable_code)]
45 let _x: (String, !) = (String::new(), return async { x + x }.await);
48 async fn read_exact(_from: &mut &[u8], _to: &mut [u8]) -> Option<()> {
52 async fn hello_world() {
54 let mut reader = &data[..];
56 let mut marker = [0u8; 1];
57 read_exact(&mut reader, &mut marker).await.unwrap();
60 fn run_fut<T>(fut: impl Future<Output = T>) -> T {
62 use std::task::{Context, Poll, Wake, Waker};
65 impl Wake for MyWaker {
66 fn wake(self: Arc<Self>) {
71 let waker = Waker::from(Arc::new(MyWaker));
72 let mut context = Context::from_waker(&waker);
74 let mut pinned = Box::pin(fut);
76 match pinned.as_mut().poll(&mut context) {
77 Poll::Pending => continue,
78 Poll::Ready(v) => return v,
85 assert_eq!(run_fut(foo(&x, 7)), 31);
86 assert_eq!(run_fut(build_aggregate(1, 2, 3, 4)), 10);
87 assert_eq!(run_fut(includes_never(false, 4)), 16);
88 assert_eq!(run_fut(partial_init(4)), 8);
89 run_fut(hello_world());