octets

Struct 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 AsRef<[u8]> for Octets<'_>

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

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.