Read bytes from a source.
| 4 | |
| 5 | /// Read bytes from a source. |
| 6 | pub trait AsyncRead { |
| 7 | async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>; |
| 8 | async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
| 9 | // total bytes written to buf |
| 10 | let mut n = 0; |
| 11 | |
| 12 | loop { |
| 13 | // grow buf if empty |
| 14 | if buf.len() == n { |
| 15 | buf.resize(n + CHUNK_SIZE, 0u8); |
| 16 | } |
| 17 | |
| 18 | let len = self.read(&mut buf[n..]).await?; |
| 19 | if len == 0 { |
| 20 | buf.truncate(n); |
| 21 | return Ok(n); |
| 22 | } |
| 23 | |
| 24 | n += len; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // If the `AsyncRead` implementation is an unbuffered wrapper around an |
| 29 | // `AsyncInputStream`, some I/O operations can be more efficient. |
| 30 | #[inline] |
| 31 | fn as_async_input_stream(&self) -> Option<&io::AsyncInputStream> { |
| 32 | None |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl<R: AsyncRead + ?Sized> AsyncRead for &mut R { |
| 37 | #[inline] |
nothing calls this directly
no outgoing calls
no test coverage detected