BytesLike

Trait BytesLike 

Source
pub trait BytesLike<'bytes>: Sized + Debug {
    type Error: Sized + Copy + Debug;
    type ExternallyTrackedLength: Sized + Copy + Debug;

    // Required methods
    fn len(&self, len: Self::ExternallyTrackedLength) -> usize;
    fn peek(&self, i: usize) -> Result<u8, Self::Error>;
    fn read_bytes(
        &mut self,
        bytes: usize,
    ) -> Result<(Self::ExternallyTrackedLength, Self), Self::Error>;
    fn read_into_slice(&mut self, slice: &mut [u8]) -> Result<(), Self::Error>;

    // Provided methods
    fn read_byte(&mut self) -> Result<u8, Self::Error> { ... }
    fn advance(&mut self, bytes: usize) -> Result<(), Self::Error> { ... }
}
Expand description

An item which is like a &[u8].

This should be a reference to a buffer for which references are cheap to work with.

Required Associated Types§

Source

type Error: Sized + Copy + Debug

The type for errors when interacting with these bytes.

Source

type ExternallyTrackedLength: Sized + Copy + Debug

The type representing the length of a read BytesLike, if a BytesLike does not inherently know its length.

This should be usize or ().

Required Methods§

Source

fn len(&self, len: Self::ExternallyTrackedLength) -> usize

The length of these bytes.

Source

fn peek(&self, i: usize) -> Result<u8, Self::Error>

Peak at a byte.

Source

fn read_bytes( &mut self, bytes: usize, ) -> Result<(Self::ExternallyTrackedLength, Self), Self::Error>

Read a fixed amount of bytes from the container.

This MUST return Ok((len, slice)) where slice is the expected length or Err(_).

Source

fn read_into_slice(&mut self, slice: &mut [u8]) -> Result<(), Self::Error>

Read a fixed amount of bytes from the container into a slice.

Provided Methods§

Source

fn read_byte(&mut self) -> Result<u8, Self::Error>

Read a byte from the container.

Source

fn advance(&mut self, bytes: usize) -> Result<(), Self::Error>

Advance the container by a certain amount of bytes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'bytes> BytesLike<'bytes> for &'bytes [u8]

Source§

type Error = SliceError

Source§

type ExternallyTrackedLength = ()

Source§

fn len(&self, (): ()) -> usize

Source§

fn peek(&self, i: usize) -> Result<u8, Self::Error>

Source§

fn read_bytes( &mut self, bytes: usize, ) -> Result<(Self::ExternallyTrackedLength, Self), Self::Error>

Source§

fn read_into_slice(&mut self, slice: &mut [u8]) -> Result<(), Self::Error>

Implementors§