3 // revisions: default nomiropt
4 //[nomiropt]compile-flags: -Z mir-opt-level=0
7 // aux-build:arc_wake.rs
9 #![feature(async_closure)]
11 extern crate arc_wake;
14 use std::future::Future;
17 atomic::{self, AtomicUsize},
19 use std::task::{Context, Poll};
20 use arc_wake::ArcWake;
26 impl ArcWake for Counter {
27 fn wake(self: Arc<Self>) {
28 Self::wake_by_ref(&self)
30 fn wake_by_ref(arc_self: &Arc<Self>) {
31 arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
35 struct WakeOnceThenComplete(bool);
37 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
39 impl Future for WakeOnceThenComplete {
41 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
45 cx.waker().wake_by_ref();
52 fn async_closure(x: u8) -> impl Future<Output = u8> {
53 (async move |x: u8| -> u8 {
54 wake_and_yield_once().await;
59 fn async_closure_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
61 async move |x: u8| unsafe_fn(unsafe_async_fn(x).await)
65 async unsafe fn unsafe_async_fn(x: u8) -> u8 {
66 wake_and_yield_once().await;
70 unsafe fn unsafe_fn(x: u8) -> u8 {
74 fn test_future_yields_once_then_returns<F, Fut>(f: F)
77 Fut: Future<Output = u8>,
79 let mut fut = Box::pin(f(9));
80 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
81 let waker = ArcWake::into_waker(counter.clone());
82 let mut cx = Context::from_waker(&waker);
83 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
84 assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx));
85 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
86 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx));
91 ($($fn_name:expr,)*) => { $(
92 test_future_yields_once_then_returns($fn_name);
98 async_closure_in_unsafe_block,