Struct mavlink::async_peek_reader::AsyncPeekReader
source · pub struct AsyncPeekReader<R, const BUFFER_SIZE: usize = 280> { /* private fields */ }
tokio-1
only.Expand description
A buffered/peekable reader
This reader wraps a type implementing tokio::io::AsyncRead
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, const BUFFER_SIZE: usize> AsyncPeekReader<R, BUFFER_SIZE>where
R: AsyncReadExt + Unpin,
impl<R, const BUFFER_SIZE: usize> AsyncPeekReader<R, BUFFER_SIZE>where
R: AsyncReadExt + Unpin,
sourcepub fn new(reader: R) -> AsyncPeekReader<R, BUFFER_SIZE>
pub fn new(reader: R) -> AsyncPeekReader<R, BUFFER_SIZE>
Instantiates a new AsyncPeekReader
, wrapping the provided tokio::io::AsyncReadExt
and using the default chunk size
sourcepub async fn peek_exact(
&mut self,
amount: usize,
) -> Result<&[u8], MessageReadError>
pub async 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 tokio::io::AsyncReadExt
until it does, an error occurs or no more data can be read (EOF).
If an EOF occurs and the specified amount could not be read, this function will return an [ErrorKind::UnexpectedEof
].
This function does not consume data from the buffer, so subsequent calls to peek
or read
functions
will still return the peeked data.
sourcepub async fn read_exact(
&mut self,
amount: usize,
) -> Result<&[u8], MessageReadError>
pub async 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 tokio::io::AsyncReadExt
until it does, an error occurs or no more data can be read (EOF).
If an EOF occurs and the specified amount could not be read, this function will return an [ErrorKind::UnexpectedEof
].
This function consumes the data from the buffer, unless an error occurs, in which case no data is consumed.
sourcepub async fn read_u8(&mut self) -> Result<u8, MessageReadError>
pub async 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 tokio::io::AsyncReadExt
until it does, an error occurs or no more data can be read (EOF).
If an EOF occurs and the specified amount could not be read, this function will return an [ErrorKind::UnexpectedEof
].
This function consumes the data from the buffer, unless an error occurs, in which case no data is consumed.
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(&mut self) -> &R
pub fn reader_ref(&mut self) -> &R
Returns an immutable reference to the underlying tokio::io::AsyncRead
Reading directly from the underlying stream 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 tokio::io::AsyncRead
Reading directly from the underlying stream will cause data loss