Skip to content

Commit

Permalink
util: add back public poll_read_buf() function
Browse files Browse the repository at this point in the history
This was accidentally removed in #3064.
  • Loading branch information
djc committed Oct 30, 2020
1 parent 24ed874 commit 060b85b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
6 changes: 3 additions & 3 deletions 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
Expand Down
1 change: 1 addition & 0 deletions tokio-util/src/io/mod.rs
Expand Up @@ -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;
40 changes: 39 additions & 1 deletion tokio-util/src/lib.rs
Expand Up @@ -69,7 +69,45 @@ mod util {
use std::pin::Pin;
use std::task::{Context, Poll};

pub(crate) fn poll_read_buf<T: AsyncRead>(
/// 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<T: AsyncRead>(
cx: &mut Context<'_>,
io: Pin<&mut T>,
buf: &mut impl BufMut,
Expand Down

0 comments on commit 060b85b

Please sign in to comment.