pub trait Read: ErrorType {
// Required method
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
// Provided method
fn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), ReadExactError<Self::Error>> { ... }
}
Expand description
Blocking reader.
This trait is the embedded-io
equivalent of std::io::Read
.
Required Methods§
Sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Read some bytes from this source into the specified buffer, returning how many bytes were read.
If no bytes are currently available to read:
- The method blocks until at least one byte becomes available;
- Once at least one (or more) bytes become available, a non-zero amount of those is copied to the
beginning of
buf
, and the amount is returned, without waiting or blocking any further for more bytes to become available.
If bytes are available to read:
- A non-zero amount of bytes is read to the beginning of
buf
, and the amount is returned immediately, without blocking and waiting for more bytes to become available;
Note that once some bytes are available to read, it is not guaranteed that all available bytes are returned.
It is possible for the implementation to read an amount of bytes less than buf.len()
while there are more
bytes immediately available.
This blocking behavior is important for the cases where Read
represents the “read” leg of a pipe-like
protocol (a socket, a pipe, a serial line etc.). The semantics is that the caller - by passing a non-empty
buffer - does expect some data (one or more bytes) - but not necessarily buf.len()
or more bytes -
to become available, before the peer represented by Read
would stop sending bytes due to
application-specific reasons (as in the peer waiting for a response to the data it had sent so far).
If the reader is at end-of-file (EOF), Ok(0)
is returned. There is no guarantee that a reader at EOF
will always be so in the future, for example a reader can stop being at EOF if another process appends
more bytes to the underlying file.
If buf.len() == 0
, read
returns without blocking, with either Ok(0)
or an error.
The Ok(0)
doesn’t indicate EOF, unlike when called with a non-empty buffer.
Provided Methods§
Sourcefn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), ReadExactError<Self::Error>>
fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
Read the exact number of bytes required to fill buf
.
This function calls read()
in a loop until exactly buf.len()
bytes have
been read, blocking if needed.
If you are using ReadReady
to avoid blocking, you should not use this function.
ReadReady::read_ready()
returning true only guarantees the first call to read()
will
not block, so this function may still block in subsequent calls.
Implementations on Foreign Types§
Source§impl Read for &[u8]
Read is implemented for &[u8]
by copying from the slice.
impl Read for &[u8]
Read is implemented for &[u8]
by copying from the slice.
Note that reading updates the slice to point to the yet unread part. The slice will be empty when EOF is reached.
Source§impl Read for VecDeque<u8>
Available on crate features std
or alloc
only.Read is implemented for VecDeque<u8>
by consuming bytes from the front of the VecDeque
.
impl Read for VecDeque<u8>
std
or alloc
only.Read is implemented for VecDeque<u8>
by consuming bytes from the front of the VecDeque
.
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Fill buf
with the contents of the “front” slice as returned by
as_slices
. If the contained byte slices of the VecDeque
are
discontiguous, multiple calls to read
will be needed to read the entire content.