2 // aux-build:arc_wake.rs
4 #![feature(arbitrary_self_types, async_await, await_macro, futures_api)]
9 use std::future::Future;
12 atomic::{self, AtomicUsize},
17 use arc_wake::ArcWake;
23 impl ArcWake for Counter {
24 fn wake(arc_self: &Arc<Self>) {
25 arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
29 struct WakeOnceThenComplete(bool);
31 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
33 impl Future for WakeOnceThenComplete {
35 fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<()> {
46 fn async_block(x: u8) -> impl Future<Output = u8> {
48 await!(wake_and_yield_once());
53 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
55 await!(wake_and_yield_once());
60 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
63 await!(wake_and_yield_once());
70 fn async_closure(x: u8) -> impl Future<Output = u8> {
71 (async move |x: u8| -> u8 {
72 await!(wake_and_yield_once());
77 async fn async_fn(x: u8) -> u8 {
78 await!(wake_and_yield_once());
82 async fn generic_async_fn<T>(x: T) -> T {
83 await!(wake_and_yield_once());
87 async fn async_fn_with_borrow(x: &u8) -> u8 {
88 await!(wake_and_yield_once());
92 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
93 await!(wake_and_yield_once());
97 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
99 await!(wake_and_yield_once());
104 /* FIXME(cramertj) support when `existential type T<'a, 'b>:;` works
105 async fn async_fn_multiple_args(x: &u8, _y: &u8) -> u8 {
106 await!(wake_and_yield_once());
111 async fn async_fn_multiple_args_named_lifetime<'a>(x: &'a u8, _y: &'a u8) -> u8 {
112 await!(wake_and_yield_once());
116 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
118 await!(async_fn_with_borrow_named_lifetime(&y))
122 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
123 await!(wake_and_yield_once());
134 async fn async_method(x: u8) -> u8 {
136 await!(unsafe_async_fn(x))
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 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
150 assert_eq!(Poll::Pending, fut.as_mut().poll(&waker));
151 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
152 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&waker));
157 ($($fn_name:expr,)*) => { $(
158 test_future_yields_once_then_returns($fn_name);
162 macro_rules! test_with_borrow {
163 ($($fn_name:expr,)*) => { $(
164 test_future_yields_once_then_returns(|x| {
178 async_fn_with_internal_borrow,
182 unsafe { await!(unsafe_async_fn(x)) }
187 async_block_with_borrow_named_lifetime,
188 async_fn_with_borrow,
189 async_fn_with_borrow_named_lifetime,
190 async_fn_with_impl_future_named_lifetime,
193 await!(async_fn_multiple_args_named_lifetime(x, x))