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 async_fn_with_borrow(x: &u8) -> u8 {
83 await!(wake_and_yield_once());
87 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
88 await!(wake_and_yield_once());
92 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
94 await!(wake_and_yield_once());
99 async fn async_fn_with_named_lifetime_multiple_args<'a>(x: &'a u8, _y: &'a u8) -> u8 {
100 await!(wake_and_yield_once());
104 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
106 await!(async_fn_with_borrow(&y))
110 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
111 await!(wake_and_yield_once());
122 async fn async_method(x: u8) -> u8 {
124 await!(unsafe_async_fn(x))
129 fn test_future_yields_once_then_returns<F, Fut>(f: F)
131 F: FnOnce(u8) -> Fut,
132 Fut: Future<Output = u8>,
134 let mut fut = Box::pin(f(9));
135 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
136 let waker = ArcWake::into_waker(counter.clone());
137 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
138 assert_eq!(Poll::Pending, fut.as_mut().poll(&waker));
139 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
140 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&waker));
145 ($($fn_name:expr,)*) => { $(
146 test_future_yields_once_then_returns($fn_name);
150 macro_rules! test_with_borrow {
151 ($($fn_name:expr,)*) => { $(
152 test_future_yields_once_then_returns(|x| {
165 async_fn_with_internal_borrow,
169 unsafe { await!(unsafe_async_fn(x)) }
175 async_block_with_borrow_named_lifetime,
176 async_fn_with_borrow,
177 async_fn_with_borrow_named_lifetime,
178 async_fn_with_impl_future_named_lifetime,
181 await!(async_fn_with_named_lifetime_multiple_args(x, x))