MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / AsyncRead

Interface AsyncRead

src/io/read.rs:6–34  ·  view source on GitHub ↗

Read bytes from a source.

Source from the content-addressed store, hash-verified

4
5/// Read bytes from a source.
6pub 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
36impl<R: AsyncRead + ?Sized> AsyncRead for &mut R {
37 #[inline]

Callers

nothing calls this directly

Implementers 6

read.rssrc/io/read.rs
streams.rssrc/io/streams.rs
stdio.rssrc/io/stdio.rs
empty.rssrc/io/empty.rs
cursor.rssrc/io/cursor.rs
tcp_stream.rssrc/net/tcp_stream.rs

Calls

no outgoing calls

Tested by

no test coverage detected