2 // aux-build:arc_wake.rs
4 #![feature(async_await, await_macro)]
9 use std::future::Future;
12 atomic::{self, AtomicUsize},
14 use std::task::{Context, Poll};
15 use arc_wake::ArcWake;
21 impl ArcWake for Counter {
22 fn wake(self: Arc<Self>) {
23 Self::wake_by_ref(&self)
25 fn wake_by_ref(arc_self: &Arc<Self>) {
26 arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
30 struct WakeOnceThenComplete(bool);
32 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
34 impl Future for WakeOnceThenComplete {
36 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
40 cx.waker().wake_by_ref();
47 fn async_block(x: u8) -> impl Future<Output = u8> {
49 await!(wake_and_yield_once());
54 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
56 await!(wake_and_yield_once());
61 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
64 await!(wake_and_yield_once());
71 fn async_closure(x: u8) -> impl Future<Output = u8> {
72 (async move |x: u8| -> u8 {
73 await!(wake_and_yield_once());
78 async fn async_fn(x: u8) -> u8 {
79 await!(wake_and_yield_once());
83 async fn generic_async_fn<T>(x: T) -> T {
84 await!(wake_and_yield_once());
88 async fn async_fn_with_borrow(x: &u8) -> u8 {
89 await!(wake_and_yield_once());
93 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
94 await!(wake_and_yield_once());
98 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
100 await!(wake_and_yield_once());
105 /* FIXME(cramertj) support when `existential type T<'a, 'b>:;` works
106 async fn async_fn_multiple_args(x: &u8, _y: &u8) -> u8 {
107 await!(wake_and_yield_once());
112 async fn async_fn_multiple_args_named_lifetime<'a>(x: &'a u8, _y: &'a u8) -> u8 {
113 await!(wake_and_yield_once());
117 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
119 await!(async_fn_with_borrow_named_lifetime(&y))
123 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
124 await!(wake_and_yield_once());
135 async fn async_method(x: u8) -> u8 {
137 await!(unsafe_async_fn(x))
142 fn test_future_yields_once_then_returns<F, Fut>(f: F)
144 F: FnOnce(u8) -> Fut,
145 Fut: Future<Output = u8>,
147 let mut fut = Box::pin(f(9));
148 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
149 let waker = ArcWake::into_waker(counter.clone());
150 let mut cx = Context::from_waker(&waker);
151 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
152 assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx));
153 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
154 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx));
159 ($($fn_name:expr,)*) => { $(
160 test_future_yields_once_then_returns($fn_name);
164 macro_rules! test_with_borrow {
165 ($($fn_name:expr,)*) => { $(
166 test_future_yields_once_then_returns(|x| {
180 async_fn_with_internal_borrow,
184 unsafe { await!(unsafe_async_fn(x)) }
189 async_block_with_borrow_named_lifetime,
190 async_fn_with_borrow,
191 async_fn_with_borrow_named_lifetime,
192 async_fn_with_impl_future_named_lifetime,
195 await!(async_fn_multiple_args_named_lifetime(x, x))