3 // revisions: default nomiropt
4 //[nomiropt]compile-flags: -Z mir-opt-level=0
9 // aux-build:arc_wake.rs
11 extern crate arc_wake
;
14 use std
::future
::Future
;
17 atomic
::{self, AtomicUsize}
,
19 use std
::task
::{Context, Poll}
;
20 use arc_wake
::ArcWake
;
26 impl ArcWake
for Counter
{
27 fn wake(self: Arc
<Self>) {
28 Self::wake_by_ref(&self)
30 fn wake_by_ref(arc_self
: &Arc
<Self>) {
31 arc_self
.wakes
.fetch_add(1, atomic
::Ordering
::SeqCst
);
35 struct WakeOnceThenComplete(bool
);
37 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
39 impl Future
for WakeOnceThenComplete
{
41 fn poll(mut self: Pin
<&mut Self>, cx
: &mut Context
<'_
>) -> Poll
<()> {
45 cx
.waker().wake_by_ref();
52 fn async_block(x
: u8) -> impl Future
<Output
= u8> {
54 wake_and_yield_once().await
;
59 fn async_block_with_borrow_named_lifetime
<'a
>(x
: &'a
u8) -> impl Future
<Output
= u8> + 'a
{
61 wake_and_yield_once().await
;
66 fn async_nonmove_block(x
: u8) -> impl Future
<Output
= u8> {
69 wake_and_yield_once().await
;
76 // see async-closure.rs for async_closure + async_closure_in_unsafe_block
78 async
fn async_fn(x
: u8) -> u8 {
79 wake_and_yield_once().await
;
83 async
fn generic_async_fn
<T
>(x
: T
) -> T
{
84 wake_and_yield_once().await
;
88 async
fn async_fn_with_borrow(x
: &u8) -> u8 {
89 wake_and_yield_once().await
;
93 async
fn async_fn_with_borrow_named_lifetime
<'a
>(x
: &'a
u8) -> u8 {
94 wake_and_yield_once().await
;
98 fn async_fn_with_impl_future_named_lifetime
<'a
>(x
: &'a
u8) -> impl Future
<Output
= u8> + 'a
{
100 wake_and_yield_once().await
;
105 async
fn async_fn_multiple_args(x
: &u8, _y
: &u8) -> u8 {
106 wake_and_yield_once().await
;
110 async
fn async_fn_multiple_args_named_lifetime
<'a
>(x
: &'a
u8, _y
: &'a
u8) -> u8 {
111 wake_and_yield_once().await
;
115 fn async_fn_with_internal_borrow(y
: u8) -> impl Future
<Output
= u8> {
117 async_fn_with_borrow_named_lifetime(&y
).await
121 async
unsafe fn unsafe_async_fn(x
: u8) -> u8 {
122 wake_and_yield_once().await
;
126 unsafe fn unsafe_fn(x
: u8) -> u8 {
130 fn async_block_in_unsafe_block(x
: u8) -> impl Future
<Output
= u8> {
133 unsafe_fn(unsafe_async_fn(x
).await
)
145 async
fn async_assoc_item(x
: u8) -> u8 {
147 unsafe_async_fn(x
).await
151 async
unsafe fn async_unsafe_assoc_item(x
: u8) -> u8 {
152 unsafe_async_fn(x
).await
156 fn test_future_yields_once_then_returns
<F
, Fut
>(f
: F
)
158 F
: FnOnce(u8) -> Fut
,
159 Fut
: Future
<Output
= u8>,
161 let mut fut
= Box
::pin(f(9));
162 let counter
= Arc
::new(Counter { wakes: AtomicUsize::new(0) }
);
163 let waker
= ArcWake
::into_waker(counter
.clone());
164 let mut cx
= Context
::from_waker(&waker
);
165 assert_eq
!(0, counter
.wakes
.load(atomic
::Ordering
::SeqCst
));
166 assert_eq
!(Poll
::Pending
, fut
.as_mut().poll(&mut cx
));
167 assert_eq
!(1, counter
.wakes
.load(atomic
::Ordering
::SeqCst
));
168 assert_eq
!(Poll
::Ready(9), fut
.as_mut().poll(&mut cx
));
173 ($
($fn_name
:expr
,)*) => { $
(
174 test_future_yields_once_then_returns($fn_name
);
178 macro_rules
! test_with_borrow
{
179 ($
($fn_name
:expr
,)*) => { $
(
180 test_future_yields_once_then_returns(|x
| {
193 async_fn_with_internal_borrow
,
194 async_block_in_unsafe_block
,
195 Foo
::async_assoc_item
,
198 unsafe { unsafe_async_fn(x).await }
203 unsafe { Foo::async_unsafe_assoc_item(x).await }
208 async_block_with_borrow_named_lifetime
,
209 async_fn_with_borrow
,
210 async_fn_with_borrow_named_lifetime
,
211 async_fn_with_impl_future_named_lifetime
,
214 async_fn_multiple_args_named_lifetime(x
, x
).await