4 // aux-build:arc_wake.rs
6 #![feature(async_await)]
11 use std::future::Future;
14 atomic::{self, AtomicUsize},
16 use std::task::{Context, Poll};
17 use arc_wake::ArcWake;
23 impl ArcWake for Counter {
24 fn wake(self: Arc<Self>) {
25 Self::wake_by_ref(&self)
27 fn wake_by_ref(arc_self: &Arc<Self>) {
28 arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
32 struct WakeOnceThenComplete(bool);
34 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
36 impl Future for WakeOnceThenComplete {
38 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
42 cx.waker().wake_by_ref();
49 fn async_block(x: u8) -> impl Future<Output = u8> {
51 wake_and_yield_once().await;
56 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
58 wake_and_yield_once().await;
63 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
66 wake_and_yield_once().await;
73 async fn async_fn(x: u8) -> u8 {
74 wake_and_yield_once().await;
78 async fn generic_async_fn<T>(x: T) -> T {
79 wake_and_yield_once().await;
83 async fn async_fn_with_borrow(x: &u8) -> u8 {
84 wake_and_yield_once().await;
88 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
89 wake_and_yield_once().await;
93 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
95 wake_and_yield_once().await;
100 /* FIXME(cramertj) support when `existential type T<'a, 'b>:;` works
101 async fn async_fn_multiple_args(x: &u8, _y: &u8) -> u8 {
102 await!(wake_and_yield_once());
107 async fn async_fn_multiple_args_named_lifetime<'a>(x: &'a u8, _y: &'a u8) -> u8 {
108 wake_and_yield_once().await;
112 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
114 async_fn_with_borrow_named_lifetime(&y).await
118 async unsafe fn unsafe_async_fn(x: u8) -> u8 {
119 wake_and_yield_once().await;
130 async fn async_assoc_item(x: u8) -> u8 {
132 unsafe_async_fn(x).await
136 async unsafe fn async_unsafe_assoc_item(x: u8) -> u8 {
137 unsafe_async_fn(x).await
141 fn test_future_yields_once_then_returns<F, Fut>(f: F)
143 F: FnOnce(u8) -> Fut,
144 Fut: Future<Output = u8>,
146 let mut fut = Box::pin(f(9));
147 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
148 let waker = ArcWake::into_waker(counter.clone());
149 let mut cx = Context::from_waker(&waker);
150 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
151 assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx));
152 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
153 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx));
158 ($($fn_name:expr,)*) => { $(
159 test_future_yields_once_then_returns($fn_name);
163 macro_rules! test_with_borrow {
164 ($($fn_name:expr,)*) => { $(
165 test_future_yields_once_then_returns(|x| {
178 async_fn_with_internal_borrow,
179 Foo::async_assoc_item,
182 unsafe { unsafe_async_fn(x).await }
187 unsafe { Foo::async_unsafe_assoc_item(x).await }
192 async_block_with_borrow_named_lifetime,
193 async_fn_with_borrow,
194 async_fn_with_borrow_named_lifetime,
195 async_fn_with_impl_future_named_lifetime,
198 async_fn_multiple_args_named_lifetime(x, x).await