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

util: add back public poll_read_buf() function #3079

Merged
merged 5 commits into from Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
djc marked this conversation as resolved.
Show resolved Hide resolved
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>(
djc marked this conversation as resolved.
Show resolved Hide resolved
cx: &mut Context<'_>,
io: Pin<&mut T>,
buf: &mut impl BufMut,
djc marked this conversation as resolved.
Show resolved Hide resolved
Expand Down