Skip to content

Commit 4186b0a

Browse files
authored
io: remove poll_{read,write}_buf from traits (#2882)
These functions have object safety issues. It also has been decided to avoid vectored operations on the I/O traits. A later PR will bring back vectored operations on specific types that support them. Refs: #2879, #2716
1 parent 760ae89 commit 4186b0a

20 files changed

+319
-514
lines changed

tokio-test/src/io.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
2222
use tokio::sync::mpsc;
2323
use tokio::time::{self, Delay, Duration, Instant};
2424

25-
use bytes::Buf;
2625
use futures_core::ready;
2726
use std::collections::VecDeque;
2827
use std::future::Future;
@@ -439,16 +438,6 @@ impl AsyncWrite for Mock {
439438
}
440439
}
441440

442-
fn poll_write_buf<B: Buf>(
443-
self: Pin<&mut Self>,
444-
cx: &mut task::Context<'_>,
445-
buf: &mut B,
446-
) -> Poll<io::Result<usize>> {
447-
let n = ready!(self.poll_write(cx, buf.bytes()))?;
448-
buf.advance(n);
449-
Poll::Ready(Ok(n))
450-
}
451-
452441
fn poll_flush(self: Pin<&mut Self>, _cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
453442
Poll::Ready(Ok(()))
454443
}

tokio-util/src/codec/framed_impl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ where
118118
type Item = Result<U::Item, U::Error>;
119119

120120
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
121+
use crate::util::poll_read_buf;
122+
121123
let mut pinned = self.project();
122124
let state: &mut ReadFrame = pinned.state.borrow_mut();
123125
loop {
@@ -148,7 +150,7 @@ where
148150
// got room for at least one byte to read to ensure that we don't
149151
// get a spurious 0 that looks like EOF
150152
state.buffer.reserve(1);
151-
let bytect = match pinned.inner.as_mut().poll_read_buf(cx, &mut state.buffer)? {
153+
let bytect = match poll_read_buf(cx, pinned.inner.as_mut(), &mut state.buffer)? {
152154
Poll::Ready(ct) => ct,
153155
Poll::Pending => return Poll::Pending,
154156
};

tokio-util/src/io/reader_stream.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ impl<R: AsyncRead> ReaderStream<R> {
7070
impl<R: AsyncRead> Stream for ReaderStream<R> {
7171
type Item = std::io::Result<Bytes>;
7272
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
73+
use crate::util::poll_read_buf;
74+
7375
let mut this = self.as_mut().project();
7476

7577
let reader = match this.reader.as_pin_mut() {
@@ -81,7 +83,7 @@ impl<R: AsyncRead> Stream for ReaderStream<R> {
8183
this.buf.reserve(CAPACITY);
8284
}
8385

84-
match reader.poll_read_buf(cx, &mut this.buf) {
86+
match poll_read_buf(cx, reader, &mut this.buf) {
8587
Poll::Pending => Poll::Pending,
8688
Poll::Ready(Err(err)) => {
8789
self.project().reader.set(None);

tokio-util/src/io/stream_reader.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bytes::{Buf, BufMut};
1+
use bytes::Buf;
22
use futures_core::stream::Stream;
33
use pin_project_lite::pin_project;
44
use std::io;
@@ -119,29 +119,6 @@ where
119119
self.consume(len);
120120
Poll::Ready(Ok(()))
121121
}
122-
fn poll_read_buf<BM: BufMut>(
123-
mut self: Pin<&mut Self>,
124-
cx: &mut Context<'_>,
125-
buf: &mut BM,
126-
) -> Poll<io::Result<usize>>
127-
where
128-
Self: Sized,
129-
{
130-
if !buf.has_remaining_mut() {
131-
return Poll::Ready(Ok(0));
132-
}
133-
134-
let inner_buf = match self.as_mut().poll_fill_buf(cx) {
135-
Poll::Ready(Ok(buf)) => buf,
136-
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
137-
Poll::Pending => return Poll::Pending,
138-
};
139-
let len = std::cmp::min(inner_buf.len(), buf.remaining_mut());
140-
buf.put_slice(&inner_buf[..len]);
141-
142-
self.consume(len);
143-
Poll::Ready(Ok(len))
144-
}
145122
}
146123

147124
impl<S, B, E> AsyncBufRead for StreamReader<S, B>

tokio-util/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,37 @@ pub mod context;
5252
pub mod sync;
5353

5454
pub mod either;
55+
56+
#[cfg(any(feature = "io", feature = "codec"))]
57+
mod util {
58+
use tokio::io::{AsyncRead, ReadBuf};
59+
60+
use bytes::BufMut;
61+
use futures_core::ready;
62+
use std::io;
63+
use std::pin::Pin;
64+
use std::task::{Context, Poll};
65+
66+
pub(crate) fn poll_read_buf<T: AsyncRead>(
67+
cx: &mut Context<'_>,
68+
io: Pin<&mut T>,
69+
buf: &mut impl BufMut,
70+
) -> Poll<io::Result<usize>> {
71+
if !buf.has_remaining_mut() {
72+
return Poll::Ready(Ok(0));
73+
}
74+
75+
let orig = buf.bytes_mut().as_ptr() as *const u8;
76+
let mut b = ReadBuf::uninit(buf.bytes_mut());
77+
78+
ready!(io.poll_read(cx, &mut b))?;
79+
let n = b.filled().len();
80+
81+
// Safety: we can assume `n` bytes were read, since they are in`filled`.
82+
assert_eq!(orig, b.filled().as_ptr());
83+
unsafe {
84+
buf.advance_mut(n);
85+
}
86+
Poll::Ready(Ok(n))
87+
}
88+
}

tokio/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ signal = [
8282
stream = ["futures-core"]
8383
sync = ["fnv"]
8484
test-util = []
85-
tcp = ["iovec", "lazy_static", "mio"]
85+
tcp = ["lazy_static", "mio"]
8686
time = ["slab"]
8787
udp = ["lazy_static", "mio"]
8888
uds = ["lazy_static", "libc", "mio", "mio-uds"]
@@ -99,7 +99,6 @@ futures-core = { version = "0.3.0", optional = true }
9999
lazy_static = { version = "1.0.2", optional = true }
100100
memchr = { version = "2.2", optional = true }
101101
mio = { version = "0.6.20", optional = true }
102-
iovec = { version = "0.1.4", optional = true }
103102
num_cpus = { version = "1.8.0", optional = true }
104103
parking_lot = { version = "0.11.0", optional = true } # Not in full
105104
slab = { version = "0.4.1", optional = true } # Backs `DelayQueue`

0 commit comments

Comments
 (0)