2 // aux-build:arc_wake.rs
4 #![feature(async_await, await_macro, futures_api)]
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(arc_self: &Arc<Self>) {
23 arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
27 struct WakeOnceThenComplete(bool);
29 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
31 impl Future for WakeOnceThenComplete {
33 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
44 fn async_block(x: u8) -> impl Future<Output = u8> {
46 await!(wake_and_yield_once());
51 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
53 await!(wake_and_yield_once());
58 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
61 await!(wake_and_yield_once());
68 fn async_closure(x: u8) -> impl Future<Output = u8> {
69 (async move |x: u8| -> u8 {
70 await!(wake_and_yield_once());
75 async fn async_fn(x: u8) -> u8 {
76 await!(wake_and_yield_once());
80 async fn generic_async_fn<T>(x: T) -> T {
81 await!(wake_and_yield_once());
85 async fn async_fn_with_borrow(x: &u8) -> u8 {
86 await!(wake_and_yield_once());
90 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
91 await!(wake_and_yield_once());
95 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
97 await!(wake_and_yield_once());
102 /* FIXME(cramertj) support when `existential type T<'a, 'b>:;` works
103 async fn async_fn_multiple_args(x: &u8, _y: &u8) -> u8 {
104 await!(wake_and_yield_once());
109 async fn async_fn_multiple_args_named_lifetime<'a>(x: &'a u8, _y: &'a u8) -> u8 {
110 await!(wake_and_yield_once());
114 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
116 await!(async_fn_with_borrow_named_lifetime(&y))
120 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
121 await!(wake_and_yield_once());
132 async fn async_method(x: u8) -> u8 {
134 await!(unsafe_async_fn(x))
139 fn test_future_yields_once_then_returns<F, Fut>(f: F)
141 F: FnOnce(u8) -> Fut,
142 Fut: Future<Output = u8>,
144 let mut fut = Box::pin(f(9));
145 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
146 let waker = ArcWake::into_waker(counter.clone());
147 let mut cx = Context::from_waker(&waker);
148 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
149 assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx));
150 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
151 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx));
156 ($($fn_name:expr,)*) => { $(
157 test_future_yields_once_then_returns($fn_name);
161 macro_rules! test_with_borrow {
162 ($($fn_name:expr,)*) => { $(
163 test_future_yields_once_then_returns(|x| {
177 async_fn_with_internal_borrow,
181 unsafe { await!(unsafe_async_fn(x)) }
186 async_block_with_borrow_named_lifetime,
187 async_fn_with_borrow,
188 async_fn_with_borrow_named_lifetime,
189 async_fn_with_impl_future_named_lifetime,
192 await!(async_fn_multiple_args_named_lifetime(x, x))