Struct octets::Octets

source ·
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>

source

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.

source

pub fn get_u8(&mut self) -> Result<u8>

Reads an unsigned 8-bit integer from the current offset and advances the buffer.

source

pub fn peek_u8(&mut self) -> Result<u8>

Reads an unsigned 8-bit integer from the current offset without advancing the buffer.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn get_bytes(&mut self, len: usize) -> Result<Octets<'a>>

Reads len bytes from the current offset without copying and advances the buffer.

source

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.

source

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.

source

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.

source

pub fn peek_bytes(&self, len: usize) -> Result<Octets<'a>>

Reads len bytes from the current offset without copying and without advancing the buffer.

source

pub fn slice(&self, len: usize) -> Result<&'a [u8]>

Returns a slice of len elements from the current offset.

source

pub fn slice_last(&self, len: usize) -> Result<&'a [u8]>

Returns a slice of len elements from the end of the buffer.

source

pub fn skip(&mut self, skip: usize) -> Result<()>

Advances the buffer’s offset.

source

pub fn cap(&self) -> usize

Returns the remaining capacity in the buffer.

source

pub fn len(&self) -> usize

Returns the total length of the buffer.

source

pub fn is_empty(&self) -> bool

Returns true if the buffer is empty.

source

pub fn off(&self) -> usize

Returns the current offset of the buffer.

source

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

Returns a reference to the internal buffer.

source

pub fn to_vec(&self) -> Vec<u8>

Copies the buffer from the current offset into a new Vec<u8>.

Trait Implementations§

source§

impl<'a> AsRef<[u8]> for Octets<'a>

source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<'a> Debug for Octets<'a>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a> PartialEq for Octets<'a>

source§

fn eq(&self, other: &Octets<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for Octets<'a>

source§

impl<'a> StructuralPartialEq for Octets<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Octets<'a>

§

impl<'a> RefUnwindSafe for Octets<'a>

§

impl<'a> Send for Octets<'a>

§

impl<'a> Sync for Octets<'a>

§

impl<'a> Unpin for Octets<'a>

§

impl<'a> UnwindSafe for Octets<'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>,

§

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

§

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.