Skip to content

Commit

Permalink
async-await: update to new future/task API (#919)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
taiki-e authored and carllerche committed Feb 22, 2019
1 parent fd22090 commit 4985e0c
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 50 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ matrix:
script: |
cd tokio-async-await
cargo check --all
cargo check --features async-await-preview
# This runs TSAN against nightly and allows failures to propagate up.
- rust: nightly-2018-11-18
Expand Down
2 changes: 1 addition & 1 deletion tokio-async-await/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ guarantees. You are living on the edge here.**
## Usage

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`:

Expand Down
38 changes: 19 additions & 19 deletions tokio-async-await/src/compat/backward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use futures::{Future, Poll};

use std::future::Future as StdFuture;
use std::pin::Pin;
use std::ptr::NonNull;
use std::task::{LocalWaker, Poll as StdPoll, UnsafeWake, Waker};
use std::ptr;
use std::task::{Poll as StdPoll, RawWaker, RawWakerVTable, Waker};

/// Convert an 0.3 `Future` to an 0.1 `Future`.
#[derive(Debug)]
Expand Down Expand Up @@ -44,9 +44,9 @@ where
fn poll(&mut self) -> Poll<Item, Error> {
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 {
StdPoll::Ready(Ok(val)) => Ok(Ready(val)),
Expand All @@ -58,26 +58,26 @@ where

// ===== NoopWaker =====

struct NoopWaker;

fn noop_local_waker() -> LocalWaker {
let w: NonNull<NoopWaker> = NonNull::dangling();
unsafe { LocalWaker::new(w) }
fn noop_raw_waker() -> RawWaker {
RawWaker::new(ptr::null(), &NOOP_WAKER_VTABLE)
}

fn noop_waker() -> Waker {
let w: NonNull<NoopWaker> = NonNull::dangling();
unsafe { Waker::new(w) }
unsafe { Waker::new_unchecked(noop_raw_waker()) }
}

unsafe impl UnsafeWake for NoopWaker {
unsafe fn clone_raw(&self) -> Waker {
noop_waker()
}
unsafe fn clone_raw(_data: *const ()) -> RawWaker {
noop_raw_waker()
}

unsafe fn drop_raw(&self) {}
unsafe fn drop_raw(_data: *const ()) {}

unsafe fn wake(&self) {
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.");
}
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.");
}

const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable {
clone: clone_raw,
drop: drop_raw,
wake,
};
5 changes: 2 additions & 3 deletions tokio-async-await/src/compat/forward.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use futures::{Async, Future};

use std::future::Future as StdFuture;
use std::marker::Unpin;
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`.
#[derive(Debug)]
Expand Down Expand Up @@ -54,7 +53,7 @@ where
{
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};

// TODO: wire in cx
Expand Down
5 changes: 2 additions & 3 deletions tokio-async-await/src/io/flush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use tokio_io::AsyncWrite;

use std::future::Future;
use std::io;
use std::marker::Unpin;
use std::pin::Pin;
use std::task::{LocalWaker, Poll};
use std::task::{Poll, Waker};

/// A future used to fully flush an I/O object.
#[derive(Debug)]
Expand All @@ -24,7 +23,7 @@ impl<'a, T: AsyncWrite + ?Sized> Flush<'a, T> {
impl<'a, T: AsyncWrite + ?Sized> Future for Flush<'a, T> {
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;
convert_poll(self.writer.poll_flush())
}
Expand Down
3 changes: 1 addition & 2 deletions tokio-async-await/src/io/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll};

use std::io;
use std::marker::Unpin;
use std::pin::Pin;

/// A future which can be used to read bytes.
Expand All @@ -26,7 +25,7 @@ impl<'a, T: AsyncRead + ?Sized> Read<'a, T> {
impl<'a, T: AsyncRead + ?Sized> Future for Read<'a, T> {
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;

let this = &mut *self;
Expand Down
3 changes: 1 addition & 2 deletions tokio-async-await/src/io/read_exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll};

use std::io;
use std::marker::Unpin;
use std::mem;
use std::pin::Pin;

Expand All @@ -31,7 +30,7 @@ fn eof() -> io::Error {
impl<'a, T: AsyncRead + ?Sized> Future for ReadExact<'a, T> {
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;

let this = &mut *self;
Expand Down
3 changes: 1 addition & 2 deletions tokio-async-await/src/io/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll};

use std::io;
use std::marker::Unpin;
use std::pin::Pin;

/// A future used to write data.
Expand All @@ -26,7 +25,7 @@ impl<'a, T: AsyncWrite + ?Sized> Write<'a, T> {
impl<'a, T: AsyncWrite + ?Sized> Future for Write<'a, T> {
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;

let this = &mut *self;
Expand Down
3 changes: 1 addition & 2 deletions tokio-async-await/src/io/write_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::future::Future;
use std::task::{self, Poll};

use std::io;
use std::marker::Unpin;
use std::mem;
use std::pin::Pin;

Expand All @@ -31,7 +30,7 @@ fn zero_write() -> io::Error {
impl<'a, T: AsyncWrite + ?Sized> Future for WriteAll<'a, T> {
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;

let this = &mut *self;
Expand Down
8 changes: 1 addition & 7 deletions tokio-async-await/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#![cfg(feature = "async-await-preview")]
#![feature(
rust_2018_preview,
arbitrary_self_types,
async_await,
await_macro,
futures_api
)]
#![feature(rust_2018_preview, async_await, await_macro, futures_api)]
#![doc(html_root_url = "https://docs.rs/tokio-async-await/0.1.5")]
#![deny(missing_docs, missing_debug_implementations)]
#![cfg_attr(test, deny(warnings))]
Expand Down
2 changes: 0 additions & 2 deletions tokio-async-await/src/sink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub use self::send::Send;

use futures::Sink;

use std::marker::Unpin;

/// An extension trait which adds utility methods to `Sink` types.
pub trait SinkExt: Sink {
/// Send an item into the sink.
Expand Down
3 changes: 1 addition & 2 deletions tokio-async-await/src/sink/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use futures::Sink;
use std::future::Future;
use std::task::{self, Poll};

use std::marker::Unpin;
use std::pin::Pin;

/// Future for the `SinkExt::send_async` combinator, which sends a value to a
Expand All @@ -28,7 +27,7 @@ impl<'a, T: Sink + Unpin + ?Sized> Send<'a, T> {
impl<T: Sink + Unpin + ?Sized> Future for Send<'_, T> {
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 futures::AsyncSink::{NotReady, Ready};

Expand Down
2 changes: 0 additions & 2 deletions tokio-async-await/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub use self::next::Next;

use futures::Stream;

use std::marker::Unpin;

/// An extension trait which adds utility methods to `Stream` types.
pub trait StreamExt: Stream {
/// Creates a future that resolves to the next item in the stream.
Expand Down
5 changes: 2 additions & 3 deletions tokio-async-await/src/stream/next.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use futures::Stream;

use std::future::Future;
use std::marker::Unpin;
use std::pin::Pin;
use std::task::{LocalWaker, Poll};
use std::task::{Poll, Waker};

/// A future of the next element of a stream.
#[derive(Debug)]
Expand All @@ -22,7 +21,7 @@ impl<'a, T: Stream + Unpin> Next<'a, T> {
impl<'a, T: Stream + Unpin> Future for Next<'a, T> {
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;

convert_poll_stream(self.stream.poll())
Expand Down

0 comments on commit 4985e0c

Please sign in to comment.