Skip to content

Commit dfbd3c9

Browse files
committed
Use std IoVec on AsyncRead/AsyncWrite
Also, this renames `poll_vectored_*` to `poll_*_vectored` to make it in the same order as std.
1 parent bbbfbb7 commit dfbd3c9

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

futures-io/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ The `AsyncRead` and `AsyncWrite` traits for the futures-rs library.
1515
name = "futures_io"
1616

1717
[features]
18-
std = ["futures-core-preview/std", "iovec"]
18+
std = ["futures-core-preview/std"]
1919
default = ["std"]
2020

2121
[dependencies]
2222
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.14", default-features = false }
23-
iovec = { version = "0.1", optional = true }
2423

2524
[dev-dependencies]
2625
futures-preview = { path = "../futures", version = "=0.3.0-alpha.14" }

futures-io/src/lib.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ mod if_std {
2020
use std::pin::Pin;
2121
use std::ptr;
2222

23-
// Re-export IoVec for convenience
24-
pub use iovec::IoVec;
25-
26-
// Re-export io::Error so that users don't have to deal
23+
// Re-export some `std::io` items so that users don't have to deal
2724
// with conflicts when `use`ing `futures::io` and `std::io`.
2825
pub use self::StdIo::Error as Error;
2926
pub use self::StdIo::ErrorKind as ErrorKind;
3027
pub use self::StdIo::Result as Result;
28+
pub use self::StdIo::{IoVec as IoVec, IoVecMut as IoVecMut};
3129

3230
/// A type used to conditionally initialize buffers passed to `AsyncRead`
3331
/// methods, modeled after `std`.
@@ -133,7 +131,7 @@ mod if_std {
133131
/// `Interrupted`. Implementations must convert `WouldBlock` into
134132
/// `Poll::Pending` and either internally retry or convert
135133
/// `Interrupted` into another error kind.
136-
fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
134+
fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoVecMut<'_>])
137135
-> Poll<Result<usize>>
138136
{
139137
if let Some(ref mut first_iovec) = vec.get_mut(0) {
@@ -194,7 +192,7 @@ mod if_std {
194192
/// `Interrupted`. Implementations must convert `WouldBlock` into
195193
/// `Poll::Pending` and either internally retry or convert
196194
/// `Interrupted` into another error kind.
197-
fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
195+
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[IoVec<'_>])
198196
-> Poll<Result<usize>>
199197
{
200198
if let Some(ref first_iovec) = vec.get(0) {
@@ -253,10 +251,10 @@ mod if_std {
253251
Pin::new(&mut **self).poll_read(cx, buf)
254252
}
255253

256-
fn poll_vectored_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
254+
fn poll_read_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoVecMut<'_>])
257255
-> Poll<Result<usize>>
258256
{
259-
Pin::new(&mut **self).poll_vectored_read(cx, vec)
257+
Pin::new(&mut **self).poll_read_vectored(cx, vec)
260258
}
261259
}
262260
}
@@ -284,10 +282,10 @@ mod if_std {
284282
Pin::get_mut(self).as_mut().poll_read(cx, buf)
285283
}
286284

287-
fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
285+
fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoVecMut<'_>])
288286
-> Poll<Result<usize>>
289287
{
290-
Pin::get_mut(self).as_mut().poll_vectored_read(cx, vec)
288+
Pin::get_mut(self).as_mut().poll_read_vectored(cx, vec)
291289
}
292290
}
293291

@@ -304,6 +302,12 @@ mod if_std {
304302
{
305303
Poll::Ready(StdIo::Read::read(&mut *self, buf))
306304
}
305+
306+
fn poll_read_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, vec: &mut [IoVecMut<'_>])
307+
-> Poll<Result<usize>>
308+
{
309+
Poll::Ready(StdIo::Read::read_vectored(&mut *self, vec))
310+
}
307311
}
308312
}
309313

@@ -327,10 +331,10 @@ mod if_std {
327331
Pin::new(&mut **self).poll_write(cx, buf)
328332
}
329333

330-
fn poll_vectored_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
334+
fn poll_write_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[IoVec<'_>])
331335
-> Poll<Result<usize>>
332336
{
333-
Pin::new(&mut **self).poll_vectored_write(cx, vec)
337+
Pin::new(&mut **self).poll_write_vectored(cx, vec)
334338
}
335339

336340
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
@@ -362,10 +366,10 @@ mod if_std {
362366
Pin::get_mut(self).as_mut().poll_write(cx, buf)
363367
}
364368

365-
fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
369+
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[IoVec<'_>])
366370
-> Poll<Result<usize>>
367371
{
368-
Pin::get_mut(self).as_mut().poll_vectored_write(cx, vec)
372+
Pin::get_mut(self).as_mut().poll_write_vectored(cx, vec)
369373
}
370374

371375
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
@@ -385,6 +389,12 @@ mod if_std {
385389
Poll::Ready(StdIo::Write::write(&mut *self, buf))
386390
}
387391

392+
fn poll_write_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, vec: &[IoVec<'_>])
393+
-> Poll<Result<usize>>
394+
{
395+
Poll::Ready(StdIo::Write::write_vectored(&mut *self, vec))
396+
}
397+
388398
fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>> {
389399
Poll::Ready(StdIo::Write::flush(&mut *self))
390400
}
@@ -413,6 +423,12 @@ mod if_std {
413423
Poll::Ready(result)
414424
}
415425

426+
fn poll_write_vectored(self: Pin<&mut Self>, _: &mut Context<'_>, vec: &[IoVec<'_>])
427+
-> Poll<Result<usize>>
428+
{
429+
Poll::Ready(StdIo::Write::write_vectored(&mut self.get_mut().get_mut().as_mut(), vec))
430+
}
431+
416432
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>> {
417433
Poll::Ready(StdIo::Write::flush(&mut self.get_mut().get_mut().as_mut()))
418434
}

futures-util/src/io/allow_std.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ impl<T> io::Write for AllowStdIo<T> where T: io::Write {
6464
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
6565
self.0.write(buf)
6666
}
67+
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
68+
self.0.write_vectored(bufs)
69+
}
6770
fn flush(&mut self) -> io::Result<()> {
6871
self.0.flush()
6972
}
@@ -82,6 +85,12 @@ impl<T> AsyncWrite for AllowStdIo<T> where T: io::Write {
8285
Poll::Ready(Ok(try_with_interrupt!(self.0.write(buf))))
8386
}
8487

88+
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoVec<'_>])
89+
-> Poll<Result<usize>>
90+
{
91+
Poll::Ready(Ok(try_with_interrupt!(self.0.write_vectored(bufs))))
92+
}
93+
8594
fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
8695
try_with_interrupt!(self.0.flush());
8796
Poll::Ready(Ok(()))
@@ -96,6 +105,9 @@ impl<T> io::Read for AllowStdIo<T> where T: io::Read {
96105
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
97106
self.0.read(buf)
98107
}
108+
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
109+
self.0.read_vectored(bufs)
110+
}
99111
// TODO: implement the `initializer` fn when it stabilizes.
100112
// See rust-lang/rust #42788
101113
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
@@ -115,4 +127,10 @@ impl<T> AsyncRead for AllowStdIo<T> where T: io::Read {
115127
{
116128
Poll::Ready(Ok(try_with_interrupt!(self.0.read(buf))))
117129
}
130+
131+
fn poll_read_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &mut [IoVecMut<'_>])
132+
-> Poll<Result<usize>>
133+
{
134+
Poll::Ready(Ok(try_with_interrupt!(self.0.read_vectored(bufs))))
135+
}
118136
}

futures-util/src/io/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use std::vec::Vec;
99

10-
pub use futures_io::{AsyncRead, AsyncWrite, IoVec};
10+
pub use futures_io::{AsyncRead, AsyncWrite, IoVec, IoVecMut};
1111

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

futures-util/src/io/split.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::lock::BiLock;
22
use futures_core::task::{Context, Poll};
3-
use futures_io::{AsyncRead, AsyncWrite, IoVec};
3+
use futures_io::{AsyncRead, AsyncWrite, IoVec, IoVecMut};
44
use std::io;
55
use std::pin::Pin;
66

@@ -43,10 +43,10 @@ impl<R: AsyncRead> AsyncRead for ReadHalf<R> {
4343
lock_and_then(&self.handle, cx, |l, cx| l.poll_read(cx, buf))
4444
}
4545

46-
fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
46+
fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoVecMut<'_>])
4747
-> Poll<io::Result<usize>>
4848
{
49-
lock_and_then(&self.handle, cx, |l, cx| l.poll_vectored_read(cx, vec))
49+
lock_and_then(&self.handle, cx, |l, cx| l.poll_read_vectored(cx, vec))
5050
}
5151
}
5252

@@ -57,10 +57,10 @@ impl<W: AsyncWrite> AsyncWrite for WriteHalf<W> {
5757
lock_and_then(&self.handle, cx, |l, cx| l.poll_write(cx, buf))
5858
}
5959

60-
fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
60+
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[IoVec<'_>])
6161
-> Poll<io::Result<usize>>
6262
{
63-
lock_and_then(&self.handle, cx, |l, cx| l.poll_vectored_write(cx, vec))
63+
lock_and_then(&self.handle, cx, |l, cx| l.poll_write_vectored(cx, vec))
6464
}
6565

6666
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {

futures/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,9 @@ pub mod io {
268268
//! sinks.
269269
270270
pub use futures_io::{
271-
Error, Initializer, IoVec, ErrorKind, AsyncRead, AsyncWrite, Result
271+
Error, Initializer, IoVec, IoVecMut, ErrorKind, AsyncRead, AsyncWrite, Result,
272272
};
273+
273274
pub use futures_util::io::{
274275
AsyncReadExt, AsyncWriteExt, AllowStdIo, Close, CopyInto, Flush,
275276
Read, ReadExact, ReadHalf, ReadToEnd, Window, WriteAll, WriteHalf,

0 commit comments

Comments
 (0)