Struct Unpacker

Source
pub struct Unpacker<'a>(/* private fields */);
Expand description

Unpacker of POD values from a byte slice.

Any reads past the end of the slice return default values rather than panicking. The caller must check the error status at the end to determine whether all returned values were valid.

Little-endian encoding is assumed.

Implementations§

Source§

impl<'a> Unpacker<'a>

Source

pub const fn new(b: &'a [u8]) -> Self

Creates a new unpacker.

Source

pub const fn invalid() -> Self

Creates an unpacker in an error state.

Source

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

Returns the remaining byte slice, if any. The caller should use Self::is_ok() to check whether the returned slice is valid.

Source

pub const fn len(&self) -> usize

Returns the remaining number of bytes.

Source

pub const fn is_empty(&self) -> bool

Returns whether the byte slice is empty.

Source

pub fn is_ok(&self) -> bool

Returns whether all reads were within the bounds of the original byte slice.

Source

pub fn take(&mut self) -> Self

Returns the remaining byte slice in a new unpacker, leaving self empty. This is primarily useful in combination with map().

Source

pub fn map<T>(self, f: impl FnOnce(&mut Self) -> T) -> Option<T>

Returns Some output of f, or None if f fails to consume the entire slice without reading past the end.

Source

pub fn map_or<T>(self, default: T, f: impl FnOnce(&mut Self) -> T) -> T

Returns the output of f, or default if f fails to consume the entire slice without reading past the end.

Source

pub fn map_or_else<T>( self, default: impl FnOnce() -> T, f: impl FnOnce(&mut Self) -> T, ) -> T

Returns the output of f, or the output of default() if f fails to consume the entire slice without reading past the end.

Source

pub const fn split_at(&self, i: usize) -> (Self, Self)

Splits the remaining byte slice at i, and returns two new unpackers, both of which will be in an error state if len() < i.

Source

pub fn skip(&mut self, n: usize) -> Option<Self>

Advances self by n bytes, returning a new unpacker for the skipped bytes or None if there is an insufficient number of bytes remaining, in which case any remaining bytes are discarded.

§Examples
if let Some(mut hdr) = p.skip(2) {
    let _ = hdr.u16();
    assert!(hdr.is_ok() && p.is_ok());
} else {
    assert!(!p.is_ok());
}
Source

pub fn bool(&mut self) -> bool

Returns the next u8 as a bool where any non-zero value is converted to true.

Source

pub fn u8(&mut self) -> u8

Returns the next u8.

Source

pub fn u16(&mut self) -> u16

Returns the next u16.

Source

pub fn u32(&mut self) -> u32

Returns the next u32.

Source

pub fn u64(&mut self) -> u64

Returns the next u64.

Source

pub fn u128(&mut self) -> u128

Returns the next u128.

Source

pub fn i8(&mut self) -> i8

Returns the next i8.

Source

pub fn i16(&mut self) -> i16

Returns the next i16.

Source

pub fn i32(&mut self) -> i32

Returns the next i32.

Source

pub fn i64(&mut self) -> i64

Returns the next i64.

Source

pub fn i128(&mut self) -> i128

Returns the next i128.

Source

pub fn bytes<const N: usize>(&mut self) -> [u8; N]

Returns the next [u8; N] array.

Source

pub unsafe fn read<T: Default>(&mut self) -> T

Returns the next T, or T::default() if there is an insufficient number of bytes remaining, in which case any remaining bytes are discarded.

§Safety

T must be able to hold the resulting bit pattern.

Trait Implementations§

Source§

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

Source§

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

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

impl<'a> Clone for Unpacker<'a>

Source§

fn clone(&self) -> Unpacker<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Unpacker<'a>

Source§

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

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

impl<'a> Default for Unpacker<'a>

Source§

fn default() -> Unpacker<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> Hash for Unpacker<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Ord for Unpacker<'a>

Source§

fn cmp(&self, other: &Unpacker<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for Unpacker<'a>

Source§

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

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

const 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> PartialOrd for Unpacker<'a>

Source§

fn partial_cmp(&self, other: &Unpacker<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Copy for Unpacker<'a>

Source§

impl<'a> Eq for Unpacker<'a>

Source§

impl<'a> StructuralPartialEq for Unpacker<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Unpacker<'a>

§

impl<'a> RefUnwindSafe for Unpacker<'a>

§

impl<'a> Send for Unpacker<'a>

§

impl<'a> Sync for Unpacker<'a>

§

impl<'a> Unpin for Unpacker<'a>

§

impl<'a> UnwindSafe for Unpacker<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> Unpack for T
where T: AsRef<[u8]>,

Source§

fn unpack(&self) -> Unpacker<'_>

Returns an unpacker for reading values from the start of the buffer.