@@ -69,10 +69,49 @@ mod util {
69
69
use std:: pin:: Pin ;
70
70
use std:: task:: { Context , Poll } ;
71
71
72
- pub ( crate ) fn poll_read_buf < T : AsyncRead > (
73
- cx : & mut Context < ' _ > ,
72
+ /// Try to read data from an `AsyncRead` into an implementer of the [`Buf`] trait.
73
+ ///
74
+ /// [`Buf`]: bytes::Buf
75
+ ///
76
+ /// # Example
77
+ ///
78
+ /// ```
79
+ /// use bytes::{Bytes, BytesMut};
80
+ /// use tokio::stream;
81
+ /// use tokio::io::Result;
82
+ /// use tokio_util::io::{StreamReader, poll_read_buf};
83
+ /// use futures::future::poll_fn;
84
+ /// use std::pin::Pin;
85
+ /// # #[tokio::main]
86
+ /// # async fn main() -> std::io::Result<()> {
87
+ ///
88
+ /// // Create a reader from an iterator. This particular reader will always be
89
+ /// // ready.
90
+ /// let mut read = StreamReader::new(stream::iter(vec![Result::Ok(Bytes::from_static(&[0, 1, 2, 3]))]));
91
+ ///
92
+ /// let mut buf = BytesMut::new();
93
+ /// let mut reads = 0;
94
+ ///
95
+ /// loop {
96
+ /// reads += 1;
97
+ /// let n = poll_fn(|cx| poll_read_buf(Pin::new(&mut read), cx, &mut buf)).await?;
98
+ ///
99
+ /// if n == 0 {
100
+ /// break;
101
+ /// }
102
+ /// }
103
+ ///
104
+ /// // one or more reads might be necessary.
105
+ /// assert!(reads >= 1);
106
+ /// assert_eq!(&buf[..], &[0, 1, 2, 3]);
107
+ /// # Ok(())
108
+ /// # }
109
+ /// ```
110
+ #[ cfg_attr( not( feature = "io" ) , allow( unreachable_pub) ) ]
111
+ pub fn poll_read_buf < T : AsyncRead , B : BufMut > (
74
112
io : Pin < & mut T > ,
75
- buf : & mut impl BufMut ,
113
+ cx : & mut Context < ' _ > ,
114
+ buf : & mut B ,
76
115
) -> Poll < io:: Result < usize > > {
77
116
if !buf. has_remaining_mut ( ) {
78
117
return Poll :: Ready ( Ok ( 0 ) ) ;
0 commit comments