3 #![feature(arbitrary_self_types, async_await, await_macro, futures_api)]
6 use std::future::Future;
9 atomic::{self, AtomicUsize},
12 LocalWaker, Poll, Wake,
13 local_waker_from_nonlocal,
20 impl Wake for Counter {
21 fn wake(this: &Arc<Self>) {
22 this.wakes.fetch_add(1, atomic::Ordering::SeqCst);
26 struct WakeOnceThenComplete(bool);
28 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
30 impl Future for WakeOnceThenComplete {
32 fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<()> {
43 fn async_block(x: u8) -> impl Future<Output = u8> {
45 await!(wake_and_yield_once());
50 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
52 await!(wake_and_yield_once());
57 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
60 await!(wake_and_yield_once());
67 fn async_closure(x: u8) -> impl Future<Output = u8> {
68 (async move |x: u8| -> u8 {
69 await!(wake_and_yield_once());
74 async fn async_fn(x: u8) -> u8 {
75 await!(wake_and_yield_once());
79 async fn async_fn_with_borrow(x: &u8) -> u8 {
80 await!(wake_and_yield_once());
84 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
85 await!(wake_and_yield_once());
89 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
91 await!(wake_and_yield_once());
96 async fn async_fn_with_named_lifetime_multiple_args<'a>(x: &'a u8, _y: &'a u8) -> u8 {
97 await!(wake_and_yield_once());
101 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
103 await!(async_fn_with_borrow(&y))
107 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
108 await!(wake_and_yield_once());
119 async fn async_method(x: u8) -> u8 {
121 await!(unsafe_async_fn(x))
126 fn test_future_yields_once_then_returns<F, Fut>(f: F)
128 F: FnOnce(u8) -> Fut,
129 Fut: Future<Output = u8>,
131 let mut fut = Box::pin(f(9));
132 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
133 let waker = local_waker_from_nonlocal(counter.clone());
134 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
135 assert_eq!(Poll::Pending, fut.as_mut().poll(&waker));
136 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
137 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&waker));
142 ($($fn_name:expr,)*) => { $(
143 test_future_yields_once_then_returns($fn_name);
147 macro_rules! test_with_borrow {
148 ($($fn_name:expr,)*) => { $(
149 test_future_yields_once_then_returns(|x| {
162 async_fn_with_internal_borrow,
166 unsafe { await!(unsafe_async_fn(x)) }
172 async_block_with_borrow_named_lifetime,
173 async_fn_with_borrow,
174 async_fn_with_borrow_named_lifetime,
175 async_fn_with_impl_future_named_lifetime,
178 await!(async_fn_with_named_lifetime_multiple_args(x, x))