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},
21 use std::future::FutureObj;
25 local_waker_from_nonlocal,
32 impl Wake for Counter {
33 fn wake(this: &Arc<Self>) {
34 this.wakes.fetch_add(1, atomic::Ordering::SeqCst);
39 impl Spawn for NoopSpawner {
40 fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> {
45 struct WakeOnceThenComplete(bool);
47 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
49 impl Future for WakeOnceThenComplete {
51 fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
62 fn async_block(x: u8) -> impl Future<Output = u8> {
64 await!(wake_and_yield_once());
69 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
71 await!(wake_and_yield_once());
76 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
79 await!(wake_and_yield_once());
86 fn async_closure(x: u8) -> impl Future<Output = u8> {
87 (async move |x: u8| -> u8 {
88 await!(wake_and_yield_once());
93 async fn async_fn(x: u8) -> u8 {
94 await!(wake_and_yield_once());
98 async fn async_fn_with_borrow(x: &u8) -> u8 {
99 await!(wake_and_yield_once());
103 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
104 await!(wake_and_yield_once());
108 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
110 await!(wake_and_yield_once());
115 async fn async_fn_with_named_lifetime_multiple_args<'a>(x: &'a u8, _y: &'a u8) -> u8 {
116 await!(wake_and_yield_once());
120 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
122 await!(async_fn_with_borrow(&y))
126 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
127 await!(wake_and_yield_once());
138 async fn async_method(x: u8) -> u8 {
140 await!(unsafe_async_fn(x))
145 fn test_future_yields_once_then_returns<F, Fut>(f: F)
147 F: FnOnce(u8) -> Fut,
148 Fut: Future<Output = u8>,
150 let mut fut = Box::pinned(f(9));
151 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
152 let waker = local_waker_from_nonlocal(counter.clone());
153 let spawner = &mut NoopSpawner;
154 let cx = &mut Context::new(&waker, spawner);
156 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
157 assert_eq!(Poll::Pending, fut.as_mut().poll(cx));
158 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
159 assert_eq!(Poll::Ready(9), fut.as_mut().poll(cx));
164 ($($fn_name:expr,)*) => { $(
165 test_future_yields_once_then_returns($fn_name);
169 macro_rules! test_with_borrow {
170 ($($fn_name:expr,)*) => { $(
171 test_future_yields_once_then_returns(|x| {
184 async_fn_with_internal_borrow,
187 unsafe { await!(unsafe_async_fn(x)) }
193 async_block_with_borrow_named_lifetime,
194 async_fn_with_borrow,
195 async_fn_with_borrow_named_lifetime,
196 async_fn_with_impl_future_named_lifetime,
199 await!(async_fn_with_named_lifetime_multiple_args(x, x))