2018-06-25 22:04:08 -05:00
|
|
|
// edition:2018
|
2019-02-05 20:32:35 -08:00
|
|
|
// aux-build:arc_wake.rs
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2018-12-17 18:14:07 -08:00
|
|
|
#![feature(arbitrary_self_types, async_await, await_macro, futures_api)]
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2019-02-05 20:32:35 -08:00
|
|
|
extern crate arc_wake;
|
|
|
|
|
2018-09-14 17:40:52 -07:00
|
|
|
use std::pin::Pin;
|
2018-06-06 15:50:59 -07:00
|
|
|
use std::future::Future;
|
|
|
|
use std::sync::{
|
|
|
|
Arc,
|
|
|
|
atomic::{self, AtomicUsize},
|
|
|
|
};
|
|
|
|
use std::task::{
|
2019-02-05 20:32:35 -08:00
|
|
|
Poll, Waker,
|
2018-06-06 15:50:59 -07:00
|
|
|
};
|
2019-02-05 20:32:35 -08:00
|
|
|
use arc_wake::ArcWake;
|
2019-02-03 12:59:51 -08:00
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
struct Counter {
|
|
|
|
wakes: AtomicUsize,
|
|
|
|
}
|
|
|
|
|
2019-02-03 12:59:51 -08:00
|
|
|
impl ArcWake for Counter {
|
|
|
|
fn wake(arc_self: &Arc<Self>) {
|
|
|
|
arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WakeOnceThenComplete(bool);
|
|
|
|
|
|
|
|
fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
|
|
|
|
|
|
|
|
impl Future for WakeOnceThenComplete {
|
|
|
|
type Output = ();
|
2019-02-03 12:59:51 -08:00
|
|
|
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<()> {
|
2018-06-06 15:50:59 -07:00
|
|
|
if self.0 {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
2019-02-03 12:59:51 -08:00
|
|
|
waker.wake();
|
2018-06-06 15:50:59 -07:00
|
|
|
self.0 = true;
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn async_block(x: u8) -> impl Future<Output = u8> {
|
|
|
|
async move {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:01:49 +01:00
|
|
|
fn async_block_with_borrow_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
|
|
|
|
async move {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
*x
|
2018-09-06 22:18:39 +01:00
|
|
|
}
|
2018-09-06 19:01:49 +01:00
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
|
|
|
|
async move {
|
|
|
|
let future = async {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
x
|
|
|
|
};
|
|
|
|
await!(future)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn async_closure(x: u8) -> impl Future<Output = u8> {
|
|
|
|
(async move |x: u8| -> u8 {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
x
|
|
|
|
})(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn async_fn(x: u8) -> u8 {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
2019-03-13 17:42:23 -07:00
|
|
|
async fn generic_async_fn<T>(x: T) -> T {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
async fn async_fn_with_borrow(x: &u8) -> u8 {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
*x
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:01:49 +01:00
|
|
|
async fn async_fn_with_borrow_named_lifetime<'a>(x: &'a u8) -> u8 {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
*x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
|
|
|
|
async move {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
*x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 17:42:23 -07:00
|
|
|
/* FIXME(cramertj) support when `existential type T<'a, 'b>:;` works
|
|
|
|
async fn async_fn_multiple_args(x: &u8, _y: &u8) -> u8 {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
*x
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
async fn async_fn_multiple_args_named_lifetime<'a>(x: &'a u8, _y: &'a u8) -> u8 {
|
2018-09-06 19:01:49 +01:00
|
|
|
await!(wake_and_yield_once());
|
2018-09-06 22:18:39 +01:00
|
|
|
*x
|
2018-09-06 19:01:49 +01:00
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
|
|
|
|
async move {
|
2019-03-13 17:42:23 -07:00
|
|
|
await!(async_fn_with_borrow_named_lifetime(&y))
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 16:49:34 -07:00
|
|
|
unsafe async fn unsafe_async_fn(x: u8) -> u8 {
|
|
|
|
await!(wake_and_yield_once());
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
2018-06-18 21:18:10 -07:00
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
trait Bar {
|
|
|
|
fn foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Foo {
|
2018-06-18 16:49:34 -07:00
|
|
|
async fn async_method(x: u8) -> u8 {
|
|
|
|
unsafe {
|
2018-06-18 21:18:10 -07:00
|
|
|
await!(unsafe_async_fn(x))
|
2018-06-18 16:49:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
fn test_future_yields_once_then_returns<F, Fut>(f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(u8) -> Fut,
|
|
|
|
Fut: Future<Output = u8>,
|
|
|
|
{
|
2018-12-18 10:25:02 -08:00
|
|
|
let mut fut = Box::pin(f(9));
|
2018-06-06 15:50:59 -07:00
|
|
|
let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
|
2019-02-03 12:59:51 -08:00
|
|
|
let waker = ArcWake::into_waker(counter.clone());
|
2018-06-06 15:50:59 -07:00
|
|
|
assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
|
2018-09-18 17:50:32 -07:00
|
|
|
assert_eq!(Poll::Pending, fut.as_mut().poll(&waker));
|
2018-06-06 15:50:59 -07:00
|
|
|
assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
|
2018-09-18 17:50:32 -07:00
|
|
|
assert_eq!(Poll::Ready(9), fut.as_mut().poll(&waker));
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
macro_rules! test {
|
2018-09-06 19:01:49 +01:00
|
|
|
($($fn_name:expr,)*) => { $(
|
2018-06-06 15:50:59 -07:00
|
|
|
test_future_yields_once_then_returns($fn_name);
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:01:49 +01:00
|
|
|
macro_rules! test_with_borrow {
|
|
|
|
($($fn_name:expr,)*) => { $(
|
2018-09-06 22:18:39 +01:00
|
|
|
test_future_yields_once_then_returns(|x| {
|
2018-09-06 19:01:49 +01:00
|
|
|
async move {
|
|
|
|
await!($fn_name(&x))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
test! {
|
|
|
|
async_block,
|
|
|
|
async_nonmove_block,
|
|
|
|
async_closure,
|
|
|
|
async_fn,
|
2019-03-13 17:42:23 -07:00
|
|
|
generic_async_fn,
|
2018-06-06 15:50:59 -07:00
|
|
|
async_fn_with_internal_borrow,
|
2018-10-04 14:44:46 +02:00
|
|
|
Foo::async_method,
|
2018-09-06 22:18:39 +01:00
|
|
|
|x| {
|
|
|
|
async move {
|
|
|
|
unsafe { await!(unsafe_async_fn(x)) }
|
|
|
|
}
|
|
|
|
},
|
2018-09-06 19:01:49 +01:00
|
|
|
}
|
|
|
|
test_with_borrow! {
|
|
|
|
async_block_with_borrow_named_lifetime,
|
|
|
|
async_fn_with_borrow,
|
|
|
|
async_fn_with_borrow_named_lifetime,
|
|
|
|
async_fn_with_impl_future_named_lifetime,
|
2018-09-06 22:18:39 +01:00
|
|
|
|x| {
|
2018-09-06 19:01:49 +01:00
|
|
|
async move {
|
2019-03-13 17:42:23 -07:00
|
|
|
await!(async_fn_multiple_args_named_lifetime(x, x))
|
2018-09-06 19:01:49 +01:00
|
|
|
}
|
|
|
|
},
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
}
|