pub struct Reader<'a> {
buffer: &'a [u8],
cursor: usize,
}
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 the buffer can be created
using the sub
function or a section of a certain length can
be obtained using the take
function
Fields§
§buffer: &'a [u8]
The underlying buffer storing the readers content
cursor: usize
Stores the current reading position for the buffer
Implementations§
source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
sourcepub fn init(bytes: &[u8]) -> Reader<'_>
pub fn init(bytes: &[u8]) -> Reader<'_>
Creates a new Reader of the provided bytes
slice with
the initial cursor position of zero.
sourcepub fn sub(&mut self, length: usize) -> Result<Reader<'_>, InvalidMessage>
pub fn sub(&mut self, length: usize) -> Result<Reader<'_>, InvalidMessage>
Attempts to create a new Reader on a sub section of this
readers bytes by taking a slice of the provided length
will return None if there is not enough bytes
sourcepub fn rest(&mut self) -> &[u8] ⓘ
pub fn rest(&mut self) -> &[u8] ⓘ
Borrows a slice of all the remaining bytes that appear after the cursor position.
Moves the cursor to the end of the buffer length.
sourcepub fn take(&mut self, length: usize) -> Option<&[u8]>
pub fn take(&mut self, length: usize) -> Option<&[u8]>
Attempts to borrow a slice of bytes from the current
cursor position of length
if there is not enough
bytes remaining after the cursor to take the length
then None is returned instead.
sourcepub fn any_left(&self) -> bool
pub fn any_left(&self) -> bool
Used to check whether the reader has any content left after the cursor (cursor has not reached end of buffer)