PeekReader

Struct PeekReader 

Source
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 peeking.

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>

Source

pub fn new(reader: R) -> Self

Instantiates a new PeekReader, wrapping the provided Reader and using the default chunk size

Source

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 Reader 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 Reader 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

Source

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 Reader 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 Reader 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

Source

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 Reader 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 Reader 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.

Source

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.

Source

pub fn reader_ref(&self) -> &R

Returns an immutable reference to the underlying Reader

Reading directly from the underlying reader will cause data loss

Source

pub fn reader_mut(&mut self) -> &mut R

Returns a mutable reference to the underlying Reader

Reading directly from the underlying reader will cause data loss

Auto Trait Implementations§

§

impl<R, const BUFFER_SIZE: usize> Freeze for PeekReader<R, BUFFER_SIZE>
where R: Freeze,

§

impl<R, const BUFFER_SIZE: usize> RefUnwindSafe for PeekReader<R, BUFFER_SIZE>
where R: RefUnwindSafe,

§

impl<R, const BUFFER_SIZE: usize> Send for PeekReader<R, BUFFER_SIZE>
where R: Send,

§

impl<R, const BUFFER_SIZE: usize> Sync for PeekReader<R, BUFFER_SIZE>
where R: Sync,

§

impl<R, const BUFFER_SIZE: usize> Unpin for PeekReader<R, BUFFER_SIZE>
where R: Unpin,

§

impl<R, const BUFFER_SIZE: usize> UnwindSafe for PeekReader<R, BUFFER_SIZE>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.