Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::io::IoSlice[Mut] on AsyncRead/AsyncWrite take2 #1533

Merged
merged 1 commit into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
3 changes: 1 addition & 2 deletions futures-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
55 changes: 36 additions & 19 deletions futures-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -112,7 +111,7 @@ mod if_std {
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8])
-> Poll<Result<usize>>;

/// 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
Expand All @@ -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
Expand All @@ -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<Result<usize>>
{
if let Some(ref mut first_iovec) = vec.get_mut(0) {
Expand Down Expand Up @@ -172,7 +171,7 @@ mod if_std {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8])
-> Poll<Result<usize>>;

/// 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
Expand All @@ -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
Expand All @@ -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<Result<usize>>
{
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))
}
}
Expand Down Expand Up @@ -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<Result<usize>>
{
Pin::new(&mut **self).poll_vectored_read(cx, vec)
Pin::new(&mut **self).poll_read_vectored(cx, vec)
}
}
}
Expand Down Expand Up @@ -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<Result<usize>>
{
Pin::get_mut(self).as_mut().poll_vectored_read(cx, vec)
Pin::get_mut(self).as_mut().poll_read_vectored(cx, vec)
}
}

Expand All @@ -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<Result<usize>>
{
Poll::Ready(StdIo::Read::read_vectored(&mut *self, vec))
}
}
}

Expand Down Expand Up @@ -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<Result<usize>>
{
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<Result<()>> {
Expand Down Expand Up @@ -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<Result<usize>>
{
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<Result<()>> {
Expand All @@ -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<Result<usize>>
{
Poll::Ready(StdIo::Write::write_vectored(&mut *self, bufs))
}

fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>> {
Poll::Ready(StdIo::Write::flush(&mut *self))
}
Expand Down Expand Up @@ -499,6 +510,12 @@ mod if_std {
Poll::Ready(result)
}

fn poll_write_vectored(self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>])
-> Poll<Result<usize>>
{
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<Result<()>> {
Poll::Ready(StdIo::Write::flush(&mut self.get_mut().get_mut().as_mut()))
}
Expand Down
24 changes: 21 additions & 3 deletions futures-util/src/io/allow_std.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -62,6 +62,9 @@ impl<T> io::Write for AllowStdIo<T> where T: io::Write {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.0.write_vectored(bufs)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
Expand All @@ -80,6 +83,12 @@ impl<T> AsyncWrite for AllowStdIo<T> 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<io::Result<usize>>
{
Poll::Ready(Ok(try_with_interrupt!(self.0.write_vectored(bufs))))
}

fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
try_with_interrupt!(self.0.flush());
Poll::Ready(Ok(()))
Expand All @@ -94,6 +103,9 @@ impl<T> io::Read for AllowStdIo<T> where T: io::Read {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
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<u8>) -> io::Result<usize> {
Expand All @@ -113,16 +125,22 @@ impl<T> AsyncRead for AllowStdIo<T> 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<io::Result<usize>>
{
Poll::Ready(Ok(try_with_interrupt!(self.0.read_vectored(bufs))))
}
}

impl<T> io::Seek for AllowStdIo<T> where T: io::Seek {
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.0.seek(pos)
}
}

impl<T> AsyncSeek for AllowStdIo<T> 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<io::Result<u64>>
{
Poll::Ready(Ok(try_with_interrupt!(self.0.seek(pos))))
Expand Down
2 changes: 1 addition & 1 deletion futures-util/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions futures-util/src/io/split.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -43,10 +43,10 @@ impl<R: AsyncRead> AsyncRead for ReadHalf<R> {
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<io::Result<usize>>
{
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))
}
}

Expand All @@ -57,10 +57,10 @@ impl<W: AsyncWrite> AsyncWrite for WriteHalf<W> {
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<io::Result<usize>>
{
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<io::Result<()>> {
Expand Down
3 changes: 2 additions & 1 deletion futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down