Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add a poll_read_exact method to AsyncRead #7211

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tokio-util/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ where
) -> Poll<Result<()>> {
delegate_call!(self.poll_read(cx, buf))
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
delegate_call!(self.poll_read_exact(cx, buf))
}
}

impl<L, R> AsyncBufRead for Either<L, R>
Expand Down
8 changes: 8 additions & 0 deletions tokio-util/src/io/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,12 @@ impl<W: AsyncRead, F> AsyncRead for InspectWriter<W, F> {
) -> Poll<std::io::Result<()>> {
self.project().writer.poll_read(cx, buf)
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
self.project().writer.poll_read_exact(cx, buf)
}
}
8 changes: 8 additions & 0 deletions tokio-util/src/io/sink_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,12 @@ impl<S: AsyncRead> AsyncRead for SinkWriter<S> {
) -> Poll<io::Result<()>> {
self.project().inner.poll_read(cx, buf)
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.project().inner.poll_read_exact(cx, buf)
}
}
91 changes: 90 additions & 1 deletion tokio/src/io/async_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@ use super::ReadBuf;
use std::io;
use std::ops::DerefMut;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::task::{ready, Context, Poll};

#[cold]
pub(crate) fn eof() -> io::Error {
io::Error::new(io::ErrorKind::UnexpectedEof, "early eof")
}

pub(crate) fn default_poll_read_exact<R: AsyncRead + ?Sized>(
mut reader: Pin<&mut R>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
loop {
// if our buffer is empty, then we need to read some data to continue.
let rem = buf.remaining();
if rem != 0 {
ready!(reader.as_mut().poll_read(cx, buf))?;
if buf.remaining() == rem {
return Poll::Ready(Err(eof()));
}
} else {
return Poll::Ready(Ok(()));
}
}
}

/// Reads bytes from a source.
///
Expand Down Expand Up @@ -56,6 +80,25 @@ pub trait AsyncRead {
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>>;

/// Attempts to read the exact number of bytes required to fill `buf`.
///
/// On success, returns `Poll::Ready(Ok(()))` and places data in the
/// unfilled portion of `buf`.
///
/// If EOF was reached, this function returns an error of kind
/// `ErrorKind::UnexpectedEof`.
///
/// If an error occurs or `Poll::Pending` is returned, `buf` contains
/// all bytes read so far. This means that calling `poll_read_exact`
/// with the same `ReadBuf` until it succeeds will work as intended.
fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
default_poll_read_exact(self, cx, buf)
}
}

macro_rules! deref_async_read {
Expand All @@ -67,6 +110,14 @@ macro_rules! deref_async_read {
) -> Poll<io::Result<()>> {
Pin::new(&mut **self).poll_read(cx, buf)
}

fn poll_read_exact(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut **self).poll_read_exact(cx, buf)
}
};
}

Expand All @@ -90,6 +141,14 @@ where
) -> Poll<io::Result<()>> {
crate::util::pin_as_deref_mut(self).poll_read(cx, buf)
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
crate::util::pin_as_deref_mut(self).poll_read_exact(cx, buf)
}
}

impl AsyncRead for &[u8] {
Expand All @@ -104,6 +163,21 @@ impl AsyncRead for &[u8] {
*self = b;
Poll::Ready(Ok(()))
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let _ = self.poll_read(cx, buf);

// If there was enough data, `poll_read` has filled the buffer
if buf.remaining() == 0 {
Poll::Ready(Ok(()))
} else {
Poll::Ready(Err(eof()))
}
}
}

impl<T: AsRef<[u8]> + Unpin> AsyncRead for io::Cursor<T> {
Expand All @@ -129,4 +203,19 @@ impl<T: AsRef<[u8]> + Unpin> AsyncRead for io::Cursor<T> {

Poll::Ready(Ok(()))
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let _ = self.poll_read(cx, buf);

// If there was enough data, `poll_read` has filled the buffer
if buf.remaining() == 0 {
Poll::Ready(Ok(()))
} else {
Poll::Ready(Err(eof()))
}
}
}
8 changes: 8 additions & 0 deletions tokio/src/io/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ where
) -> Poll<Result<(), io::Error>> {
self.project().reader.poll_read(cx, buf)
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.project().reader.poll_read_exact(cx, buf)
}
}

impl<R, W> AsyncWrite for Join<R, W>
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/io/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ impl<T: AsyncRead> AsyncRead for ReadHalf<T> {
) -> Poll<io::Result<()>> {
self.inner.with_lock(|stream| stream.poll_read(cx, buf))
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.inner
.with_lock(|stream| stream.poll_read_exact(cx, buf))
}
}

impl<T: AsyncWrite> AsyncWrite for WriteHalf<T> {
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/io/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ impl AsyncRead for Stdin {
) -> Poll<io::Result<()>> {
Pin::new(&mut self.std).poll_read(cx, buf)
}

fn poll_read_exact(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.std).poll_read_exact(cx, buf)
}
}
25 changes: 25 additions & 0 deletions tokio/src/io/util/buf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ impl<R: AsyncRead> BufReader<R> {
*me.pos = 0;
*me.cap = 0;
}

#[cold]
fn poll_read_exact_slow(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
crate::io::async_read::default_poll_read_exact(self, cx, buf)
}
}

impl<R: AsyncRead> AsyncRead for BufReader<R> {
Expand All @@ -117,6 +126,22 @@ impl<R: AsyncRead> AsyncRead for BufReader<R> {
self.consume(amt);
Poll::Ready(Ok(()))
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
// Fast path: all the data we need is already in the buffer
if let Some(data) = self.buffer().get(..buf.remaining()) {
buf.put_slice(data);
*self.project().pos += data.len();
debug_assert!(buf.remaining() == 0);
return Poll::Ready(Ok(()));
}

self.poll_read_exact_slow(cx, buf)
}
}

impl<R: AsyncRead> AsyncBufRead for BufReader<R> {
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/io/util/buf_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ impl<RW: AsyncRead + AsyncWrite> AsyncRead for BufStream<RW> {
) -> Poll<io::Result<()>> {
self.project().inner.poll_read(cx, buf)
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.project().inner.poll_read_exact(cx, buf)
}
}

/// Seek to an offset, in bytes, in the underlying stream.
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/io/util/buf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ impl<W: AsyncWrite + AsyncRead> AsyncRead for BufWriter<W> {
) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_read(cx, buf)
}

fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.get_pin_mut().poll_read_exact(cx, buf)
}
}

impl<W: AsyncWrite + AsyncBufRead> AsyncBufRead for BufWriter<W> {
Expand Down
16 changes: 16 additions & 0 deletions tokio/src/io/util/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ impl AsyncRead for Empty {
ready!(poll_proceed_and_make_progress(cx));
Poll::Ready(Ok(()))
}

#[inline]
fn poll_read_exact(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
ready!(crate::trace::trace_leaf(cx));
ready!(poll_proceed_and_make_progress(cx));

if buf.remaining() == 0 {
Poll::Ready(Ok(()))
} else {
Poll::Ready(Err(crate::io::async_read::eof()))
}
}
}

impl AsyncBufRead for Empty {
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/io/util/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ impl AsyncRead for DuplexStream {
) -> Poll<std::io::Result<()>> {
Pin::new(&mut *self.read.lock()).poll_read(cx, buf)
}

#[allow(unused_mut)]
fn poll_read_exact(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut *self.read.lock()).poll_read_exact(cx, buf)
}
}

impl AsyncWrite for DuplexStream {
Expand Down
19 changes: 3 additions & 16 deletions tokio/src/io/util/read_exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ pin_project! {
}
}

fn eof() -> io::Error {
io::Error::new(io::ErrorKind::UnexpectedEof, "early eof")
}

impl<A> Future for ReadExact<'_, A>
where
A: AsyncRead + Unpin + ?Sized,
Expand All @@ -53,17 +49,8 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
let me = self.project();

loop {
// if our buffer is empty, then we need to read some data to continue.
let rem = me.buf.remaining();
if rem != 0 {
ready!(Pin::new(&mut *me.reader).poll_read(cx, me.buf))?;
if me.buf.remaining() == rem {
return Err(eof()).into();
}
} else {
return Poll::Ready(Ok(me.buf.capacity()));
}
}
ready!(Pin::new(&mut *me.reader).poll_read_exact(cx, me.buf))?;

Poll::Ready(Ok(me.buf.capacity()))
}
}
Loading