We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 84519fd commit 372e366Copy full SHA for 372e366
src/io/read.rs
@@ -1,6 +1,27 @@
1
use crate::io;
2
3
+const CHUNK_SIZE: usize = 2048;
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
}
0 commit comments