]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/async-await.rs
Rollup merge of #60627 - matklad:test, r=estebank
[rust.git] / src / test / run-pass / async-await.rs
1 // edition:2018
2 // aux-build:arc_wake.rs
3
4 #![feature(async_await)]
5
6 extern crate arc_wake;
7
8 use std::pin::Pin;
9 use std::future::Future;
10 use std::sync::{
11     Arc,
12     atomic::{self, AtomicUsize},
13 };
14 use std::task::{Context, Poll};
15 use arc_wake::ArcWake;
16
17 struct Counter {
18     wakes: AtomicUsize,
19 }
20
21 impl ArcWake for Counter {
22     fn wake(self: Arc<Self>) {
23         Self::wake_by_ref(&self)
24     }
25     fn wake_by_ref(arc_self: &Arc<Self>) {
26         arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
27     }
28 }
29
30 struct WakeOnceThenComplete(bool);
31
32 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
33
34 impl Future for WakeOnceThenComplete {
35     type Output = ();
36     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
37         if self.0 {
38             Poll::Ready(())
39         } else {
40             cx.waker().wake_by_ref();
41             self.0 = true;
42             Poll::Pending
43         }
44     }
45 }
46
47 fn async_block(x: u8) -> impl Future<Output = u8> {
48     async move {
49         wake_and_yield_once().await;
50         x
51     }
52 }
53
54 fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
55     async move {
56         wake_and_yield_once().await;
57         *x
58     }
59 }
60
61 fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
62     async move {
63         let future = async {
64             wake_and_yield_once().await;
65             x
66         };
67         future.await
68     }
69 }
70
71 fn async_closure(x: u8) -> impl Future<Output = u8> {
72     (async move |x: u8| -> u8 {
73         wake_and_yield_once().await;
74         x
75     })(x)
76 }
77
78 async fn async_fn(x: u8) -> u8 {
79     wake_and_yield_once().await;
80     x
81 }
82
83 async fn generic_async_fn<T>(x: T) -> T {
84     wake_and_yield_once().await;
85     x
86 }
87
88 async fn async_fn_with_borrow(x: &u8) -> u8 {
89     wake_and_yield_once().await;
90     *x
91 }
92
93 async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
94     wake_and_yield_once().await;
95     *x
96 }
97
98 fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
99     async move {
100         wake_and_yield_once().await;
101         *x
102     }
103 }
104
105 /* FIXME(cramertj) support when `existential type T<'a, 'b>:;` works
106 async fn async_fn_multiple_args(x: &u8, _y: &u8) -> u8 {
107     await!(wake_and_yield_once());
108     *x
109 }
110 */
111
112 async fn async_fn_multiple_args_named_lifetime<'a>(x: &'a u8, _y: &'a u8) -> u8 {
113     wake_and_yield_once().await;
114     *x
115 }
116
117 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
118     async move {
119         async_fn_with_borrow_named_lifetime(&y).await
120     }
121 }
122
123 unsafe async fn unsafe_async_fn(x: u8) -> u8 {
124     wake_and_yield_once().await;
125     x
126 }
127
128 struct Foo;
129
130 trait Bar {
131     fn foo() {}
132 }
133
134 impl Foo {
135     async fn async_method(x: u8) -> u8 {
136         unsafe {
137             unsafe_async_fn(x).await
138         }
139     }
140 }
141
142 fn test_future_yields_once_then_returns<F, Fut>(f: F)
143 where
144     F: FnOnce(u8) -> Fut,
145     Fut: Future<Output = u8>,
146 {
147     let mut fut = Box::pin(f(9));
148     let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
149     let waker = ArcWake::into_waker(counter.clone());
150     let mut cx = Context::from_waker(&waker);
151     assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
152     assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx));
153     assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
154     assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx));
155 }
156
157 fn main() {
158     macro_rules! test {
159         ($($fn_name:expr,)*) => { $(
160             test_future_yields_once_then_returns($fn_name);
161         )* }
162     }
163
164     macro_rules! test_with_borrow {
165         ($($fn_name:expr,)*) => { $(
166             test_future_yields_once_then_returns(|x| {
167                 async move {
168                     $fn_name(&x).await
169                 }
170             });
171         )* }
172     }
173
174     test! {
175         async_block,
176         async_nonmove_block,
177         async_closure,
178         async_fn,
179         generic_async_fn,
180         async_fn_with_internal_borrow,
181         Foo::async_method,
182         |x| {
183             async move {
184                 unsafe { unsafe_async_fn(x).await }
185             }
186         },
187     }
188     test_with_borrow! {
189         async_block_with_borrow_named_lifetime,
190         async_fn_with_borrow,
191         async_fn_with_borrow_named_lifetime,
192         async_fn_with_impl_future_named_lifetime,
193         |x| {
194             async move {
195                 async_fn_multiple_args_named_lifetime(x, x).await
196             }
197         },
198     }
199 }