#[repr(transparent)]pub struct Unpacker<'a>(_);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>
impl<'a> Unpacker<'a>
sourcepub fn into_inner(self) -> Option<&'a [u8]>
pub fn into_inner(self) -> Option<&'a [u8]>
Returns the remaining byte slice or None if any reads went past the
end of the original slice.
sourcepub fn is_ok(&self) -> bool
pub fn is_ok(&self) -> bool
Returns whether all reads were within the bounds of the original byte slice.
sourcepub fn take(&mut self) -> Self
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().
sourcepub fn map<T>(self, f: impl FnOnce(&mut Self) -> T) -> Option<T>
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.
sourcepub fn map_or<T>(self, default: T, f: impl FnOnce(&mut Self) -> T) -> T
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.
sourcepub fn map_or_else<T>(
self,
default: impl FnOnce() -> T,
f: impl FnOnce(&mut Self) -> T
) -> T
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.
sourcepub const fn split_at(&self, i: usize) -> (Self, Self)
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.
sourcepub fn skip(&mut self, n: usize) -> Option<Self>
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());
}Trait Implementations§
source§impl<'a> Ord for Unpacker<'a>
impl<'a> Ord for Unpacker<'a>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a> PartialEq<Unpacker<'a>> for Unpacker<'a>
impl<'a> PartialEq<Unpacker<'a>> for Unpacker<'a>
source§impl<'a> PartialOrd<Unpacker<'a>> for Unpacker<'a>
impl<'a> PartialOrd<Unpacker<'a>> for Unpacker<'a>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more