3 // compile-flags: -Zdrop-tracking
5 #![feature(never_type)]
7 use std::future::Future;
9 // See if we can run a basic `async fn`
10 pub async fn foo(x: &u32, y: u32) -> u32 {
14 let y = async { *y + *z }.await;
20 async fn add(x: u32, y: u32) -> u32 {
21 let a = async { x + y };
25 async fn build_aggregate(a: u32, b: u32, c: u32, d: u32) -> u32 {
26 let x = (add(a, b).await, add(c, d).await);
35 async fn includes_never(crash: bool, x: u32) -> u32 {
36 let result = async { x * x }.await;
42 result *= async { x + x }.await;
47 async fn partial_init(x: u32) -> u32 {
48 #[allow(unreachable_code)]
49 let _x: (String, !) = (String::new(), return async { x + x }.await);
52 async fn read_exact(_from: &mut &[u8], _to: &mut [u8]) -> Option<()> {
56 async fn hello_world() {
58 let mut reader = &data[..];
60 let mut marker = [0u8; 1];
61 read_exact(&mut reader, &mut marker).await.unwrap();
64 fn run_fut<T>(fut: impl Future<Output = T>) -> T {
66 use std::task::{Context, Poll, Wake, Waker};
69 impl Wake for MyWaker {
70 fn wake(self: Arc<Self>) {
75 let waker = Waker::from(Arc::new(MyWaker));
76 let mut context = Context::from_waker(&waker);
78 let mut pinned = Box::pin(fut);
80 match pinned.as_mut().poll(&mut context) {
81 Poll::Pending => continue,
82 Poll::Ready(v) => return v,
89 assert_eq!(run_fut(foo(&x, 7)), 31);
90 assert_eq!(run_fut(build_aggregate(1, 2, 3, 4)), 10);
91 assert_eq!(run_fut(includes_never(false, 4)), 16);
92 assert_eq!(run_fut(partial_init(4)), 8);
93 run_fut(hello_world());