pub struct Octets<'a> { /* private fields */ }
Expand description
A zero-copy immutable byte buffer.
Octets
wraps an in-memory buffer of bytes and provides utility functions
for manipulating it. The underlying buffer is provided by the user and is
not copied when creating an Octets
. Operations are panic-free and will
avoid indexing the buffer past its end.
Additionally, an offset (initially set to the start of the buffer) is incremented as bytes are read from / written to the buffer, to allow for sequential operations.
Implementations§
Source§impl<'a> Octets<'a>
impl<'a> Octets<'a>
Sourcepub fn with_slice(buf: &'a [u8]) -> Self
pub fn with_slice(buf: &'a [u8]) -> Self
Creates an Octets
from the given slice, without copying.
Since the Octets
is immutable, the input slice needs to be
immutable.
Sourcepub fn get_u8(&mut self) -> Result<u8>
pub fn get_u8(&mut self) -> Result<u8>
Reads an unsigned 8-bit integer from the current offset and advances the buffer.
Sourcepub fn peek_u8(&mut self) -> Result<u8>
pub fn peek_u8(&mut self) -> Result<u8>
Reads an unsigned 8-bit integer from the current offset without advancing the buffer.
Sourcepub fn get_u16(&mut self) -> Result<u16>
pub fn get_u16(&mut self) -> Result<u16>
Reads an unsigned 16-bit integer in network byte-order from the current offset and advances the buffer.
Sourcepub fn get_u24(&mut self) -> Result<u32>
pub fn get_u24(&mut self) -> Result<u32>
Reads an unsigned 24-bit integer in network byte-order from the current offset and advances the buffer.
Sourcepub fn get_u32(&mut self) -> Result<u32>
pub fn get_u32(&mut self) -> Result<u32>
Reads an unsigned 32-bit integer in network byte-order from the current offset and advances the buffer.
Sourcepub fn get_u64(&mut self) -> Result<u64>
pub fn get_u64(&mut self) -> Result<u64>
Reads an unsigned 64-bit integer in network byte-order from the current offset and advances the buffer.
Sourcepub fn get_varint(&mut self) -> Result<u64>
pub fn get_varint(&mut self) -> Result<u64>
Reads an unsigned variable-length integer in network byte-order from the current offset and advances the buffer.
Sourcepub fn get_bytes(&mut self, len: usize) -> Result<Octets<'a>>
pub fn get_bytes(&mut self, len: usize) -> Result<Octets<'a>>
Reads len
bytes from the current offset without copying and advances
the buffer.
Sourcepub fn get_bytes_with_u8_length(&mut self) -> Result<Octets<'a>>
pub fn get_bytes_with_u8_length(&mut self) -> Result<Octets<'a>>
Reads len
bytes from the current offset without copying and advances
the buffer, where len
is an unsigned 8-bit integer prefix.
Sourcepub fn get_bytes_with_u16_length(&mut self) -> Result<Octets<'a>>
pub fn get_bytes_with_u16_length(&mut self) -> Result<Octets<'a>>
Reads len
bytes from the current offset without copying and advances
the buffer, where len
is an unsigned 16-bit integer prefix in network
byte-order.
Sourcepub fn get_bytes_with_varint_length(&mut self) -> Result<Octets<'a>>
pub fn get_bytes_with_varint_length(&mut self) -> Result<Octets<'a>>
Reads len
bytes from the current offset without copying and advances
the buffer, where len
is an unsigned variable-length integer prefix
in network byte-order.
Sourcepub fn peek_bytes(&self, len: usize) -> Result<Octets<'a>>
pub fn peek_bytes(&self, len: usize) -> Result<Octets<'a>>
Reads len
bytes from the current offset without copying and without
advancing the buffer.
Sourcepub fn slice(&self, len: usize) -> Result<&'a [u8]>
pub fn slice(&self, len: usize) -> Result<&'a [u8]>
Returns a slice of len
elements from the current offset.
Sourcepub fn slice_last(&self, len: usize) -> Result<&'a [u8]>
pub fn slice_last(&self, len: usize) -> Result<&'a [u8]>
Returns a slice of len
elements from the end of the buffer.