6 // aux-build:arc_wake.rs
11 use std
::future
::Future
;
14 atomic
::{self, AtomicUsize}
,
16 use std
::task
::{Context, Poll}
;
17 use arc_wake
::ArcWake
;
23 impl ArcWake
for Counter
{
24 fn wake(self: Arc
<Self>) {
25 Self::wake_by_ref(&self)
27 fn wake_by_ref(arc_self
: &Arc
<Self>) {
28 arc_self
.wakes
.fetch_add(1, atomic
::Ordering
::SeqCst
);
32 struct WakeOnceThenComplete(bool
);
34 fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
36 impl Future
for WakeOnceThenComplete
{
38 fn poll(mut self: Pin
<&mut Self>, cx
: &mut Context
<'_
>) -> Poll
<()> {
42 cx
.waker().wake_by_ref();
49 fn async_block(x
: u8) -> impl Future
<Output
= u8> {
51 wake_and_yield_once().await
;
56 fn async_block_with_borrow_named_lifetime
<'a
>(x
: &'a
u8) -> impl Future
<Output
= u8> + 'a
{
58 wake_and_yield_once().await
;
63 fn async_nonmove_block(x
: u8) -> impl Future
<Output
= u8> {
66 wake_and_yield_once().await
;
73 // see async-closure.rs for async_closure + async_closure_in_unsafe_block
75 async
fn async_fn(x
: u8) -> u8 {
76 wake_and_yield_once().await
;
80 async
fn generic_async_fn
<T
>(x
: T
) -> T
{
81 wake_and_yield_once().await
;
85 async
fn async_fn_with_borrow(x
: &u8) -> u8 {
86 wake_and_yield_once().await
;
90 async
fn async_fn_with_borrow_named_lifetime
<'a
>(x
: &'a
u8) -> u8 {
91 wake_and_yield_once().await
;
95 fn async_fn_with_impl_future_named_lifetime
<'a
>(x
: &'a
u8) -> impl Future
<Output
= u8> + 'a
{
97 wake_and_yield_once().await
;
102 async
fn async_fn_multiple_args(x
: &u8, _y
: &u8) -> u8 {
103 wake_and_yield_once().await
;
107 async
fn async_fn_multiple_args_named_lifetime
<'a
>(x
: &'a
u8, _y
: &'a
u8) -> u8 {
108 wake_and_yield_once().await
;
112 fn async_fn_with_internal_borrow(y
: u8) -> impl Future
<Output
= u8> {
114 async_fn_with_borrow_named_lifetime(&y
).await
118 async
unsafe fn unsafe_async_fn(x
: u8) -> u8 {
119 wake_and_yield_once().await
;
123 unsafe fn unsafe_fn(x
: u8) -> u8 {
127 fn async_block_in_unsafe_block(x
: u8) -> impl Future
<Output
= u8> {
130 unsafe_fn(unsafe_async_fn(x
).await
)
142 async
fn async_assoc_item(x
: u8) -> u8 {
144 unsafe_async_fn(x
).await
148 async
unsafe fn async_unsafe_assoc_item(x
: u8) -> u8 {
149 unsafe_async_fn(x
).await
153 fn test_future_yields_once_then_returns
<F
, Fut
>(f
: F
)
155 F
: FnOnce(u8) -> Fut
,
156 Fut
: Future
<Output
= u8>,
158 let mut fut
= Box
::pin(f(9));
159 let counter
= Arc
::new(Counter { wakes: AtomicUsize::new(0) }
);
160 let waker
= ArcWake
::into_waker(counter
.clone());
161 let mut cx
= Context
::from_waker(&waker
);
162 assert_eq
!(0, counter
.wakes
.load(atomic
::Ordering
::SeqCst
));
163 assert_eq
!(Poll
::Pending
, fut
.as_mut().poll(&mut cx
));
164 assert_eq
!(1, counter
.wakes
.load(atomic
::Ordering
::SeqCst
));
165 assert_eq
!(Poll
::Ready(9), fut
.as_mut().poll(&mut cx
));
170 ($
($fn_name
:expr
,)*) => { $
(
171 test_future_yields_once_then_returns($fn_name
);
175 macro_rules
! test_with_borrow
{
176 ($
($fn_name
:expr
,)*) => { $
(
177 test_future_yields_once_then_returns(|x
| {
190 async_fn_with_internal_borrow
,
191 async_block_in_unsafe_block
,
192 Foo
::async_assoc_item
,
195 unsafe { unsafe_async_fn(x).await }
200 unsafe { Foo::async_unsafe_assoc_item(x).await }
205 async_block_with_borrow_named_lifetime
,
206 async_fn_with_borrow
,
207 async_fn_with_borrow_named_lifetime
,
208 async_fn_with_impl_future_named_lifetime
,
211 async_fn_multiple_args_named_lifetime(x
, x
).await