Skip to content

Use std IoVec on AsyncRead/AsyncWrite #1476

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

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 3 additions & 2 deletions futures-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ 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"]
nightly = []
iovec = []

[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
56 changes: 44 additions & 12 deletions futures-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.14/futures_io")]

#![feature(futures_api)]
#![cfg_attr(feature = "iovec", feature(iovec))]

#[cfg(all(feature = "iovec", not(feature = "nightly")))]
compile_error!("The `iovec` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[cfg(feature = "std")]
mod if_std {
Expand All @@ -21,8 +25,9 @@ mod if_std {
use std::pin::Pin;
use std::ptr;

// Re-export IoVec for convenience
pub use iovec::IoVec;
// Re-export IoVec and IoVecMut for convenience
#[cfg(feature = "iovec")]
pub use self::StdIo::{IoVec, IoVecMut};

// Re-export io::Error so that users don't have to deal
// with conflicts when `use`ing `futures::io` and `std::io`.
Expand Down Expand Up @@ -134,7 +139,8 @@ 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])
#[cfg(feature = "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 @@ -195,7 +201,8 @@ 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])
#[cfg(feature = "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 @@ -254,10 +261,11 @@ 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])
#[cfg(feature = "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 All @@ -281,10 +289,11 @@ mod if_std {
T::poll_read((*self).as_mut(), cx, buf)
}

fn poll_vectored_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
#[cfg(feature = "iovec")]
fn poll_read_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoVecMut<'_>])
-> Poll<Result<usize>>
{
T::poll_vectored_read((*self).as_mut(), cx, vec)
T::poll_read_vectored((*self).as_mut(), cx, vec)
}
}

Expand All @@ -301,6 +310,13 @@ mod if_std {
{
Poll::Ready(StdIo::Read::read(&mut *self, buf))
}

#[cfg(feature = "iovec")]
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 @@ -324,10 +340,11 @@ 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])
#[cfg(feature = "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 @@ -355,10 +372,11 @@ mod if_std {
T::poll_write((*self).as_mut(), cx, buf)
}

fn poll_vectored_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
#[cfg(feature = "iovec")]
fn poll_write_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[IoVec<'_>])
-> Poll<Result<usize>>
{
T::poll_vectored_write((*self).as_mut(), cx, vec)
T::poll_write_vectored((*self).as_mut(), cx, vec)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
Expand All @@ -378,6 +396,13 @@ mod if_std {
Poll::Ready(StdIo::Write::write(&mut *self, buf))
}

#[cfg(feature = "iovec")]
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 @@ -406,6 +431,13 @@ mod if_std {
Poll::Ready(result)
}

#[cfg(feature = "iovec")]
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
3 changes: 2 additions & 1 deletion futures-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ default = ["std", "futures-core-preview/either", "futures-sink-preview/either"]
compat = ["std", "futures_01"]
io-compat = ["compat", "tokio-io"]
bench = []
nightly = ["futures-core-preview/nightly", "futures-sink-preview/nightly"]
nightly = ["futures-core-preview/nightly", "futures-io-preview/nightly", "futures-sink-preview/nightly"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic"]
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc"]
iovec = ["futures-io-preview/iovec"]

[dependencies]
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.14", default-features = false }
Expand Down
5 changes: 4 additions & 1 deletion futures-util/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

use std::vec::Vec;

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

#[cfg(feature = "iovec")]
pub use futures_io::{IoVec, IoVecMut};

#[cfg(feature = "io-compat")] use crate::compat::Compat;

Expand Down
14 changes: 9 additions & 5 deletions futures-util/src/io/split.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::lock::BiLock;
use futures_core::task::{Context, Poll};
use futures_io::{AsyncRead, AsyncWrite, IoVec};
use futures_io::{AsyncRead, AsyncWrite};
#[cfg(feature = "iovec")]
use futures_io::{IoVec, IoVecMut};
use std::io;
use std::pin::Pin;

Expand Down Expand Up @@ -43,10 +45,11 @@ 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])
#[cfg(feature = "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 +60,11 @@ 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])
#[cfg(feature = "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
4 changes: 4 additions & 0 deletions futures-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![cfg_attr(feature = "alloc", feature(box_into_pin))]
#![cfg_attr(feature = "std", feature(async_await, await_macro))]
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
#![cfg_attr(feature = "iovec", feature(iovec))]

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
Expand All @@ -14,6 +15,9 @@
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[cfg(all(feature = "iovec", not(feature = "nightly")))]
compile_error!("The `iovec` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[cfg(feature = "alloc")]
extern crate alloc;

Expand Down
3 changes: 2 additions & 1 deletion futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ futures-test-preview = { path = "../futures-test", version = "=0.3.0-alpha.14" }
tokio = "0.1.11"

[features]
nightly = ["futures-core-preview/nightly", "futures-sink-preview/nightly", "futures-util-preview/nightly"]
nightly = ["futures-core-preview/nightly", "futures-io-preview/nightly", "futures-sink-preview/nightly", "futures-util-preview/nightly"]
std = ["alloc", "futures-core-preview/std", "futures-executor-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-util-preview/std"]
default = ["std"]
compat = ["std", "futures-util-preview/compat"]
io-compat = ["compat", "futures-util-preview/io-compat"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic", "futures-util-preview/cfg-target-has-atomic"]
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "futures-util-preview/alloc"]
iovec = ["futures-io-preview/iovec", "futures-util-preview/iovec"]
10 changes: 9 additions & 1 deletion futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#![feature(futures_api)]
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
#![cfg_attr(feature = "iovec", feature(iovec))]

#![cfg_attr(not(feature = "std"), no_std)]

Expand All @@ -33,6 +34,9 @@
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[cfg(all(feature = "iovec", not(feature = "nightly")))]
compile_error!("The `iovec` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[doc(hidden)] pub use futures_util::core_reexport;

#[doc(hidden)] pub use futures_core::future::Future;
Expand Down Expand Up @@ -255,8 +259,12 @@ pub mod io {
//! sinks.

pub use futures_io::{
Error, Initializer, IoVec, ErrorKind, AsyncRead, AsyncWrite, Result
Error, Initializer, ErrorKind, AsyncRead, AsyncWrite, Result,
};

#[cfg(feature = "iovec")]
pub use futures_io::{IoVec, IoVecMut};

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