From b021e63c4822db60af72177557fa5408af3bf5d1 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 30 Apr 2019 12:05:30 +0900 Subject: [PATCH] Use std::io::IoSlice[Mut] on AsyncRead/AsyncWrite Also, this renames `poll_vectored_*` to `poll_*_vectored` to make it in the same order as std. --- .travis.yml | 2 +- README.md | 2 +- futures-io/Cargo.toml | 3 +- futures-io/src/lib.rs | 55 +++++++++++++++++++++----------- futures-util/src/io/allow_std.rs | 24 ++++++++++++-- futures-util/src/io/mod.rs | 2 +- futures-util/src/io/split.rs | 10 +++--- futures/src/lib.rs | 3 +- 8 files changed, 68 insertions(+), 33 deletions(-) diff --git a/.travis.yml b/.travis.yml index f882d4d437..33f0def8ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ matrix: # When updating this, the reminder to update the minimum required version in README.md. - name: cargo test (minimum required version) - rust: nightly-2019-04-25 + rust: nightly-2019-04-30 - name: cargo clippy rust: nightly diff --git a/README.md b/README.md index 002e6a3878..311283701e 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Now, you can use futures-rs: use futures::future::Future; // Note: It's not `futures_preview` ``` -The current version of futures-rs requires Rust nightly 2019-04-25 or later. +The current version of futures-rs requires Rust nightly 2019-04-30 or later. ### Feature `std` diff --git a/futures-io/Cargo.toml b/futures-io/Cargo.toml index 011e951566..29a5c6bf3f 100644 --- a/futures-io/Cargo.toml +++ b/futures-io/Cargo.toml @@ -15,12 +15,11 @@ The `AsyncRead` and `AsyncWrite` traits for the futures-rs library. name = "futures_io" [features] -std = ["futures-core-preview/std", "iovec"] +std = ["futures-core-preview/std"] default = ["std"] [dependencies] futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.15", default-features = false } -iovec = { version = "0.1", optional = true } [dev-dependencies] futures-preview = { path = "../futures", version = "=0.3.0-alpha.15" } diff --git a/futures-io/src/lib.rs b/futures-io/src/lib.rs index 9d761e5699..07d3dd290c 100644 --- a/futures-io/src/lib.rs +++ b/futures-io/src/lib.rs @@ -20,14 +20,13 @@ mod if_std { use std::pin::Pin; use std::ptr; - // Re-export IoVec for convenience - pub use iovec::IoVec; - // Re-export some types from `std::io` so that users don't have to deal // with conflicts when `use`ing `futures::io` and `std::io`. pub use self::StdIo::Error as Error; pub use self::StdIo::ErrorKind as ErrorKind; pub use self::StdIo::Result as Result; + pub use self::StdIo::IoSlice as IoSlice; + pub use self::StdIo::IoSliceMut as IoSliceMut; pub use self::StdIo::SeekFrom as SeekFrom; /// A type used to conditionally initialize buffers passed to `AsyncRead` @@ -112,7 +111,7 @@ mod if_std { fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll>; - /// Attempt to read from the `AsyncRead` into `vec` using vectored + /// Attempt to read from the `AsyncRead` into `bufs` using vectored /// IO operations. /// /// This method is similar to `poll_read`, but allows data to be read @@ -125,7 +124,7 @@ mod if_std { /// `cx.waker().wake_by_ref()`) to receive a notification when the object becomes /// readable or is closed. /// By default, this method delegates to using `poll_read` on the first - /// buffer in `vec`. Objects which support vectored IO should override + /// buffer in `bufs`. Objects which support vectored IO should override /// this method. /// /// # Implementation @@ -134,7 +133,7 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec]) + fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll> { if let Some(ref mut first_iovec) = vec.get_mut(0) { @@ -172,7 +171,7 @@ mod if_std { fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll>; - /// Attempt to write bytes from `vec` into the object using vectored + /// Attempt to write bytes from `bufs` into the object using vectored /// IO operations. /// /// This method is similar to `poll_write`, but allows data from multiple buffers to be written @@ -186,7 +185,7 @@ mod if_std { /// readable or is closed. /// /// By default, this method delegates to using `poll_write` on the first - /// buffer in `vec`. Objects which support vectored IO should override + /// buffer in `bufs`. Objects which support vectored IO should override /// this method. /// /// # Implementation @@ -195,13 +194,13 @@ mod if_std { /// `Interrupted`. Implementations must convert `WouldBlock` into /// `Poll::Pending` and either internally retry or convert /// `Interrupted` into another error kind. - fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec]) + fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll> { - if let Some(ref first_iovec) = vec.get(0) { + if let Some(ref first_iovec) = bufs.get(0) { self.poll_write(cx, &*first_iovec) } else { - // `vec` is empty. + // `bufs` is empty. Poll::Ready(Ok(0)) } } @@ -335,10 +334,10 @@ mod if_std { Pin::new(&mut **self).poll_read(cx, buf) } - fn poll_vectored_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec]) + fn poll_read_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll> { - Pin::new(&mut **self).poll_vectored_read(cx, vec) + Pin::new(&mut **self).poll_read_vectored(cx, vec) } } } @@ -366,10 +365,10 @@ mod if_std { Pin::get_mut(self).as_mut().poll_read(cx, buf) } - fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec]) + fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll> { - Pin::get_mut(self).as_mut().poll_vectored_read(cx, vec) + Pin::get_mut(self).as_mut().poll_read_vectored(cx, vec) } } @@ -386,6 +385,12 @@ mod if_std { { Poll::Ready(StdIo::Read::read(&mut *self, buf)) } + + fn poll_read_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) + -> Poll> + { + Poll::Ready(StdIo::Read::read_vectored(&mut *self, vec)) + } } } @@ -413,10 +418,10 @@ mod if_std { Pin::new(&mut **self).poll_write(cx, buf) } - fn poll_vectored_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec]) + fn poll_write_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll> { - Pin::new(&mut **self).poll_vectored_write(cx, vec) + Pin::new(&mut **self).poll_write_vectored(cx, bufs) } fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { @@ -448,10 +453,10 @@ mod if_std { Pin::get_mut(self).as_mut().poll_write(cx, buf) } - fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec]) + fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll> { - Pin::get_mut(self).as_mut().poll_vectored_write(cx, vec) + Pin::get_mut(self).as_mut().poll_write_vectored(cx, bufs) } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { @@ -471,6 +476,12 @@ mod if_std { Poll::Ready(StdIo::Write::write(&mut *self, buf)) } + fn poll_write_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>]) + -> Poll> + { + Poll::Ready(StdIo::Write::write_vectored(&mut *self, bufs)) + } + fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { Poll::Ready(StdIo::Write::flush(&mut *self)) } @@ -499,6 +510,12 @@ mod if_std { Poll::Ready(result) } + fn poll_write_vectored(self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>]) + -> Poll> + { + Poll::Ready(StdIo::Write::write_vectored(&mut self.get_mut().get_mut().as_mut(), bufs)) + } + fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { Poll::Ready(StdIo::Write::flush(&mut self.get_mut().get_mut().as_mut())) } diff --git a/futures-util/src/io/allow_std.rs b/futures-util/src/io/allow_std.rs index 53e359ec7a..4e752bb1e9 100644 --- a/futures-util/src/io/allow_std.rs +++ b/futures-util/src/io/allow_std.rs @@ -1,5 +1,5 @@ use futures_core::task::{Context, Poll}; -use futures_io::{AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead}; +use futures_io::{AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, IoSlice, IoSliceMut, SeekFrom}; use std::{fmt, io}; use std::pin::Pin; @@ -62,6 +62,9 @@ impl io::Write for AllowStdIo where T: io::Write { fn write(&mut self, buf: &[u8]) -> io::Result { self.0.write(buf) } + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + self.0.write_vectored(bufs) + } fn flush(&mut self) -> io::Result<()> { self.0.flush() } @@ -80,6 +83,12 @@ impl AsyncWrite for AllowStdIo where T: io::Write { Poll::Ready(Ok(try_with_interrupt!(self.0.write(buf)))) } + fn poll_write_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>]) + -> Poll> + { + Poll::Ready(Ok(try_with_interrupt!(self.0.write_vectored(bufs)))) + } + fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { try_with_interrupt!(self.0.flush()); Poll::Ready(Ok(())) @@ -94,6 +103,9 @@ impl io::Read for AllowStdIo where T: io::Read { fn read(&mut self, buf: &mut [u8]) -> io::Result { self.0.read(buf) } + fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result { + self.0.read_vectored(bufs) + } // TODO: implement the `initializer` fn when it stabilizes. // See rust-lang/rust #42788 fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { @@ -113,16 +125,22 @@ impl AsyncRead for AllowStdIo where T: io::Read { { Poll::Ready(Ok(try_with_interrupt!(self.0.read(buf)))) } + + fn poll_read_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>]) + -> Poll> + { + Poll::Ready(Ok(try_with_interrupt!(self.0.read_vectored(bufs)))) + } } impl io::Seek for AllowStdIo where T: io::Seek { - fn seek(&mut self, pos: io::SeekFrom) -> io::Result { + fn seek(&mut self, pos: SeekFrom) -> io::Result { self.0.seek(pos) } } impl AsyncSeek for AllowStdIo where T: io::Seek { - fn poll_seek(mut self: Pin<&mut Self>, _: &mut Context<'_>, pos: io::SeekFrom) + fn poll_seek(mut self: Pin<&mut Self>, _: &mut Context<'_>, pos: SeekFrom) -> Poll> { Poll::Ready(Ok(try_with_interrupt!(self.0.seek(pos)))) diff --git a/futures-util/src/io/mod.rs b/futures-util/src/io/mod.rs index b8fa599e3a..c058378dab 100644 --- a/futures-util/src/io/mod.rs +++ b/futures-util/src/io/mod.rs @@ -6,7 +6,7 @@ //! to the `AsyncRead` and `AsyncWrite` types. pub use futures_io::{ - AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, IoVec, SeekFrom, + AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, IoSlice, IoSliceMut, SeekFrom, }; #[cfg(feature = "io-compat")] use crate::compat::Compat; diff --git a/futures-util/src/io/split.rs b/futures-util/src/io/split.rs index 333fc17983..bc47e4a98b 100644 --- a/futures-util/src/io/split.rs +++ b/futures-util/src/io/split.rs @@ -1,6 +1,6 @@ use crate::lock::BiLock; use futures_core::task::{Context, Poll}; -use futures_io::{AsyncRead, AsyncWrite, IoVec}; +use futures_io::{AsyncRead, AsyncWrite, IoSlice, IoSliceMut}; use std::io; use std::pin::Pin; @@ -43,10 +43,10 @@ impl AsyncRead for ReadHalf { lock_and_then(&self.handle, cx, |l, cx| l.poll_read(cx, buf)) } - fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec]) + fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>]) -> Poll> { - lock_and_then(&self.handle, cx, |l, cx| l.poll_vectored_read(cx, vec)) + lock_and_then(&self.handle, cx, |l, cx| l.poll_read_vectored(cx, vec)) } } @@ -57,10 +57,10 @@ impl AsyncWrite for WriteHalf { lock_and_then(&self.handle, cx, |l, cx| l.poll_write(cx, buf)) } - fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec]) + fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll> { - lock_and_then(&self.handle, cx, |l, cx| l.poll_vectored_write(cx, vec)) + lock_and_then(&self.handle, cx, |l, cx| l.poll_write_vectored(cx, bufs)) } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { diff --git a/futures/src/lib.rs b/futures/src/lib.rs index 0a73700df4..842f590eb3 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -272,8 +272,9 @@ pub mod io { pub use futures_io::{ AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, Error, ErrorKind, - Initializer, IoVec, Result, SeekFrom, + Initializer, IoSlice, IoSliceMut, Result, SeekFrom, }; + pub use futures_util::io::{ AsyncReadExt, AsyncWriteExt, AsyncSeekExt, AsyncBufReadExt, AllowStdIo, Close, CopyInto, Flush, Read, ReadExact, ReadHalf, ReadToEnd, ReadUntil,