Cleanup and fix method resolution issue

This commit is contained in:
Taylor Cramer 2018-09-14 17:40:52 -07:00
parent 974bdc80fe
commit 3ec1810e32
6 changed files with 117 additions and 73 deletions

View file

@ -12,8 +12,7 @@
#![feature(arbitrary_self_types, async_await, await_macro, futures_api, pin)]
use std::pin::PinBox;
use std::pin::PinMut;
use std::pin::Pin;
use std::future::Future;
use std::sync::{
Arc,
@ -49,7 +48,7 @@ fn wake_and_yield_once() -> WakeOnceThenComplete { WakeOnceThenComplete(false) }
impl Future for WakeOnceThenComplete {
type Output = ();
fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<()> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
if self.0 {
Poll::Ready(())
} else {
@ -124,16 +123,16 @@ where
F: FnOnce(u8) -> Fut,
Fut: Future<Output = u8>,
{
let mut fut = PinBox::new(f(9));
let mut fut = Box::pinned(f(9));
let counter = Arc::new(Counter { wakes: AtomicUsize::new(0) });
let waker = local_waker_from_nonlocal(counter.clone());
let spawner = &mut NoopSpawner;
let cx = &mut Context::new(&waker, spawner);
assert_eq!(0, counter.wakes.load(atomic::Ordering::SeqCst));
assert_eq!(Poll::Pending, fut.as_pin_mut().poll(cx));
assert_eq!(Poll::Pending, fut.as_mut().poll(cx));
assert_eq!(1, counter.wakes.load(atomic::Ordering::SeqCst));
assert_eq!(Poll::Ready(9), fut.as_pin_mut().poll(cx));
assert_eq!(Poll::Ready(9), fut.as_mut().poll(cx));
}
fn main() {

View file

@ -11,9 +11,8 @@
#![feature(arbitrary_self_types, futures_api, pin)]
#![allow(unused)]
use std::pin::PinBox;
use std::future::Future;
use std::pin::PinMut;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::{
Arc,
@ -54,12 +53,12 @@ struct MyFuture;
impl Future for MyFuture {
type Output = ();
fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
// Ensure all the methods work appropriately
cx.waker().wake();
cx.waker().wake();
cx.local_waker().wake();
cx.spawner().spawn_obj(PinBox::new(MyFuture).into()).unwrap();
cx.spawner().spawn_obj(Box::pinned(MyFuture).into()).unwrap();
Poll::Ready(())
}
}
@ -72,7 +71,7 @@ fn test_local_waker() {
let waker = unsafe { local_waker(counter.clone()) };
let spawner = &mut NoopSpawner;
let cx = &mut Context::new(&waker, spawner);
assert_eq!(Poll::Ready(()), PinMut::new(&mut MyFuture).poll(cx));
assert_eq!(Poll::Ready(()), Pin::new(&mut MyFuture).poll(cx));
assert_eq!(1, counter.local_wakes.load(atomic::Ordering::SeqCst));
assert_eq!(2, counter.nonlocal_wakes.load(atomic::Ordering::SeqCst));
}
@ -85,7 +84,7 @@ fn test_local_as_nonlocal_waker() {
let waker: LocalWaker = local_waker_from_nonlocal(counter.clone());
let spawner = &mut NoopSpawner;
let cx = &mut Context::new(&waker, spawner);
assert_eq!(Poll::Ready(()), PinMut::new(&mut MyFuture).poll(cx));
assert_eq!(Poll::Ready(()), Pin::new(&mut MyFuture).poll(cx));
assert_eq!(0, counter.local_wakes.load(atomic::Ordering::SeqCst));
assert_eq!(3, counter.nonlocal_wakes.load(atomic::Ordering::SeqCst));
}