diff options
author | Carl Lerche <me@carllerche.com> | 2020-09-24 17:26:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-24 17:26:03 -0700 |
commit | 4186b0aa38abbec7670d53882d5cdfd4b12add5c (patch) | |
tree | b067117fcb1a4c479cd274465bcac0431c2e59f7 /tokio/src/io | |
parent | 760ae89401d9addb71ebf19674980577b5501edd (diff) |
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
Diffstat (limited to 'tokio/src/io')
-rw-r--r-- | tokio/src/io/async_read.rs | 31 | ||||
-rw-r--r-- | tokio/src/io/async_write.rs | 22 | ||||
-rw-r--r-- | tokio/src/io/split.rs | 19 | ||||
-rw-r--r-- | tokio/src/io/util/async_read_ext.rs | 68 | ||||
-rw-r--r-- | tokio/src/io/util/async_write_ext.rs | 76 | ||||
-rw-r--r-- | tokio/src/io/util/buf_reader.rs | 9 | ||||
-rw-r--r-- | tokio/src/io/util/mod.rs | 2 | ||||
-rw-r--r-- | tokio/src/io/util/read_buf.rs | 38 | ||||
-rw-r--r-- | tokio/src/io/util/write_buf.rs | 40 |
9 files changed, 0 insertions, 305 deletions
diff --git a/tokio/src/io/async_read.rs b/tokio/src/io/async_read.rs index d341b63d..ba2303d1 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<io::Result<()>>; - - /// 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<B: BufMut>( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll<io::Result<usize>> - 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 ecf7575b..66ba4bf3 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<Result<(), io::Error>>; - - /// 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<B: Buf>( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll<Result<usize, io::Error>> - 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 dcd3da20..fd3273ee 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<T: AsyncRead> AsyncRead for ReadHalf<T> { let mut inner = ready!(self.inner.poll_lock(cx)); inner.stream_pin().poll_read(cx, buf) } - - fn poll_read_buf<B: BufMut>( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll<io::Result<usize>> { - let mut inner = ready!(self.inner.poll_lock(cx)); - inner.stream_pin().poll_read_buf(cx, buf) - } } impl<T: AsyncWrite> AsyncWrite for WriteHalf<T> { @@ -137,15 +127,6 @@ impl<T: AsyncWrite> AsyncWrite for WriteHalf<T> { let mut inner = ready!(self.inner.poll_lock(cx)); inner.stream_pin().poll_shutdown(cx) } - - fn poll_write_buf<B: Buf>( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll<Result<usize, io::Error>> { - let mut inner = ready!(self.inner.poll_lock(cx)); - inner.stream_pin().poll_write_buf(cx, buf) - } } impl<T> Inner<T> { diff --git a/tokio/src/io/util/async_read_ext.rs b/tokio/src/io/util/async_read_ext.rs index 0ab66c28..d631bd7e 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<B: BufMut>(&mut self, buf: &mut B) -> io::Result<usize>; - /// ``` - /// - /// 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 321301e2..5c6187b7 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<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize>; - /// ``` - /// - /// 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 3ab78f0e..9264ca59 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<R: AsyncRead + AsyncWrite> AsyncWrite for BufReader<R> { self.get_pin_mut().poll_write(cx, buf) } - fn poll_write_buf<B: Buf>( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll<io::Result<usize>> { - self.get_pin_mut().poll_write_buf(cx, buf) - } - fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { self.get_pin_mut().poll_flush(cx) } diff --git a/tokio/src/io/util/mod.rs b/tokio/src/io/util/mod.rs index 1bd0a3f8..52dab990 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 6ee3d249..00000000 --- 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<R, B> Future for ReadBuf<'_, R, B> -where - R: AsyncRead + Unpin, - B: BufMut, -{ - type Output = io::Result<usize>; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> { - 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 cedfde64..00000000 --- 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<W, B> Future for WriteBuf<'_, W, B> -where - W: AsyncWrite + Unpin, - B: Buf, -{ - type Output = io::Result<usize>; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> { - let me = &mut *self; - Pin::new(&mut *me.writer).poll_write_buf(cx, me.buf) - } -} |