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>
impl<'a> Unpacker<'a>
Sourcepub const fn into_inner(self) -> &'a [u8]
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.
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());
}