pub struct PeekReader<R, const BUFFER_SIZE: usize = 280> { /* private fields */ }
Expand description
A buffered/peekable reader
This reader wraps a type implementing Read
and adds buffering via an internal buffer.
It allows the user to peek
a specified number of bytes (without consuming them),
to read
bytes (consuming them), or to consume
them after peek
ing.
NOTE: This reader is generic over the size of the buffer, defaulting to MAVLink’s current largest possible message size of 280 bytes
Implementations§
Source§impl<R: Read, const BUFFER_SIZE: usize> PeekReader<R, BUFFER_SIZE>
impl<R: Read, const BUFFER_SIZE: usize> PeekReader<R, BUFFER_SIZE>
Sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Instantiates a new PeekReader
, wrapping the provided Read
er and using the default chunk size
Sourcepub fn peek_exact(&mut self, amount: usize) -> Result<&[u8], MessageReadError>
pub fn peek_exact(&mut self, amount: usize) -> Result<&[u8], MessageReadError>
Peeks an exact amount of bytes from the internal buffer
If the internal buffer does not contain enough data, this function will read
from the underlying Read
er until it does, an error occurs or no more data can be read (EOF).
This function does not consume data from the buffer, so subsequent calls to peek
or read
functions
will still return the peeked data.
§Errors
- If any error occurs while reading from the underlying
Read
er it is returned - If an EOF occurs and the specified amount could not be read, this function will return an
ErrorKind::UnexpectedEof
.
§Panics
Will panic when attempting to read more bytes then BUFFER_SIZE
Sourcepub fn read_exact(&mut self, amount: usize) -> Result<&[u8], MessageReadError>
pub fn read_exact(&mut self, amount: usize) -> Result<&[u8], MessageReadError>
Reads a specified amount of bytes from the internal buffer
If the internal buffer does not contain enough data, this function will read
from the underlying Read
er until it does, an error occurs or no more data can be read (EOF).
This function consumes the data from the buffer, unless an error occurs, in which case no data is consumed.
§Errors
- If any error occurs while reading from the underlying
Read
er it is returned - If an EOF occurs and the specified amount could not be read, this function will return an
ErrorKind::UnexpectedEof
.
§Panics
Will panic when attempting to read more bytes then BUFFER_SIZE
Sourcepub fn read_u8(&mut self) -> Result<u8, MessageReadError>
pub fn read_u8(&mut self) -> Result<u8, MessageReadError>
Reads a byte from the internal buffer
If the internal buffer does not contain enough data, this function will read
from the underlying Read
er until it does, an error occurs or no more data can be read (EOF).
This function consumes the data from the buffer, unless an error occurs, in which case no data is consumed.
§Errors
- If any error occurs while reading from the underlying
Read
er it is returned - If an EOF occurs before a byte could be read, this function will return an
ErrorKind::UnexpectedEof
.
§Panics
Will panic if this PeekReader
’s BUFFER_SIZE
is 0.
Sourcepub fn consume(&mut self, amount: usize) -> usize
pub fn consume(&mut self, amount: usize) -> usize
Consumes a specified amount of bytes from the buffer
If the internal buffer does not contain enough data, this function will consume as much data as is buffered.
Sourcepub fn reader_ref(&self) -> &R
pub fn reader_ref(&self) -> &R
Returns an immutable reference to the underlying Read
er
Reading directly from the underlying reader will cause data loss
Sourcepub fn reader_mut(&mut self) -> &mut R
pub fn reader_mut(&mut self) -> &mut R
Returns a mutable reference to the underlying Read
er
Reading directly from the underlying reader will cause data loss