]> git.lizzy.rs Git - rust.git/blobdiff - src/test/run-pass/async-await.rs
Stabilize futures_api
[rust.git] / src / test / run-pass / async-await.rs
index 1843feed927a23ede730677397cdf7196c78b8a8..e1b4328debd9a8918e7bea0c3e4aa16326bc31a9 100644 (file)
@@ -1,7 +1,7 @@
 // edition:2018
 // aux-build:arc_wake.rs
 
-#![feature(arbitrary_self_types, async_await, await_macro, futures_api)]
+#![feature(async_await, await_macro)]
 
 extern crate arc_wake;
 
@@ -11,9 +11,7 @@
     Arc,
     atomic::{self, AtomicUsize},
 };
-use std::task::{
-    Poll, Waker,
-};
+use std::task::{Context, Poll};
 use arc_wake::ArcWake;
 
 struct Counter {
@@ -21,7 +19,10 @@ struct Counter {
 }
 
 impl ArcWake for Counter {
-    fn wake(arc_self: &Arc<Self>) {
+    fn wake(self: Arc<Self>) {
+        Self::wake_by_ref(&self)
+    }
+    fn wake_by_ref(arc_self: &Arc<Self>) {
         arc_self.wakes.fetch_add(1, atomic::Ordering::SeqCst);
     }
 }
@@ -32,11 +33,11 @@ fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
 
 impl Future for WakeOnceThenComplete {
     type Output = ();
-    fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<()> {
+    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
         if self.0 {
             Poll::Ready(())
         } else {
-            waker.wake();
+            cx.waker().wake_by_ref();
             self.0 = true;
             Poll::Pending
         }
@@ -79,6 +80,11 @@ async fn async_fn(x: u8) -> u8 {
     x
 }
 
+async fn generic_async_fn<T>(x: T) -> T {
+    await!(wake_and_yield_once());
+    x
+}
+
 async fn async_fn_with_borrow(x: &u8) -> u8 {
     await!(wake_and_yield_once());
     *x
@@ -96,14 +102,21 @@ fn async_fn_with_impl_future_named_lifetime<'a>(x: &'a u8) -> impl Future<Output
     }
 }
 
-async fn async_fn_with_named_lifetime_multiple_args<'a>(x: &'a u8, _y: &'a u8) -> u8 {
+/* 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 {
     await!(wake_and_yield_once());
     *x
 }
 
 fn async_fn_with_internal_borrow(y: u8) -> impl Future<Output = u8> {
     async move {
-        await!(async_fn_with_borrow(&y))
+        await!(async_fn_with_borrow_named_lifetime(&y))
     }
 }
 
@@ -134,10 +147,11 @@ fn test_future_yields_once_then_returns<F, Fut>(f: F)
     let mut fut = Box::pin(f(9));
     let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
     let waker = ArcWake::into_waker(counter.clone());
+    let mut cx = Context::from_waker(&waker);
     assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
-    assert_eq!(Poll::Pending, fut.as_mut().poll(&waker));
+    assert_eq!(Poll::Pending, fut.as_mut().poll(&mut cx));
     assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
-    assert_eq!(Poll::Ready(9), fut.as_mut().poll(&waker));
+    assert_eq!(Poll::Ready(9), fut.as_mut().poll(&mut cx));
 }
 
 fn main() {
@@ -162,6 +176,7 @@ macro_rules! test_with_borrow {
         async_nonmove_block,
         async_closure,
         async_fn,
+        generic_async_fn,
         async_fn_with_internal_borrow,
         Foo::async_method,
         |x| {
@@ -170,7 +185,6 @@ macro_rules! test_with_borrow {
             }
         },
     }
-
     test_with_borrow! {
         async_block_with_borrow_named_lifetime,
         async_fn_with_borrow,
@@ -178,7 +192,7 @@ macro_rules! test_with_borrow {
         async_fn_with_impl_future_named_lifetime,
         |x| {
             async move {
-                await!(async_fn_with_named_lifetime_multiple_args(x, x))
+                await!(async_fn_multiple_args_named_lifetime(x, x))
             }
         },
     }