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>
impl<'a> Reader<'a>
Sourcepub const fn init(bytes: &'a [u8]) -> Self
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).
Sourcepub const fn used(&self) -> usize
pub const fn used(&self) -> usize
Returns the cursor position, i.e., the number of bytes that have been read from the buffer.
Sourcepub const fn unread(&self) -> &'a [u8]
pub const fn unread(&self) -> &'a [u8]
Returns all unread bytes of the reader without advancing the cursor.
Sourcepub const fn read(&mut self, length: usize) -> Result<&'a [u8], Error>
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
Sourcepub const fn read_array<const N: usize>(&mut self) -> Result<&'a [u8; N], Error>
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
Sourcepub const fn read_all(&mut self) -> &'a [u8]
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]);Sourcepub const fn sub(&mut self, length: usize) -> Result<Self, Error>
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);