From 060b85bd695f231f31888b371e8e2fdf59d74702 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 30 Oct 2020 20:26:26 +0100 Subject: [PATCH 1/5] util: add back public poll_read_buf() function This was accidentally removed in #3064. --- tokio-util/CHANGELOG.md | 6 +++--- tokio-util/src/io/mod.rs | 1 + tokio-util/src/lib.rs | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/tokio-util/CHANGELOG.md b/tokio-util/CHANGELOG.md index 64228895200..48dfe778e26 100644 --- a/tokio-util/CHANGELOG.md +++ b/tokio-util/CHANGELOG.md @@ -1,11 +1,11 @@ +### Added +- io: `poll_read_buf` util fn (#2972). + # 0.5.0 (October 30, 2020) ### Changed - io: update `bytes` to 0.6 (#3071). -### Added -- io: `poll_read_buf` util fn (#2972). - # 0.4.0 (October 15, 2020) ### Added diff --git a/tokio-util/src/io/mod.rs b/tokio-util/src/io/mod.rs index 6f181ab1703..ceb3ac27185 100644 --- a/tokio-util/src/io/mod.rs +++ b/tokio-util/src/io/mod.rs @@ -10,6 +10,7 @@ mod read_buf; mod reader_stream; mod stream_reader; +pub use crate::util::poll_read_buf; pub use self::read_buf::read_buf; pub use self::reader_stream::ReaderStream; pub use self::stream_reader::StreamReader; diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs index 253f4437aac..7c0b0aad092 100644 --- a/tokio-util/src/lib.rs +++ b/tokio-util/src/lib.rs @@ -69,7 +69,45 @@ mod util { use std::pin::Pin; use std::task::{Context, Poll}; - pub(crate) fn poll_read_buf( + /// Try to read data from an `AsyncRead` into an implementer of the [`Buf`] trait. + /// + /// [`Buf`]: bytes::Buf + /// + /// # Example + /// + /// ``` + /// use bytes::{Bytes, BytesMut}; + /// use tokio::stream; + /// use tokio::io::Result; + /// use tokio_util::io::{StreamReader, poll_read_buf}; + /// use futures::future::poll_fn; + /// use std::pin::Pin; + /// # #[tokio::main] + /// # async fn main() -> std::io::Result<()> { + /// + /// // Create a reader from an iterator. This particular reader will always be + /// // ready. + /// let mut read = StreamReader::new(stream::iter(vec![Result::Ok(Bytes::from_static(&[0, 1, 2, 3]))])); + /// + /// let mut buf = BytesMut::new(); + /// let mut reads = 0; + /// + /// loop { + /// reads += 1; + /// let n = poll_fn(|cx| poll_read_buf(cx, Pin::new(&mut read), &mut buf)).await?; + /// + /// if n == 0 { + /// break; + /// } + /// } + /// + /// // one or more reads might be necessary. + /// assert!(reads >= 1); + /// assert_eq!(&buf[..], &[0, 1, 2, 3]); + /// # Ok(()) + /// # } + /// ``` + pub fn poll_read_buf( cx: &mut Context<'_>, io: Pin<&mut T>, buf: &mut impl BufMut, From e56aa530057ac9e2e6754200ec1b5f936f508518 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 31 Oct 2020 11:52:20 +0100 Subject: [PATCH 2/5] Update tokio-util/src/lib.rs Co-authored-by: Alice Ryhl --- tokio-util/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs index 7c0b0aad092..bc45c5129e2 100644 --- a/tokio-util/src/lib.rs +++ b/tokio-util/src/lib.rs @@ -107,6 +107,7 @@ mod util { /// # Ok(()) /// # } /// ``` + #[cfg_attr(not(feature = "io"), allow(unreachable_pub))] pub fn poll_read_buf( cx: &mut Context<'_>, io: Pin<&mut T>, From df068f97f9503e98e58fc3f88b55168f626fd5b4 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 31 Oct 2020 11:52:27 +0100 Subject: [PATCH 3/5] Update tokio-util/src/lib.rs Co-authored-by: Alice Ryhl --- tokio-util/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs index bc45c5129e2..46640cafea7 100644 --- a/tokio-util/src/lib.rs +++ b/tokio-util/src/lib.rs @@ -108,10 +108,10 @@ mod util { /// # } /// ``` #[cfg_attr(not(feature = "io"), allow(unreachable_pub))] - pub fn poll_read_buf( - cx: &mut Context<'_>, + pub fn poll_read_buf( io: Pin<&mut T>, - buf: &mut impl BufMut, + cx: &mut Context<'_>, + buf: &mut B, ) -> Poll> { if !buf.has_remaining_mut() { return Poll::Ready(Ok(0)); From 8a73e8061dd165a57c14199ebf7b4e276dbf8487 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 31 Oct 2020 11:52:37 +0100 Subject: [PATCH 4/5] Update tokio-util/src/io/mod.rs Co-authored-by: Alice Ryhl --- tokio-util/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio-util/src/io/mod.rs b/tokio-util/src/io/mod.rs index ceb3ac27185..eefd65a5e83 100644 --- a/tokio-util/src/io/mod.rs +++ b/tokio-util/src/io/mod.rs @@ -10,7 +10,7 @@ mod read_buf; mod reader_stream; mod stream_reader; -pub use crate::util::poll_read_buf; pub use self::read_buf::read_buf; pub use self::reader_stream::ReaderStream; pub use self::stream_reader::StreamReader; +pub use crate::util::poll_read_buf; From 1a8863e3c01c9467beb74185678996e099bf0b65 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 31 Oct 2020 12:29:57 +0100 Subject: [PATCH 5/5] Fix up argument order --- tokio-util/src/codec/framed_impl.rs | 2 +- tokio-util/src/io/read_buf.rs | 2 +- tokio-util/src/io/reader_stream.rs | 2 +- tokio-util/src/lib.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tokio-util/src/codec/framed_impl.rs b/tokio-util/src/codec/framed_impl.rs index c161808f66e..e8b29999e10 100644 --- a/tokio-util/src/codec/framed_impl.rs +++ b/tokio-util/src/codec/framed_impl.rs @@ -150,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 poll_read_buf(cx, pinned.inner.as_mut(), &mut state.buffer)? { + let bytect = match poll_read_buf(pinned.inner.as_mut(), cx, &mut state.buffer)? { Poll::Ready(ct) => ct, Poll::Pending => return Poll::Pending, }; diff --git a/tokio-util/src/io/read_buf.rs b/tokio-util/src/io/read_buf.rs index 5bc0d5866f4..cc3c505f934 100644 --- a/tokio-util/src/io/read_buf.rs +++ b/tokio-util/src/io/read_buf.rs @@ -59,7 +59,7 @@ where fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = &mut *self; - crate::util::poll_read_buf(cx, Pin::new(this.0), this.1) + crate::util::poll_read_buf(Pin::new(this.0), cx, this.1) } } } diff --git a/tokio-util/src/io/reader_stream.rs b/tokio-util/src/io/reader_stream.rs index ab0c22fba73..3e6a05eff4d 100644 --- a/tokio-util/src/io/reader_stream.rs +++ b/tokio-util/src/io/reader_stream.rs @@ -83,7 +83,7 @@ impl Stream for ReaderStream { this.buf.reserve(CAPACITY); } - match poll_read_buf(cx, reader, &mut this.buf) { + match poll_read_buf(reader, cx, &mut this.buf) { Poll::Pending => Poll::Pending, Poll::Ready(Err(err)) => { self.project().reader.set(None); diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs index 46640cafea7..09dd5a1071c 100644 --- a/tokio-util/src/lib.rs +++ b/tokio-util/src/lib.rs @@ -94,7 +94,7 @@ mod util { /// /// loop { /// reads += 1; - /// let n = poll_fn(|cx| poll_read_buf(cx, Pin::new(&mut read), &mut buf)).await?; + /// let n = poll_fn(|cx| poll_read_buf(Pin::new(&mut read), cx, &mut buf)).await?; /// /// if n == 0 { /// break;