Skip to main content

BytesReadRef

Trait BytesReadRef 

Source
pub trait BytesReadRef<'a>: BytesRead {
    // Required methods
    fn as_slice_ref(&self) -> &'a [u8] ;
    fn remaining_ref(&self) -> &'a [u8] ;
    fn try_read_ref(&mut self, len: usize) -> Result<&'a [u8], ReadError>;
    fn peek_ref(&self, len: usize) -> Option<&'a [u8]>;

    // Provided method
    fn read_ref(&mut self, len: usize) -> &'a [u8]  { ... }
}
Expand description

Read bytes while keeping the original reference.

use simple_bytes::{Bytes, BytesRead, BytesReadRef};

let mut bytes = Bytes::from("hey".as_ref());
let h = bytes.read_u8();
let ey: &'static [u8] = bytes.remaining_ref();

Required Methods§

Source

fn as_slice_ref(&self) -> &'a [u8]

Returns the entire slice.

Source

fn remaining_ref(&self) -> &'a [u8]

Returns all remaining bytes.

Source

fn try_read_ref(&mut self, len: usize) -> Result<&'a [u8], ReadError>

Try to read a given length of bytes.

§Failes

If len exceeds self.remaining().len().

Source

fn peek_ref(&self, len: usize) -> Option<&'a [u8]>

Tries to read a given length without updating the internal position. Returns None if there are not enought bytes remaining.

Provided Methods§

Source

fn read_ref(&mut self, len: usize) -> &'a [u8]

Reads a given length of bytes.

§Panics

If len exceeds self.remaining().len().

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'a, R: BytesReadRef<'a>> BytesReadRef<'a> for &mut R

Source§

fn as_slice_ref(&self) -> &'a [u8]

Source§

fn remaining_ref(&self) -> &'a [u8]

Source§

fn try_read_ref(&mut self, len: usize) -> Result<&'a [u8], ReadError>

Source§

fn peek_ref(&self, len: usize) -> Option<&'a [u8]>

Implementors§

Source§

impl<'a> BytesReadRef<'a> for Bytes<'a>