Skip to content

Commit

Permalink
Use std IoVec on AsyncRead/AsyncWrite
Browse files Browse the repository at this point in the history
Also, this renames `poll_vectored_*` to `poll_*_vectored` to make it in
the same order as std.
  • Loading branch information
taiki-e committed Apr 25, 2019
1 parent bbbfbb7 commit e9e69c4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
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.14", default-features = false }
iovec = { version = "0.1", optional = true }

[dev-dependencies]
futures-preview = { path = "../futures", version = "=0.3.0-alpha.14" }
Expand Down
40 changes: 28 additions & 12 deletions futures-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ mod if_std {
use std::pin::Pin;
use std::ptr;

// Re-export IoVec for convenience
pub use iovec::IoVec;

// Re-export io::Error so that users don't have to deal
// Re-export some `std::io` items 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::{IoVec as IoVec, IoVecMut as IoVecMut};

/// A type used to conditionally initialize buffers passed to `AsyncRead`
/// methods, modeled after `std`.
Expand Down Expand Up @@ -133,7 +131,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 [IoVecMut<'_>])
-> Poll<Result<usize>>
{
if let Some(ref mut first_iovec) = vec.get_mut(0) {
Expand Down Expand Up @@ -194,7 +192,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_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[IoVec<'_>])
-> Poll<Result<usize>>
{
if let Some(ref first_iovec) = vec.get(0) {
Expand Down Expand Up @@ -253,10 +251,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 [IoVecMut<'_>])
-> 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 @@ -284,7 +282,7 @@ 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_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoVecMut<'_>])
-> Poll<Result<usize>>
{
Pin::get_mut(self).as_mut().poll_vectored_read(cx, vec)
Expand All @@ -304,6 +302,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 [IoVecMut<'_>])
-> Poll<Result<usize>>
{
Poll::Ready(StdIo::Read::read_vectored(&mut *self, vec))
}
}
}

Expand All @@ -327,10 +331,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<'_>, vec: &[IoVec<'_>])
-> Poll<Result<usize>>
{
Pin::new(&mut **self).poll_vectored_write(cx, vec)
Pin::new(&mut **self).poll_write_vectored(cx, vec)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
Expand Down Expand Up @@ -362,7 +366,7 @@ 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_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[IoVec<'_>])
-> Poll<Result<usize>>
{
Pin::get_mut(self).as_mut().poll_vectored_write(cx, vec)
Expand All @@ -385,6 +389,12 @@ mod if_std {
Poll::Ready(StdIo::Write::write(&mut *self, buf))
}

fn poll_write_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, vec: &[IoVec<'_>])
-> Poll<Result<usize>>
{
Poll::Ready(StdIo::Write::write_vectored(&mut *self, vec))
}

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

fn poll_write_vectored(self: Pin<&mut Self>, _: &mut Context<'_>, vec: &[IoVec<'_>])
-> Poll<Result<usize>>
{
Poll::Ready(StdIo::Write::write_vectored(&mut self.get_mut().get_mut().as_mut(), vec))
}

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
2 changes: 1 addition & 1 deletion futures-util/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::vec::Vec;

pub use futures_io::{AsyncRead, AsyncWrite, IoVec};
pub use futures_io::{AsyncRead, AsyncWrite, IoVec, IoVecMut};

#[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, IoVec, IoVecMut};
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 [IoVecMut<'_>])
-> 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<'_>, vec: &[IoVec<'_>])
-> 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, vec))
}

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 @@ -268,8 +268,9 @@ pub mod io {
//! sinks.
pub use futures_io::{
Error, Initializer, IoVec, ErrorKind, AsyncRead, AsyncWrite, Result
Error, Initializer, IoVec, IoVecMut, ErrorKind, AsyncRead, AsyncWrite, Result,
};

pub use futures_util::io::{
AsyncReadExt, AsyncWriteExt, AllowStdIo, Close, CopyInto, Flush,
Read, ReadExact, ReadHalf, ReadToEnd, Window, WriteAll, WriteHalf,
Expand Down

0 comments on commit e9e69c4

Please sign in to comment.