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)]
17 use std::future::Future;
20 atomic::{self, AtomicUsize},
22 use std::future::FutureObj;
26 local_waker_from_nonlocal,
33 impl Wake for Counter {
34 fn wake(this: &Arc<Self>) {
35 this.wakes.fetch_add(1, atomic::Ordering::SeqCst);
40 impl Spawn for NoopSpawner {
41 fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> {
46 struct WakeOnceThenComplete(bool);
48 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
50 impl Future for WakeOnceThenComplete {
52 fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<()> {
63 fn async_block(x: u8) -> impl Future<Output = u8> {
65 await!(wake_and_yield_once());
70 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
72 await!(wake_and_yield_once());
77 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
80 await!(wake_and_yield_once());
87 fn async_closure(x: u8) -> impl Future<Output = u8> {
88 (async move |x: u8| -> u8 {
89 await!(wake_and_yield_once());
94 async fn async_fn(x: u8) -> u8 {
95 await!(wake_and_yield_once());
99 async fn async_fn_with_borrow(x: &u8) -> u8 {
100 await!(wake_and_yield_once());
104 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
105 await!(wake_and_yield_once());
109 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
111 await!(wake_and_yield_once());
116 async fn async_fn_with_named_lifetime_multiple_args<'a>(x: &'a u8, _y: &'a u8) -> u8 {
117 await!(wake_and_yield_once());
121 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
123 await!(async_fn_with_borrow(&y))
127 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
128 await!(wake_and_yield_once());
139 async fn async_method(x: u8) -> u8 {
141 await!(unsafe_async_fn(x))
146 fn test_future_yields_once_then_returns<F, Fut>(f: F)
148 F: FnOnce(u8) -> Fut,
149 Fut: Future<Output = u8>,
151 let mut fut = PinBox::new(f(9));
152 let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
153 let waker = local_waker_from_nonlocal(counter.clone());
154 let spawner = &mut NoopSpawner;
155 let cx = &mut Context::new(&waker, spawner);
157 assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
158 assert_eq!(Poll::Pending, fut.as_pin_mut().poll(cx));
159 assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
160 assert_eq!(Poll::Ready(9), fut.as_pin_mut().poll(cx));
165 ($($fn_name:expr,)*) => { $(
166 test_future_yields_once_then_returns($fn_name);
170 macro_rules! test_with_borrow {
171 ($($fn_name:expr,)*) => { $(
172 test_future_yields_once_then_returns(|x| {
185 async_fn_with_internal_borrow,
188 unsafe { await!(unsafe_async_fn(x)) }
194 async_block_with_borrow_named_lifetime,
195 async_fn_with_borrow,
196 async_fn_with_borrow_named_lifetime,
197 async_fn_with_impl_future_named_lifetime,
200 await!(async_fn_with_named_lifetime_multiple_args(x, x))