1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 #![feature(arbitrary_self_types, async_await, await_macro, futures_api, pin)]
16 use std::future::Future;
19 atomic::{self, AtomicUsize},
22 LocalWaker, Poll, Wake,
23 local_waker_from_nonlocal,
30 impl Wake for Counter {
31 fn wake(this: &Arc<Self>) {
32 this.wakes.fetch_add(1, atomic::Ordering::SeqCst);
36 struct WakeOnceThenComplete(bool);
38 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
40 impl Future for WakeOnceThenComplete {
42 fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<()> {
53 fn async_block(x: u8) -> impl Future<Output = u8> {
55 await!(wake_and_yield_once());
60 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
62 await!(wake_and_yield_once());
67 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
70 await!(wake_and_yield_once());
77 fn async_closure(x: u8) -> impl Future<Output = u8> {
78 (async move |x: u8| -> u8 {
79 await!(wake_and_yield_once());
84 async fn async_fn(x: u8) -> u8 {
85 await!(wake_and_yield_once());
89 async fn async_fn_with_borrow(x: &u8) -> u8 {
90 await!(wake_and_yield_once());
94 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
95 await!(wake_and_yield_once());
99 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
101 await!(wake_and_yield_once());
106 async fn async_fn_with_named_lifetime_multiple_args<'a>(x: &'a u8, _y: &'a u8) -> u8 {
107 await!(wake_and_yield_once());
111 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
113 await!(async_fn_with_borrow(&y))
117 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
118 await!(wake_and_yield_once());
129 async fn async_method(x: u8) -> u8 {
131 await!(unsafe_async_fn(x))
136 fn test_future_yields_once_then_returns<F, Fut>(f: F)
138 F: FnOnce(u8) -> Fut,
139 Fut: Future<Output = u8>,
141 let mut fut = Box::pinned(f(9));
142 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
143 let waker = local_waker_from_nonlocal(counter.clone());
144 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
145 assert_eq!(Poll::Pending, fut.as_mut().poll(&waker));
146 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
147 assert_eq!(Poll::Ready(9), fut.as_mut().poll(&waker));
152 ($($fn_name:expr,)*) => { $(
153 test_future_yields_once_then_returns($fn_name);
157 macro_rules! test_with_borrow {
158 ($($fn_name:expr,)*) => { $(
159 test_future_yields_once_then_returns(|x| {
172 async_fn_with_internal_borrow,
175 unsafe { await!(unsafe_async_fn(x)) }
181 async_block_with_borrow_named_lifetime,
182 async_fn_with_borrow,
183 async_fn_with_borrow_named_lifetime,
184 async_fn_with_impl_future_named_lifetime,
187 await!(async_fn_with_named_lifetime_multiple_args(x, x))