Skip to content

Commit 17ab22a

Browse files
committed
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.
1 parent 50f3f71 commit 17ab22a

File tree

6 files changed

+62
-27
lines changed

6 files changed

+62
-27
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.15", default-features = false }
23-
iovec = { version = "0.1", optional = true }
2423

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

futures-io/src/lib.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ 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-
2623
// Re-export some types from `std::io` 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::IoSlice as IoSlice;
29+
pub use self::StdIo::IoSliceMut as IoSliceMut;
3130
pub use self::StdIo::SeekFrom as SeekFrom;
3231

3332
/// A type used to conditionally initialize buffers passed to `AsyncRead`
@@ -134,7 +133,7 @@ mod if_std {
134133
/// `Interrupted`. Implementations must convert `WouldBlock` into
135134
/// `Poll::Pending` and either internally retry or convert
136135
/// `Interrupted` into another error kind.
137-
fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
136+
fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>])
138137
-> Poll<Result<usize>>
139138
{
140139
if let Some(ref mut first_iovec) = vec.get_mut(0) {
@@ -195,13 +194,13 @@ mod if_std {
195194
/// `Interrupted`. Implementations must convert `WouldBlock` into
196195
/// `Poll::Pending` and either internally retry or convert
197196
/// `Interrupted` into another error kind.
198-
fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
197+
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>])
199198
-> Poll<Result<usize>>
200199
{
201-
if let Some(ref first_iovec) = vec.get(0) {
200+
if let Some(ref first_iovec) = bufs.get(0) {
202201
self.poll_write(cx, &*first_iovec)
203202
} else {
204-
// `vec` is empty.
203+
// `bufs` is empty.
205204
Poll::Ready(Ok(0))
206205
}
207206
}
@@ -335,10 +334,10 @@ mod if_std {
335334
Pin::new(&mut **self).poll_read(cx, buf)
336335
}
337336

338-
fn poll_vectored_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
337+
fn poll_read_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>])
339338
-> Poll<Result<usize>>
340339
{
341-
Pin::new(&mut **self).poll_vectored_read(cx, vec)
340+
Pin::new(&mut **self).poll_read_vectored(cx, vec)
342341
}
343342
}
344343
}
@@ -366,10 +365,10 @@ mod if_std {
366365
Pin::get_mut(self).as_mut().poll_read(cx, buf)
367366
}
368367

369-
fn poll_vectored_read(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [&mut IoVec])
368+
fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &mut [IoSliceMut<'_>])
370369
-> Poll<Result<usize>>
371370
{
372-
Pin::get_mut(self).as_mut().poll_vectored_read(cx, vec)
371+
Pin::get_mut(self).as_mut().poll_read_vectored(cx, vec)
373372
}
374373
}
375374

@@ -386,6 +385,12 @@ mod if_std {
386385
{
387386
Poll::Ready(StdIo::Read::read(&mut *self, buf))
388387
}
388+
389+
fn poll_read_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, vec: &mut [IoSliceMut<'_>])
390+
-> Poll<Result<usize>>
391+
{
392+
Poll::Ready(StdIo::Read::read_vectored(&mut *self, vec))
393+
}
389394
}
390395
}
391396

@@ -413,10 +418,10 @@ mod if_std {
413418
Pin::new(&mut **self).poll_write(cx, buf)
414419
}
415420

416-
fn poll_vectored_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
421+
fn poll_write_vectored(mut self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>])
417422
-> Poll<Result<usize>>
418423
{
419-
Pin::new(&mut **self).poll_vectored_write(cx, vec)
424+
Pin::new(&mut **self).poll_write_vectored(cx, bufs)
420425
}
421426

422427
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
@@ -448,10 +453,10 @@ mod if_std {
448453
Pin::get_mut(self).as_mut().poll_write(cx, buf)
449454
}
450455

451-
fn poll_vectored_write(self: Pin<&mut Self>, cx: &mut Context<'_>, vec: &[&IoVec])
456+
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>])
452457
-> Poll<Result<usize>>
453458
{
454-
Pin::get_mut(self).as_mut().poll_vectored_write(cx, vec)
459+
Pin::get_mut(self).as_mut().poll_write_vectored(cx, bufs)
455460
}
456461

457462
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
@@ -471,6 +476,12 @@ mod if_std {
471476
Poll::Ready(StdIo::Write::write(&mut *self, buf))
472477
}
473478

479+
fn poll_write_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>])
480+
-> Poll<Result<usize>>
481+
{
482+
Poll::Ready(StdIo::Write::write_vectored(&mut *self, bufs))
483+
}
484+
474485
fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>> {
475486
Poll::Ready(StdIo::Write::flush(&mut *self))
476487
}
@@ -499,6 +510,12 @@ mod if_std {
499510
Poll::Ready(result)
500511
}
501512

513+
fn poll_write_vectored(self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>])
514+
-> Poll<Result<usize>>
515+
{
516+
Poll::Ready(StdIo::Write::write_vectored(&mut self.get_mut().get_mut().as_mut(), bufs))
517+
}
518+
502519
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>> {
503520
Poll::Ready(StdIo::Write::flush(&mut self.get_mut().get_mut().as_mut()))
504521
}

futures-util/src/io/allow_std.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use futures_core::task::{Context, Poll};
2-
use futures_io::{AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead};
2+
use futures_io::{AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, IoSlice, IoSliceMut, SeekFrom};
33
use std::{fmt, io};
44
use std::pin::Pin;
55

@@ -62,6 +62,9 @@ impl<T> io::Write for AllowStdIo<T> where T: io::Write {
6262
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
6363
self.0.write(buf)
6464
}
65+
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
66+
self.0.write_vectored(bufs)
67+
}
6568
fn flush(&mut self) -> io::Result<()> {
6669
self.0.flush()
6770
}
@@ -80,6 +83,12 @@ impl<T> AsyncWrite for AllowStdIo<T> where T: io::Write {
8083
Poll::Ready(Ok(try_with_interrupt!(self.0.write(buf))))
8184
}
8285

86+
fn poll_write_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>])
87+
-> Poll<io::Result<usize>>
88+
{
89+
Poll::Ready(Ok(try_with_interrupt!(self.0.write_vectored(bufs))))
90+
}
91+
8392
fn poll_flush(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
8493
try_with_interrupt!(self.0.flush());
8594
Poll::Ready(Ok(()))
@@ -94,6 +103,9 @@ impl<T> io::Read for AllowStdIo<T> where T: io::Read {
94103
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
95104
self.0.read(buf)
96105
}
106+
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
107+
self.0.read_vectored(bufs)
108+
}
97109
// TODO: implement the `initializer` fn when it stabilizes.
98110
// See rust-lang/rust #42788
99111
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
@@ -113,16 +125,22 @@ impl<T> AsyncRead for AllowStdIo<T> where T: io::Read {
113125
{
114126
Poll::Ready(Ok(try_with_interrupt!(self.0.read(buf))))
115127
}
128+
129+
fn poll_read_vectored(mut self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>])
130+
-> Poll<io::Result<usize>>
131+
{
132+
Poll::Ready(Ok(try_with_interrupt!(self.0.read_vectored(bufs))))
133+
}
116134
}
117135

118136
impl<T> io::Seek for AllowStdIo<T> where T: io::Seek {
119-
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
137+
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
120138
self.0.seek(pos)
121139
}
122140
}
123141

124142
impl<T> AsyncSeek for AllowStdIo<T> where T: io::Seek {
125-
fn poll_seek(mut self: Pin<&mut Self>, _: &mut Context<'_>, pos: io::SeekFrom)
143+
fn poll_seek(mut self: Pin<&mut Self>, _: &mut Context<'_>, pos: SeekFrom)
126144
-> Poll<io::Result<u64>>
127145
{
128146
Poll::Ready(Ok(try_with_interrupt!(self.0.seek(pos))))

futures-util/src/io/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! to the `AsyncRead` and `AsyncWrite` types.
77
88
pub use futures_io::{
9-
AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, IoVec, SeekFrom,
9+
AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, IoSlice, IoSliceMut, SeekFrom,
1010
};
1111

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

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, IoSlice, IoSliceMut};
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 [IoSliceMut<'_>])
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<'_>, bufs: &[IoSlice<'_>])
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, bufs))
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
@@ -272,8 +272,9 @@ pub mod io {
272272
273273
pub use futures_io::{
274274
AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, Error, ErrorKind,
275-
Initializer, IoVec, Result, SeekFrom,
275+
Initializer, IoSlice, IoSliceMut, Result, SeekFrom,
276276
};
277+
277278
pub use futures_util::io::{
278279
AsyncReadExt, AsyncWriteExt, AsyncSeekExt, AsyncBufReadExt, AllowStdIo,
279280
Close, CopyInto, Flush, Read, ReadExact, ReadHalf, ReadToEnd, ReadUntil,

0 commit comments

Comments
 (0)