Reader

Struct Reader 

Source
pub struct Reader<'a> { /* private fields */ }
Expand description

Wrapper over a slice of bytes that allows reading chunks from with the current position state held using a cursor.

A new reader for a sub section of the buffer can be created using the sub function or a section of a certain length can be obtained using the read function

Implementations§

Source§

impl<'a> Reader<'a>

Source

pub const fn init(bytes: &'a [u8]) -> Self

Creates a new Reader of the provided bytes slice with the initial cursor position of zero.

Note that the provided slice must be network byte order (big-endian).

Source

pub const fn used(&self) -> usize

Returns the cursor position, i.e., the number of bytes that have been read from the buffer.

Source

pub const fn left(&self) -> usize

Returns the unread bytes length.

Source

pub const fn unread(&self) -> &'a [u8]

Returns all unread bytes of the reader without advancing the cursor.

Source

pub const fn read(&mut self, length: usize) -> Result<&'a [u8], Error>

Reads a slice of length bytes from the reader and returns it.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78]);
// Read 2 bytes
let bytes = reader.read(2).unwrap();
assert_eq!(bytes, &[0x12, 0x34]);
// Read another 2 bytes
let bytes = reader.read(2).unwrap();
assert_eq!(bytes, &[0x56, 0x78]);
// Try to read more bytes than available.
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78]);
assert!(matches!(
    reader.read(5),
    Err(slicur::Error::InsufficientData { missing }) if missing.get() == 1
));
§Errors

Error.

Source

pub const fn read_array<const N: usize>(&mut self) -> Result<&'a [u8; N], Error>

Reads an array of N bytes from the reader and returns it.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78]);
// Read 2 bytes
let bytes = reader.read_array::<2>().unwrap();
assert_eq!(bytes, &[0x12, 0x34]);
// Try to read more bytes than available.
assert!(matches!(
    reader.read_array::<3>(),
    Err(slicur::Error::InsufficientData { missing }) if missing.get() == 1
));
§Errors

Error.

Source

pub const fn read_all(&mut self) -> &'a [u8]

Reads all unread bytes from the reader.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78]);
// Read 2 bytes
let bytes = reader.read(2).unwrap();
assert_eq!(bytes, &[0x12, 0x34]);
// Read all remaining bytes
let bytes = reader.read_all();
assert_eq!(bytes, &[0x56, 0x78]);
Source

pub fn read_u8(&mut self) -> Result<u8, Error>

Reads a single byte from the reader and returns it.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56]);
assert_eq!(reader.read_u8().unwrap(), 0x12);
assert_eq!(reader.read_u8().unwrap(), 0x34);
assert_eq!(reader.read_u8().unwrap(), 0x56);
§Errors

Error.

Source

pub fn read_u16(&mut self) -> Result<u16, Error>

Reads a u16 from the reader and returns it.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78, 0x89]);
assert_eq!(reader.read_u16().unwrap(), 0x1234);
assert_eq!(reader.read_u16().unwrap(), 0x5678);
§Errors

Error.

Source

pub fn read_u24(&mut self) -> Result<u32, Error>

Reads a u24 from the reader and returns it.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xCD]);
assert_eq!(reader.read_u24().unwrap(), 0x123456);
assert_eq!(reader.read_u24().unwrap(), 0x789ABC);
§Errors

Error.

Source

pub fn read_u32(&mut self) -> Result<u32, Error>

Reads a u32 from the reader and returns it.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x11]);
assert_eq!(reader.read_u32().unwrap(), 0x12345678);
assert_eq!(reader.read_u32().unwrap(), 0x9ABCDEF0);
§Errors

Error.

Source

pub fn read_u64(&mut self) -> Result<u64, Error>

Reads a u64 from the reader and returns it.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x11]);
assert_eq!(reader.read_u64().unwrap(), 0x123456789ABCDEF0);
§Errors

Error.

Source

pub fn read_u128(&mut self) -> Result<u128, Error>

Reads a u128 from the reader and returns it.

§Examples
let mut reader = Reader::init(&[
    0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    0x88, 0x99,
]);
assert_eq!(
    reader.read_u128().unwrap(),
    0x123456789ABCDEF01122334455667788
);
§Errors

Error.

Source

pub const fn sub(&mut self, length: usize) -> Result<Self, Error>

Reads a slice of length bytes from the reader and creates a new Reader over it, i.e., self.read(length).map(Reader::init).

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78, 0x9A]);
let mut sub_reader = reader.sub(3).unwrap();
assert_eq!(sub_reader.read_u8().unwrap(), 0x12);
assert_eq!(sub_reader.read_u16().unwrap(), 0x3456);
§Errors

Error.

Source

pub const fn advance(&mut self, length: usize) -> Result<(), Error>

Advances the cursor by the specified length without returning any data.

§Examples
let mut reader = Reader::init(&[0x12, 0x34, 0x56, 0x78]);
assert!(reader.advance(2).is_ok());
assert!(reader.advance(3).is_err());
§Errors

Error.

Trait Implementations§

Source§

impl Debug for Reader<'_>

Available on non-crate feature feat-debug-buffer only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

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