async-await: update to new future/task API (#919)

- Rewrite noop_waker with items from the new API and replaces
  LocalWaker with Waker.

- Bump the minimum required version for `tokio-async-await` to
  1.34.0-nightly.

- `Unpin` was added to std prelude.

- Add `cargo check` to .travis.yml

Fixes: #908
This commit is contained in:
Taiki Endo 2019-02-23 06:30:21 +09:00 committed by Carl Lerche
parent fd22090df8
commit 4985e0c608
14 changed files with 35 additions and 52 deletions

View File

@ -49,6 +49,7 @@ matrix:
script: | script: |
cd tokio-async-await cd tokio-async-await
cargo check --all cargo check --all
cargo check --features async-await-preview
# This runs TSAN against nightly and allows failures to propagate up. # This runs TSAN against nightly and allows failures to propagate up.
- rust: nightly-2018-11-18 - rust: nightly-2018-11-18

View File

@ -9,7 +9,7 @@ guarantees. You are living on the edge here.**
## Usage ## Usage
To use this crate, you need to start with a Rust 2018 edition crate, with rustc To use this crate, you need to start with a Rust 2018 edition crate, with rustc
1.33.0-nightly or later. 1.34.0-nightly or later.
Add this to your `Cargo.toml`: Add this to your `Cargo.toml`:

View File

@ -2,8 +2,8 @@ use futures::{Future, Poll};
use std::future::Future as StdFuture; use std::future::Future as StdFuture;
use std::pin::Pin; use std::pin::Pin;
use std::ptr::NonNull; use std::ptr;
use std::task::{LocalWaker, Poll as StdPoll, UnsafeWake, Waker}; use std::task::{Poll as StdPoll, RawWaker, RawWakerVTable, Waker};
/// Convert an 0.3 `Future` to an 0.1 `Future`. /// Convert an 0.3 `Future` to an 0.1 `Future`.
#[derive(Debug)] #[derive(Debug)]
@ -44,9 +44,9 @@ where
fn poll(&mut self) -> Poll<Item, Error> { fn poll(&mut self) -> Poll<Item, Error> {
use futures::Async::*; use futures::Async::*;
let local_waker = noop_local_waker(); let waker = noop_waker();
let res = self.0.as_mut().poll(&local_waker); let res = self.0.as_mut().poll(&waker);
match res { match res {
StdPoll::Ready(Ok(val)) => Ok(Ready(val)), StdPoll::Ready(Ok(val)) => Ok(Ready(val)),
@ -58,26 +58,26 @@ where
// ===== NoopWaker ===== // ===== NoopWaker =====
struct NoopWaker; fn noop_raw_waker() -> RawWaker {
RawWaker::new(ptr::null(), &NOOP_WAKER_VTABLE)
fn noop_local_waker() -> LocalWaker {
let w: NonNull<NoopWaker> = NonNull::dangling();
unsafe { LocalWaker::new(w) }
} }
fn noop_waker() -> Waker { fn noop_waker() -> Waker {
let w: NonNull<NoopWaker> = NonNull::dangling(); unsafe { Waker::new_unchecked(noop_raw_waker()) }
unsafe { Waker::new(w) }
} }
unsafe impl UnsafeWake for NoopWaker { unsafe fn clone_raw(_data: *const ()) -> RawWaker {
unsafe fn clone_raw(&self) -> Waker { noop_raw_waker()
noop_waker() }
}
unsafe fn drop_raw(&self) {} unsafe fn drop_raw(_data: *const ()) {}
unsafe fn wake(&self) { unsafe fn wake(_data: *const ()) {
unimplemented!("async-await-preview currently only supports futures 0.1. Use the compatibility layer of futures 0.3 instead, if you want to use futures 0.3."); unimplemented!("async-await-preview currently only supports futures 0.1. Use the compatibility layer of futures 0.3 instead, if you want to use futures 0.3.");
}
} }
const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable {
clone: clone_raw,
drop: drop_raw,
wake,
};

View File

@ -1,9 +1,8 @@
use futures::{Async, Future}; use futures::{Async, Future};
use std::future::Future as StdFuture; use std::future::Future as StdFuture;
use std::marker::Unpin;
use std::pin::Pin; use std::pin::Pin;
use std::task::{LocalWaker, Poll as StdPoll}; use std::task::{Poll as StdPoll, Waker};
/// Converts an 0.1 `Future` into an 0.3 `Future`. /// Converts an 0.1 `Future` into an 0.3 `Future`.
#[derive(Debug)] #[derive(Debug)]
@ -54,7 +53,7 @@ where
{ {
type Output = Result<T::Item, T::Error>; type Output = Result<T::Item, T::Error>;
fn poll(mut self: Pin<&mut Self>, _lw: &LocalWaker) -> StdPoll<Self::Output> { fn poll(mut self: Pin<&mut Self>, _waker: &Waker) -> StdPoll<Self::Output> {
use futures::Async::{NotReady, Ready}; use futures::Async::{NotReady, Ready};
// TODO: wire in cx // TODO: wire in cx

View File

@ -2,9 +2,8 @@ use tokio_io::AsyncWrite;
use std::future::Future; use std::future::Future;
use std::io; use std::io;
use std::marker::Unpin;
use std::pin::Pin; use std::pin::Pin;
use std::task::{LocalWaker, Poll}; use std::task::{Poll, Waker};
/// A future used to fully flush an I/O object. /// A future used to fully flush an I/O object.
#[derive(Debug)] #[derive(Debug)]
@ -24,7 +23,7 @@ impl<'a, T: AsyncWrite + ?Sized> Flush<'a, T> {
impl<'a, T: AsyncWrite + ?Sized> Future for Flush<'a, T> { impl<'a, T: AsyncWrite + ?Sized> Future for Flush<'a, T> {
type Output = io::Result<()>; type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, _wx: &LocalWaker) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, _wx: &Waker) -> Poll<Self::Output> {
use crate::compat::forward::convert_poll; use crate::compat::forward::convert_poll;
convert_poll(self.writer.poll_flush()) convert_poll(self.writer.poll_flush())
} }

View File

@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll}; use std::task::{self, Poll};
use std::io; use std::io;
use std::marker::Unpin;
use std::pin::Pin; use std::pin::Pin;
/// A future which can be used to read bytes. /// A future which can be used to read bytes.
@ -26,7 +25,7 @@ impl<'a, T: AsyncRead + ?Sized> Read<'a, T> {
impl<'a, T: AsyncRead + ?Sized> Future for Read<'a, T> { impl<'a, T: AsyncRead + ?Sized> Future for Read<'a, T> {
type Output = io::Result<usize>; type Output = io::Result<usize>;
fn poll(mut self: Pin<&mut Self>, _lw: &task::LocalWaker) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, _waker: &task::Waker) -> Poll<Self::Output> {
use crate::compat::forward::convert_poll; use crate::compat::forward::convert_poll;
let this = &mut *self; let this = &mut *self;

View File

@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll}; use std::task::{self, Poll};
use std::io; use std::io;
use std::marker::Unpin;
use std::mem; use std::mem;
use std::pin::Pin; use std::pin::Pin;
@ -31,7 +30,7 @@ fn eof() -> io::Error {
impl<'a, T: AsyncRead + ?Sized> Future for ReadExact<'a, T> { impl<'a, T: AsyncRead + ?Sized> Future for ReadExact<'a, T> {
type Output = io::Result<()>; type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, _lw: &task::LocalWaker) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, _waker: &task::Waker) -> Poll<Self::Output> {
use crate::compat::forward::convert_poll; use crate::compat::forward::convert_poll;
let this = &mut *self; let this = &mut *self;

View File

@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll}; use std::task::{self, Poll};
use std::io; use std::io;
use std::marker::Unpin;
use std::pin::Pin; use std::pin::Pin;
/// A future used to write data. /// A future used to write data.
@ -26,7 +25,7 @@ impl<'a, T: AsyncWrite + ?Sized> Write<'a, T> {
impl<'a, T: AsyncWrite + ?Sized> Future for Write<'a, T> { impl<'a, T: AsyncWrite + ?Sized> Future for Write<'a, T> {
type Output = io::Result<usize>; type Output = io::Result<usize>;
fn poll(mut self: Pin<&mut Self>, _lw: &task::LocalWaker) -> Poll<io::Result<usize>> { fn poll(mut self: Pin<&mut Self>, _waker: &task::Waker) -> Poll<io::Result<usize>> {
use crate::compat::forward::convert_poll; use crate::compat::forward::convert_poll;
let this = &mut *self; let this = &mut *self;

View File

@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll}; use std::task::{self, Poll};
use std::io; use std::io;
use std::marker::Unpin;
use std::mem; use std::mem;
use std::pin::Pin; use std::pin::Pin;
@ -31,7 +30,7 @@ fn zero_write() -> io::Error {
impl<'a, T: AsyncWrite + ?Sized> Future for WriteAll<'a, T> { impl<'a, T: AsyncWrite + ?Sized> Future for WriteAll<'a, T> {
type Output = io::Result<()>; type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, _lw: &task::LocalWaker) -> Poll<io::Result<()>> { fn poll(mut self: Pin<&mut Self>, _waker: &task::Waker) -> Poll<io::Result<()>> {
use crate::compat::forward::convert_poll; use crate::compat::forward::convert_poll;
let this = &mut *self; let this = &mut *self;

View File

@ -1,11 +1,5 @@
#![cfg(feature = "async-await-preview")] #![cfg(feature = "async-await-preview")]
#![feature( #![feature(rust_2018_preview, async_await, await_macro, futures_api)]
rust_2018_preview,
arbitrary_self_types,
async_await,
await_macro,
futures_api
)]
#![doc(html_root_url = "https://docs.rs/tokio-async-await/0.1.5")] #![doc(html_root_url = "https://docs.rs/tokio-async-await/0.1.5")]
#![deny(missing_docs, missing_debug_implementations)] #![deny(missing_docs, missing_debug_implementations)]
#![cfg_attr(test, deny(warnings))] #![cfg_attr(test, deny(warnings))]

View File

@ -6,8 +6,6 @@ pub use self::send::Send;
use futures::Sink; use futures::Sink;
use std::marker::Unpin;
/// An extension trait which adds utility methods to `Sink` types. /// An extension trait which adds utility methods to `Sink` types.
pub trait SinkExt: Sink { pub trait SinkExt: Sink {
/// Send an item into the sink. /// Send an item into the sink.

View File

@ -3,7 +3,6 @@ use futures::Sink;
use std::future::Future; use std::future::Future;
use std::task::{self, Poll}; use std::task::{self, Poll};
use std::marker::Unpin;
use std::pin::Pin; use std::pin::Pin;
/// Future for the `SinkExt::send_async` combinator, which sends a value to a /// Future for the `SinkExt::send_async` combinator, which sends a value to a
@ -28,7 +27,7 @@ impl<'a, T: Sink + Unpin + ?Sized> Send<'a, T> {
impl<T: Sink + Unpin + ?Sized> Future for Send<'_, T> { impl<T: Sink + Unpin + ?Sized> Future for Send<'_, T> {
type Output = Result<(), T::SinkError>; type Output = Result<(), T::SinkError>;
fn poll(mut self: Pin<&mut Self>, _lw: &task::LocalWaker) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, _waker: &task::Waker) -> Poll<Self::Output> {
use crate::compat::forward::convert_poll; use crate::compat::forward::convert_poll;
use futures::AsyncSink::{NotReady, Ready}; use futures::AsyncSink::{NotReady, Ready};

View File

@ -6,8 +6,6 @@ pub use self::next::Next;
use futures::Stream; use futures::Stream;
use std::marker::Unpin;
/// An extension trait which adds utility methods to `Stream` types. /// An extension trait which adds utility methods to `Stream` types.
pub trait StreamExt: Stream { pub trait StreamExt: Stream {
/// Creates a future that resolves to the next item in the stream. /// Creates a future that resolves to the next item in the stream.

View File

@ -1,9 +1,8 @@
use futures::Stream; use futures::Stream;
use std::future::Future; use std::future::Future;
use std::marker::Unpin;
use std::pin::Pin; use std::pin::Pin;
use std::task::{LocalWaker, Poll}; use std::task::{Poll, Waker};
/// A future of the next element of a stream. /// A future of the next element of a stream.
#[derive(Debug)] #[derive(Debug)]
@ -22,7 +21,7 @@ impl<'a, T: Stream + Unpin> Next<'a, T> {
impl<'a, T: Stream + Unpin> Future for Next<'a, T> { impl<'a, T: Stream + Unpin> Future for Next<'a, T> {
type Output = Option<Result<T::Item, T::Error>>; type Output = Option<Result<T::Item, T::Error>>;
fn poll(mut self: Pin<&mut Self>, _lw: &LocalWaker) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, _waker: &Waker) -> Poll<Self::Output> {
use crate::compat::forward::convert_poll_stream; use crate::compat::forward::convert_poll_stream;
convert_poll_stream(self.stream.poll()) convert_poll_stream(self.stream.poll())