From faf93060796bfa31b3d1be4aa33f7b45e6b06042 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 24 Sep 2020 15:18:02 -0700 Subject: [PATCH 1/5] io: remove poll_{read,write}_buf from traits 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 --- tokio-test/src/io.rs | 11 --- tokio-util/src/codec/framed_impl.rs | 4 +- tokio-util/src/io/mod.rs | 25 ++++++ tokio-util/src/io/reader_stream.rs | 4 +- tokio-util/src/io/stream_reader.rs | 25 +----- tokio/Cargo.toml | 3 +- tokio/src/io/async_read.rs | 31 -------- tokio/src/io/async_write.rs | 22 ------ tokio/src/io/split.rs | 19 ----- tokio/src/io/util/async_read_ext.rs | 68 ---------------- tokio/src/io/util/async_write_ext.rs | 76 ------------------ tokio/src/io/util/buf_reader.rs | 9 --- tokio/src/io/util/mod.rs | 2 - tokio/src/io/util/read_buf.rs | 38 --------- tokio/src/io/util/write_buf.rs | 40 ---------- tokio/src/net/tcp/split.rs | 9 --- tokio/src/net/tcp/split_owned.rs | 9 --- tokio/src/net/tcp/stream.rs | 114 --------------------------- tokio/tests/io_async_read.rs | 105 +----------------------- 19 files changed, 34 insertions(+), 580 deletions(-) delete mode 100644 tokio/src/io/util/read_buf.rs delete mode 100644 tokio/src/io/util/write_buf.rs diff --git a/tokio-test/src/io.rs b/tokio-test/src/io.rs index ac70a06f1aa..4f0b589743b 100644 --- a/tokio-test/src/io.rs +++ b/tokio-test/src/io.rs @@ -22,7 +22,6 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio::sync::mpsc; use tokio::time::{self, Delay, Duration, Instant}; -use bytes::Buf; use futures_core::ready; use std::collections::VecDeque; use std::future::Future; @@ -439,16 +438,6 @@ impl AsyncWrite for Mock { } } - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut task::Context<'_>, - buf: &mut B, - ) -> Poll> { - let n = ready!(self.poll_write(cx, buf.bytes()))?; - buf.advance(n); - Poll::Ready(Ok(n)) - } - fn poll_flush(self: Pin<&mut Self>, _cx: &mut task::Context<'_>) -> Poll> { Poll::Ready(Ok(())) } diff --git a/tokio-util/src/codec/framed_impl.rs b/tokio-util/src/codec/framed_impl.rs index eb2e0d38c6d..8ee20052e20 100644 --- a/tokio-util/src/codec/framed_impl.rs +++ b/tokio-util/src/codec/framed_impl.rs @@ -118,6 +118,8 @@ where type Item = Result; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + use crate::io::poll_read_buf; + let mut pinned = self.project(); let state: &mut ReadFrame = pinned.state.borrow_mut(); loop { @@ -148,7 +150,7 @@ where // got room for at least one byte to read to ensure that we don't // get a spurious 0 that looks like EOF state.buffer.reserve(1); - let bytect = match pinned.inner.as_mut().poll_read_buf(cx, &mut state.buffer)? { + let bytect = match poll_read_buf(cx, pinned.inner.as_mut(), &mut state.buffer)? { Poll::Ready(ct) => ct, Poll::Pending => return Poll::Pending, }; diff --git a/tokio-util/src/io/mod.rs b/tokio-util/src/io/mod.rs index 53066c4e444..9e8e7255492 100644 --- a/tokio-util/src/io/mod.rs +++ b/tokio-util/src/io/mod.rs @@ -11,3 +11,28 @@ mod stream_reader; pub use self::reader_stream::ReaderStream; pub use self::stream_reader::StreamReader; + +use tokio::io::{AsyncRead, ReadBuf}; + +use bytes::BufMut; +use futures_core::ready; +use std::io; +use std::pin::Pin; +use std::task::{Context, Poll}; + +pub(crate) fn poll_read_buf(cx: &mut Context<'_>, io: Pin<&mut T>, buf: &mut impl BufMut) -> Poll> { + if !buf.has_remaining_mut() { + return Poll::Ready(Ok(0)); + } + + let mut b = ReadBuf::uninit(buf.bytes_mut()); + + ready!(io.poll_read(cx, &mut b))?; + let n = b.filled().len(); + + // Safety: we can assume `n` bytes were read, since they are in`filled`. + unsafe { + buf.advance_mut(n); + } + Poll::Ready(Ok(n)) +} \ No newline at end of file diff --git a/tokio-util/src/io/reader_stream.rs b/tokio-util/src/io/reader_stream.rs index bde7cceec5c..dcfd2242b6d 100644 --- a/tokio-util/src/io/reader_stream.rs +++ b/tokio-util/src/io/reader_stream.rs @@ -70,6 +70,8 @@ impl ReaderStream { impl Stream for ReaderStream { type Item = std::io::Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + use crate::io::poll_read_buf; + let mut this = self.as_mut().project(); let reader = match this.reader.as_pin_mut() { @@ -81,7 +83,7 @@ impl Stream for ReaderStream { this.buf.reserve(CAPACITY); } - match reader.poll_read_buf(cx, &mut this.buf) { + match poll_read_buf(cx, reader, &mut this.buf) { Poll::Pending => Poll::Pending, Poll::Ready(Err(err)) => { self.project().reader.set(None); diff --git a/tokio-util/src/io/stream_reader.rs b/tokio-util/src/io/stream_reader.rs index 5c3ab019eba..def843b1ddd 100644 --- a/tokio-util/src/io/stream_reader.rs +++ b/tokio-util/src/io/stream_reader.rs @@ -1,4 +1,4 @@ -use bytes::{Buf, BufMut}; +use bytes::Buf; use futures_core::stream::Stream; use pin_project_lite::pin_project; use std::io; @@ -119,29 +119,6 @@ where self.consume(len); Poll::Ready(Ok(())) } - fn poll_read_buf( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut BM, - ) -> Poll> - where - Self: Sized, - { - if !buf.has_remaining_mut() { - return Poll::Ready(Ok(0)); - } - - let inner_buf = match self.as_mut().poll_fill_buf(cx) { - Poll::Ready(Ok(buf)) => buf, - Poll::Ready(Err(err)) => return Poll::Ready(Err(err)), - Poll::Pending => return Poll::Pending, - }; - let len = std::cmp::min(inner_buf.len(), buf.remaining_mut()); - buf.put_slice(&inner_buf[..len]); - - self.consume(len); - Poll::Ready(Ok(len)) - } } impl AsyncBufRead for StreamReader diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 4d8edf63fc7..98451b4a8d3 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -83,7 +83,7 @@ signal = [ stream = ["futures-core"] sync = ["fnv"] test-util = [] -tcp = ["io-driver", "iovec"] +tcp = ["io-driver"] time = ["slab"] udp = ["io-driver"] uds = ["io-driver", "mio-uds", "libc"] @@ -100,7 +100,6 @@ futures-core = { version = "0.3.0", optional = true } lazy_static = { version = "1.0.2", optional = true } memchr = { version = "2.2", optional = true } mio = { version = "0.6.20", optional = true } -iovec = { version = "0.1.4", optional = true } num_cpus = { version = "1.8.0", optional = true } parking_lot = { version = "0.11.0", optional = true } # Not in full slab = { version = "0.4.1", optional = true } # Backs `DelayQueue` diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index d341b63d41a..ba2303d1d2f 100644 --- a/tokio/src/io/async_read.rs +++ b/tokio/src/io/async_read.rs @@ -1,5 +1,4 @@ use super::ReadBuf; -use bytes::BufMut; use std::io; use std::ops::DerefMut; use std::pin::Pin; @@ -54,36 +53,6 @@ pub trait AsyncRead { cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll>; - - /// Pulls some bytes from this source into the specified `BufMut`, returning - /// how many bytes were read. - /// - /// The `buf` provided will have bytes read into it and the internal cursor - /// will be advanced if any bytes were read. Note that this method typically - /// will not reallocate the buffer provided. - fn poll_read_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> - where - Self: Sized, - { - if !buf.has_remaining_mut() { - return Poll::Ready(Ok(0)); - } - - let mut b = ReadBuf::uninit(buf.bytes_mut()); - - ready!(self.poll_read(cx, &mut b))?; - let n = b.filled().len(); - - // Safety: we can assume `n` bytes were read, since they are in`filled`. - unsafe { - buf.advance_mut(n); - } - Poll::Ready(Ok(n)) - } } macro_rules! deref_async_read { diff --git a/tokio/src/io/async_write.rs b/tokio/src/io/async_write.rs index ecf7575b128..66ba4bf3b27 100644 --- a/tokio/src/io/async_write.rs +++ b/tokio/src/io/async_write.rs @@ -1,4 +1,3 @@ -use bytes::Buf; use std::io; use std::ops::DerefMut; use std::pin::Pin; @@ -128,27 +127,6 @@ pub trait AsyncWrite { /// This function will panic if not called within the context of a future's /// task. fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>; - - /// Writes a `Buf` into this value, returning how many bytes were written. - /// - /// Note that this method will advance the `buf` provided automatically by - /// the number of bytes written. - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> - where - Self: Sized, - { - if !buf.has_remaining() { - return Poll::Ready(Ok(0)); - } - - let n = ready!(self.poll_write(cx, buf.bytes()))?; - buf.advance(n); - Poll::Ready(Ok(n)) - } } macro_rules! deref_async_write { diff --git a/tokio/src/io/split.rs b/tokio/src/io/split.rs index dcd3da2032b..fd3273ee28a 100644 --- a/tokio/src/io/split.rs +++ b/tokio/src/io/split.rs @@ -6,7 +6,6 @@ use crate::io::{AsyncRead, AsyncWrite, ReadBuf}; -use bytes::{Buf, BufMut}; use std::cell::UnsafeCell; use std::fmt; use std::io; @@ -107,15 +106,6 @@ impl AsyncRead for ReadHalf { let mut inner = ready!(self.inner.poll_lock(cx)); inner.stream_pin().poll_read(cx, buf) } - - fn poll_read_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - let mut inner = ready!(self.inner.poll_lock(cx)); - inner.stream_pin().poll_read_buf(cx, buf) - } } impl AsyncWrite for WriteHalf { @@ -137,15 +127,6 @@ impl AsyncWrite for WriteHalf { let mut inner = ready!(self.inner.poll_lock(cx)); inner.stream_pin().poll_shutdown(cx) } - - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - let mut inner = ready!(self.inner.poll_lock(cx)); - inner.stream_pin().poll_write_buf(cx, buf) - } } impl Inner { diff --git a/tokio/src/io/util/async_read_ext.rs b/tokio/src/io/util/async_read_ext.rs index 0ab66c286d3..d631bd7e423 100644 --- a/tokio/src/io/util/async_read_ext.rs +++ b/tokio/src/io/util/async_read_ext.rs @@ -1,6 +1,5 @@ use crate::io::util::chain::{chain, Chain}; use crate::io::util::read::{read, Read}; -use crate::io::util::read_buf::{read_buf, ReadBuf}; use crate::io::util::read_exact::{read_exact, ReadExact}; use crate::io::util::read_int::{ ReadI128, ReadI128Le, ReadI16, ReadI16Le, ReadI32, ReadI32Le, ReadI64, ReadI64Le, ReadI8, @@ -13,8 +12,6 @@ use crate::io::util::read_to_string::{read_to_string, ReadToString}; use crate::io::util::take::{take, Take}; use crate::io::AsyncRead; -use bytes::BufMut; - cfg_io_util! { /// Defines numeric reader macro_rules! read_impl { @@ -166,71 +163,6 @@ cfg_io_util! { read(self, buf) } - /// Pulls some bytes from this source into the specified buffer, - /// advancing the buffer's internal cursor. - /// - /// Equivalent to: - /// - /// ```ignore - /// async fn read_buf(&mut self, buf: &mut B) -> io::Result; - /// ``` - /// - /// Usually, only a single `read` syscall is issued, even if there is - /// more space in the supplied buffer. - /// - /// This function does not provide any guarantees about whether it - /// completes immediately or asynchronously - /// - /// # Return - /// - /// On a successful read, the number of read bytes is returned. If the - /// supplied buffer is not empty and the function returns `Ok(0)` then - /// the source as reached an "end-of-file" event. - /// - /// # Errors - /// - /// If this function encounters any form of I/O or other error, an error - /// variant will be returned. If an error is returned then it must be - /// guaranteed that no bytes were read. - /// - /// # Examples - /// - /// [`File`] implements `Read` and [`BytesMut`] implements [`BufMut`]: - /// - /// [`File`]: crate::fs::File - /// [`BytesMut`]: bytes::BytesMut - /// [`BufMut`]: bytes::BufMut - /// - /// ```no_run - /// use tokio::fs::File; - /// use tokio::io::{self, AsyncReadExt}; - /// - /// use bytes::BytesMut; - /// - /// #[tokio::main] - /// async fn main() -> io::Result<()> { - /// let mut f = File::open("foo.txt").await?; - /// let mut buffer = BytesMut::with_capacity(10); - /// - /// assert!(buffer.is_empty()); - /// - /// // read up to 10 bytes, note that the return value is not needed - /// // to access the data that was read as `buffer`'s internal - /// // cursor is updated. - /// f.read_buf(&mut buffer).await?; - /// - /// println!("The bytes: {:?}", &buffer[..]); - /// Ok(()) - /// } - /// ``` - fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B> - where - Self: Sized + Unpin, - B: BufMut, - { - read_buf(self, buf) - } - /// Reads the exact number of bytes required to fill `buf`. /// /// Equivalent to: diff --git a/tokio/src/io/util/async_write_ext.rs b/tokio/src/io/util/async_write_ext.rs index 321301e2897..5c6187b7880 100644 --- a/tokio/src/io/util/async_write_ext.rs +++ b/tokio/src/io/util/async_write_ext.rs @@ -2,7 +2,6 @@ use crate::io::util::flush::{flush, Flush}; use crate::io::util::shutdown::{shutdown, Shutdown}; use crate::io::util::write::{write, Write}; use crate::io::util::write_all::{write_all, WriteAll}; -use crate::io::util::write_buf::{write_buf, WriteBuf}; use crate::io::util::write_int::{ WriteI128, WriteI128Le, WriteI16, WriteI16Le, WriteI32, WriteI32Le, WriteI64, WriteI64Le, WriteI8, @@ -13,8 +12,6 @@ use crate::io::util::write_int::{ }; use crate::io::AsyncWrite; -use bytes::Buf; - cfg_io_util! { /// Defines numeric writer macro_rules! write_impl { @@ -119,79 +116,6 @@ cfg_io_util! { write(self, src) } - /// Writes a buffer into this writer, advancing the buffer's internal - /// cursor. - /// - /// Equivalent to: - /// - /// ```ignore - /// async fn write_buf(&mut self, buf: &mut B) -> io::Result; - /// ``` - /// - /// This function will attempt to write the entire contents of `buf`, but - /// the entire write may not succeed, or the write may also generate an - /// error. After the operation completes, the buffer's - /// internal cursor is advanced by the number of bytes written. A - /// subsequent call to `write_buf` using the **same** `buf` value will - /// resume from the point that the first call to `write_buf` completed. - /// A call to `write` represents *at most one* attempt to write to any - /// wrapped object. - /// - /// # Return - /// - /// If the return value is `Ok(n)` then it must be guaranteed that `n <= - /// buf.len()`. A return value of `0` typically means that the - /// underlying object is no longer able to accept bytes and will likely - /// not be able to in the future as well, or that the buffer provided is - /// empty. - /// - /// # Errors - /// - /// Each call to `write` may generate an I/O error indicating that the - /// operation could not be completed. If an error is returned then no bytes - /// in the buffer were written to this writer. - /// - /// It is **not** considered an error if the entire buffer could not be - /// written to this writer. - /// - /// # Examples - /// - /// [`File`] implements `Read` and [`Cursor<&[u8]>`] implements [`Buf`]: - /// - /// [`File`]: crate::fs::File - /// [`Buf`]: bytes::Buf - /// - /// ```no_run - /// use tokio::io::{self, AsyncWriteExt}; - /// use tokio::fs::File; - /// - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// #[tokio::main] - /// async fn main() -> io::Result<()> { - /// let mut file = File::create("foo.txt").await?; - /// let mut buffer = Cursor::new(b"data to write"); - /// - /// // Loop until the entire contents of the buffer are written to - /// // the file. - /// while buffer.has_remaining() { - /// // Writes some prefix of the byte string, not necessarily - /// // all of it. - /// file.write_buf(&mut buffer).await?; - /// } - /// - /// Ok(()) - /// } - /// ``` - fn write_buf<'a, B>(&'a mut self, src: &'a mut B) -> WriteBuf<'a, Self, B> - where - Self: Sized + Unpin, - B: Buf, - { - write_buf(self, src) - } - /// Attempts to write an entire buffer into this writer. /// /// Equivalent to: diff --git a/tokio/src/io/util/buf_reader.rs b/tokio/src/io/util/buf_reader.rs index 3ab78f0eb8d..9264ca59e2a 100644 --- a/tokio/src/io/util/buf_reader.rs +++ b/tokio/src/io/util/buf_reader.rs @@ -1,7 +1,6 @@ use crate::io::util::DEFAULT_BUF_SIZE; use crate::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf}; -use bytes::Buf; use pin_project_lite::pin_project; use std::io; use std::pin::Pin; @@ -151,14 +150,6 @@ impl AsyncWrite for BufReader { self.get_pin_mut().poll_write(cx, buf) } - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - self.get_pin_mut().poll_write_buf(cx, buf) - } - fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { self.get_pin_mut().poll_flush(cx) } diff --git a/tokio/src/io/util/mod.rs b/tokio/src/io/util/mod.rs index 1bd0a3f87b4..52dab990c62 100644 --- a/tokio/src/io/util/mod.rs +++ b/tokio/src/io/util/mod.rs @@ -39,7 +39,6 @@ cfg_io_util! { pub use mem::{duplex, DuplexStream}; mod read; - mod read_buf; mod read_exact; mod read_int; mod read_line; @@ -68,7 +67,6 @@ cfg_io_util! { mod write; mod write_all; - mod write_buf; mod write_int; diff --git a/tokio/src/io/util/read_buf.rs b/tokio/src/io/util/read_buf.rs deleted file mode 100644 index 6ee3d249f82..00000000000 --- a/tokio/src/io/util/read_buf.rs +++ /dev/null @@ -1,38 +0,0 @@ -use crate::io::AsyncRead; - -use bytes::BufMut; -use std::future::Future; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -pub(crate) fn read_buf<'a, R, B>(reader: &'a mut R, buf: &'a mut B) -> ReadBuf<'a, R, B> -where - R: AsyncRead + Unpin, - B: BufMut, -{ - ReadBuf { reader, buf } -} - -cfg_io_util! { - /// Future returned by [`read_buf`](crate::io::AsyncReadExt::read_buf). - #[derive(Debug)] - #[must_use = "futures do nothing unless you `.await` or poll them"] - pub struct ReadBuf<'a, R, B> { - reader: &'a mut R, - buf: &'a mut B, - } -} - -impl Future for ReadBuf<'_, R, B> -where - R: AsyncRead + Unpin, - B: BufMut, -{ - type Output = io::Result; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let me = &mut *self; - Pin::new(&mut *me.reader).poll_read_buf(cx, me.buf) - } -} diff --git a/tokio/src/io/util/write_buf.rs b/tokio/src/io/util/write_buf.rs deleted file mode 100644 index cedfde64e6e..00000000000 --- a/tokio/src/io/util/write_buf.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::io::AsyncWrite; - -use bytes::Buf; -use std::future::Future; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -cfg_io_util! { - /// A future to write some of the buffer to an `AsyncWrite`. - #[derive(Debug)] - #[must_use = "futures do nothing unless you `.await` or poll them"] - pub struct WriteBuf<'a, W, B> { - writer: &'a mut W, - buf: &'a mut B, - } -} - -/// Tries to write some bytes from the given `buf` to the writer in an -/// asynchronous manner, returning a future. -pub(crate) fn write_buf<'a, W, B>(writer: &'a mut W, buf: &'a mut B) -> WriteBuf<'a, W, B> -where - W: AsyncWrite + Unpin, - B: Buf, -{ - WriteBuf { writer, buf } -} - -impl Future for WriteBuf<'_, W, B> -where - W: AsyncWrite + Unpin, - B: Buf, -{ - type Output = io::Result; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let me = &mut *self; - Pin::new(&mut *me.writer).poll_write_buf(cx, me.buf) - } -} diff --git a/tokio/src/net/tcp/split.rs b/tokio/src/net/tcp/split.rs index 9d99d7bdfbf..6e927f05855 100644 --- a/tokio/src/net/tcp/split.rs +++ b/tokio/src/net/tcp/split.rs @@ -12,7 +12,6 @@ use crate::future::poll_fn; use crate::io::{AsyncRead, AsyncWrite, ReadBuf}; use crate::net::TcpStream; -use bytes::Buf; use std::io; use std::net::Shutdown; use std::pin::Pin; @@ -148,14 +147,6 @@ impl AsyncWrite for WriteHalf<'_> { self.0.poll_write_priv(cx, buf) } - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - self.0.poll_write_buf_priv(cx, buf) - } - #[inline] fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { // tcp flush is a no-op diff --git a/tokio/src/net/tcp/split_owned.rs b/tokio/src/net/tcp/split_owned.rs index 87be6efd8a1..2f35f495ca2 100644 --- a/tokio/src/net/tcp/split_owned.rs +++ b/tokio/src/net/tcp/split_owned.rs @@ -12,7 +12,6 @@ use crate::future::poll_fn; use crate::io::{AsyncRead, AsyncWrite, ReadBuf}; use crate::net::TcpStream; -use bytes::Buf; use std::error::Error; use std::net::Shutdown; use std::pin::Pin; @@ -230,14 +229,6 @@ impl AsyncWrite for OwnedWriteHalf { self.inner.poll_write_priv(cx, buf) } - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - self.inner.poll_write_buf_priv(cx, buf) - } - #[inline] fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { // tcp flush is a no-op diff --git a/tokio/src/net/tcp/stream.rs b/tokio/src/net/tcp/stream.rs index f4f705b40e4..4bf17449170 100644 --- a/tokio/src/net/tcp/stream.rs +++ b/tokio/src/net/tcp/stream.rs @@ -4,8 +4,6 @@ use crate::net::tcp::split::{split, ReadHalf, WriteHalf}; use crate::net::tcp::split_owned::{split_owned, OwnedReadHalf, OwnedWriteHalf}; use crate::net::ToSocketAddrs; -use bytes::Buf; -use iovec::IoVec; use std::convert::TryFrom; use std::fmt; use std::io::{self, Read, Write}; @@ -745,110 +743,6 @@ impl TcpStream { } } } - - pub(super) fn poll_write_buf_priv( - &self, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - use std::io::IoSlice; - - loop { - let ev = ready!(self.io.poll_write_ready(cx))?; - - // The `IoVec` (v0.1.x) type can't have a zero-length size, so create - // a dummy version from a 1-length slice which we'll overwrite with - // the `bytes_vectored` method. - static S: &[u8] = &[0]; - const MAX_BUFS: usize = 64; - - // IoSlice isn't Copy, so we must expand this manually ;_; - let mut slices: [IoSlice<'_>; MAX_BUFS] = [ - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - IoSlice::new(S), - ]; - let cnt = buf.bytes_vectored(&mut slices); - - let iovec = <&IoVec>::from(S); - let mut vecs = [iovec; MAX_BUFS]; - for i in 0..cnt { - vecs[i] = (*slices[i]).into(); - } - - match self.io.get_ref().write_bufs(&vecs[..cnt]) { - Ok(n) => { - buf.advance(n); - return Poll::Ready(Ok(n)); - } - Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { - self.io.clear_readiness(ev); - } - Err(e) => return Poll::Ready(Err(e)), - } - } - } } impl TryFrom for mio::net::TcpStream { @@ -893,14 +787,6 @@ impl AsyncWrite for TcpStream { self.poll_write_priv(cx, buf) } - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - self.poll_write_buf_priv(cx, buf) - } - #[inline] fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { // tcp flush is a no-op diff --git a/tokio/tests/io_async_read.rs b/tokio/tests/io_async_read.rs index d1aae9a1a7f..aaeadfa4c11 100644 --- a/tokio/tests/io_async_read.rs +++ b/tokio/tests/io_async_read.rs @@ -1,113 +1,10 @@ #![warn(rust_2018_idioms)] #![cfg(feature = "full")] -use tokio::io::{AsyncRead, ReadBuf}; -use tokio_test::task; -use tokio_test::{assert_ready_err, assert_ready_ok}; - -use bytes::BytesMut; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; +use tokio::io::AsyncRead; #[test] fn assert_obj_safe() { fn _assert() {} _assert::>(); } - -#[test] -fn read_buf_success() { - struct Rd; - - impl AsyncRead for Rd { - fn poll_read( - self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &mut ReadBuf<'_>, - ) -> Poll> { - buf.append(b"hello world"); - Poll::Ready(Ok(())) - } - } - - let mut buf = BytesMut::with_capacity(65); - - task::spawn(Rd).enter(|cx, rd| { - let n = assert_ready_ok!(rd.poll_read_buf(cx, &mut buf)); - - assert_eq!(11, n); - assert_eq!(buf[..], b"hello world"[..]); - }); -} - -#[test] -fn read_buf_error() { - struct Rd; - - impl AsyncRead for Rd { - fn poll_read( - self: Pin<&mut Self>, - _cx: &mut Context<'_>, - _buf: &mut ReadBuf<'_>, - ) -> Poll> { - let err = io::ErrorKind::Other.into(); - Poll::Ready(Err(err)) - } - } - - let mut buf = BytesMut::with_capacity(65); - - task::spawn(Rd).enter(|cx, rd| { - let err = assert_ready_err!(rd.poll_read_buf(cx, &mut buf)); - assert_eq!(err.kind(), io::ErrorKind::Other); - }); -} - -#[test] -fn read_buf_no_capacity() { - struct Rd; - - impl AsyncRead for Rd { - fn poll_read( - self: Pin<&mut Self>, - _cx: &mut Context<'_>, - _buf: &mut ReadBuf<'_>, - ) -> Poll> { - unimplemented!(); - } - } - - let mut buf = [0u8; 0]; - - task::spawn(Rd).enter(|cx, rd| { - let n = assert_ready_ok!(rd.poll_read_buf(cx, &mut &mut buf[..])); - assert_eq!(0, n); - }); -} - -#[test] -fn read_buf_uninitialized_ok() { - struct Rd; - - impl AsyncRead for Rd { - fn poll_read( - self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &mut ReadBuf<'_>, - ) -> Poll> { - assert_eq!(buf.remaining(), 64); - assert_eq!(buf.filled().len(), 0); - assert_eq!(buf.initialized().len(), 0); - Poll::Ready(Ok(())) - } - } - - // Can't create BytesMut w/ zero capacity, so fill it up - let mut buf = BytesMut::with_capacity(64); - - task::spawn(Rd).enter(|cx, rd| { - let n = assert_ready_ok!(rd.poll_read_buf(cx, &mut buf)); - assert_eq!(0, n); - }); -} From 673f0d60d63443b5c8b0c5bd7dbc770e77c16c1e Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 24 Sep 2020 15:30:44 -0700 Subject: [PATCH 2/5] fmt --- tokio-util/src/io/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tokio-util/src/io/mod.rs b/tokio-util/src/io/mod.rs index 9e8e7255492..09a69c1310e 100644 --- a/tokio-util/src/io/mod.rs +++ b/tokio-util/src/io/mod.rs @@ -20,7 +20,11 @@ use std::io; use std::pin::Pin; use std::task::{Context, Poll}; -pub(crate) fn poll_read_buf(cx: &mut Context<'_>, io: Pin<&mut T>, buf: &mut impl BufMut) -> Poll> { +pub(crate) fn poll_read_buf( + cx: &mut Context<'_>, + io: Pin<&mut T>, + buf: &mut impl BufMut, +) -> Poll> { if !buf.has_remaining_mut() { return Poll::Ready(Ok(0)); } @@ -35,4 +39,4 @@ pub(crate) fn poll_read_buf(cx: &mut Context<'_>, io: Pin<&mut T>, buf.advance_mut(n); } Poll::Ready(Ok(n)) -} \ No newline at end of file +} From 7415cad10b74598c076d7908c137c9d6a09eced5 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 24 Sep 2020 15:49:07 -0700 Subject: [PATCH 3/5] fix feature build --- tokio-util/src/codec/framed_impl.rs | 2 +- tokio-util/src/io/mod.rs | 29 -------------------------- tokio-util/src/io/reader_stream.rs | 2 +- tokio-util/src/lib.rs | 32 +++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/tokio-util/src/codec/framed_impl.rs b/tokio-util/src/codec/framed_impl.rs index 8ee20052e20..c161808f66e 100644 --- a/tokio-util/src/codec/framed_impl.rs +++ b/tokio-util/src/codec/framed_impl.rs @@ -118,7 +118,7 @@ where type Item = Result; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - use crate::io::poll_read_buf; + use crate::util::poll_read_buf; let mut pinned = self.project(); let state: &mut ReadFrame = pinned.state.borrow_mut(); diff --git a/tokio-util/src/io/mod.rs b/tokio-util/src/io/mod.rs index 09a69c1310e..53066c4e444 100644 --- a/tokio-util/src/io/mod.rs +++ b/tokio-util/src/io/mod.rs @@ -11,32 +11,3 @@ mod stream_reader; pub use self::reader_stream::ReaderStream; pub use self::stream_reader::StreamReader; - -use tokio::io::{AsyncRead, ReadBuf}; - -use bytes::BufMut; -use futures_core::ready; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -pub(crate) fn poll_read_buf( - cx: &mut Context<'_>, - io: Pin<&mut T>, - buf: &mut impl BufMut, -) -> Poll> { - if !buf.has_remaining_mut() { - return Poll::Ready(Ok(0)); - } - - let mut b = ReadBuf::uninit(buf.bytes_mut()); - - ready!(io.poll_read(cx, &mut b))?; - let n = b.filled().len(); - - // Safety: we can assume `n` bytes were read, since they are in`filled`. - unsafe { - buf.advance_mut(n); - } - Poll::Ready(Ok(n)) -} diff --git a/tokio-util/src/io/reader_stream.rs b/tokio-util/src/io/reader_stream.rs index dcfd2242b6d..ab0c22fba73 100644 --- a/tokio-util/src/io/reader_stream.rs +++ b/tokio-util/src/io/reader_stream.rs @@ -70,7 +70,7 @@ impl ReaderStream { impl Stream for ReaderStream { type Item = std::io::Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - use crate::io::poll_read_buf; + use crate::util::poll_read_buf; let mut this = self.as_mut().project(); diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs index b96d9044ea1..ce17da92382 100644 --- a/tokio-util/src/lib.rs +++ b/tokio-util/src/lib.rs @@ -52,3 +52,35 @@ pub mod context; pub mod sync; pub mod either; + +#[cfg(any(feature = "io", feature = "codec"))] +mod util { + use tokio::io::{AsyncRead, ReadBuf}; + + use bytes::BufMut; + use futures_core::ready; + use std::io; + use std::pin::Pin; + use std::task::{Context, Poll}; + + pub(crate) fn poll_read_buf( + cx: &mut Context<'_>, + io: Pin<&mut T>, + buf: &mut impl BufMut, + ) -> Poll> { + if !buf.has_remaining_mut() { + return Poll::Ready(Ok(0)); + } + + let mut b = ReadBuf::uninit(buf.bytes_mut()); + + ready!(io.poll_read(cx, &mut b))?; + let n = b.filled().len(); + + // Safety: we can assume `n` bytes were read, since they are in`filled`. + unsafe { + buf.advance_mut(n); + } + Poll::Ready(Ok(n)) + } +} \ No newline at end of file From 086c5ff544621e90f462ab3a91956b6e700fe6de Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 24 Sep 2020 15:53:22 -0700 Subject: [PATCH 4/5] fix feedback --- tokio-util/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs index ce17da92382..a5ee45f1f1c 100644 --- a/tokio-util/src/lib.rs +++ b/tokio-util/src/lib.rs @@ -72,12 +72,14 @@ mod util { return Poll::Ready(Ok(0)); } + let orig = buf.bytes_mut().as_ptr() as *const u8; let mut b = ReadBuf::uninit(buf.bytes_mut()); ready!(io.poll_read(cx, &mut b))?; let n = b.filled().len(); // Safety: we can assume `n` bytes were read, since they are in`filled`. + assert_eq!(orig, b.filled().as_ptr()); unsafe { buf.advance_mut(n); } From 405938ef9dd1cdf32d378d79a51a631cd209dd01 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 24 Sep 2020 15:53:33 -0700 Subject: [PATCH 5/5] fmt --- tokio-util/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs index a5ee45f1f1c..eb35345e796 100644 --- a/tokio-util/src/lib.rs +++ b/tokio-util/src/lib.rs @@ -85,4 +85,4 @@ mod util { } Poll::Ready(Ok(n)) } -} \ No newline at end of file +}