summaryrefslogtreecommitdiffstats
path: root/tokio-util/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tokio-util/src/lib.rs')
-rw-r--r--tokio-util/src/lib.rs45
1 files changed, 42 insertions, 3 deletions
diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs
index 253f4437..09dd5a10 100644
--- a/tokio-util/src/lib.rs
+++ b/tokio-util/src/lib.rs
@@ -69,10 +69,49 @@ mod util {
use std::pin::Pin;
use std::task::{Context, Poll};
- pub(crate) fn poll_read_buf<T: AsyncRead>(
- cx: &mut Context<'_>,
+ /// 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(Pin::new(&mut read), cx, &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(())
+ /// # }
+ /// ```
+ #[cfg_attr(not(feature = "io"), allow(unreachable_pub))]
+ pub fn poll_read_buf<T: AsyncRead, B: BufMut>(
io: Pin<&mut T>,
- buf: &mut impl BufMut,
+ cx: &mut Context<'_>,
+ buf: &mut B,
) -> Poll<io::Result<usize>> {
if !buf.has_remaining_mut() {
return Poll::Ready(Ok(0));