pub struct NibSlice<'a> { /* private fields */ }Expand description
A const slice (&[u4]) over nibbles (4-bit values)
Internally, it operates on an array of bytes, and interprets them as pairs of nibbles. This is
intended to allow use of a NibSlice to examine binary structures that are composed of
nibbles.
For each byte, the nibble composed of the lower bits (mask = 0x0f) is considered to come
before the nibble composed of the higher bits (mask = 0xf0).
Implementations§
Source§impl<'a> NibSlice<'a>
impl<'a> NibSlice<'a>
Sourcepub fn from_bytes_exclude(inner: &'a [u8], exclude: Exclude) -> Self
pub fn from_bytes_exclude(inner: &'a [u8], exclude: Exclude) -> Self
Create a NibSlice from a slice of bytes and whether to exclude either end of the slice
Sourcepub fn from_bytes_skip_last(inner: &'a [u8]) -> Self
pub fn from_bytes_skip_last(inner: &'a [u8]) -> Self
Create a NibSlice from a slice of bytes, excluding the last nibble in the slice
Sourcepub fn from_bytes(inner: &'a [u8]) -> Self
pub fn from_bytes(inner: &'a [u8]) -> Self
Sourcepub fn index<S: SliceIndex<'a>>(&self, idx: S) -> S::Output
pub fn index<S: SliceIndex<'a>>(&self, idx: S) -> S::Output
Index, using various ranges, a NibSlice into NibSlices that are sub-slices
§Examples
use quartet::{NibSlice, Exclude};
let ns = NibSlice::from_bytes_exclude(&[0x12, 0x34, 0x56], Exclude::Both);
let n = ns.index(2..4);
assert_eq!(n, NibSlice::from_bytes(&[0x45]));Sourcepub fn get<S: SliceIndex<'a>>(&self, idx: S) -> Option<S::Output>
pub fn get<S: SliceIndex<'a>>(&self, idx: S) -> Option<S::Output>
Get the NibSlice refered to by the indexing value, or return None if index is out of
range
Sourcepub fn nibble(&self) -> u8
pub fn nibble(&self) -> u8
If the slice refers to a single nibble, return that nibble as a byte. Panic if slice does not have exactly one nibble
§Examples
use quartet::{NibSlice, Exclude};
let orig_s = NibSlice::from_bytes_exclude(&[0x02, 0x34], Exclude::First);
let nib_s = orig_s.index(..1);
assert_eq!(nib_s.len(), 1);
let nib = nib_s.nibble();
assert_eq!(nib, 0x2);Sourcepub fn try_nibble(&self) -> Option<u8>
pub fn try_nibble(&self) -> Option<u8>
If the slice refers to a single nibble, return that nibble as a byte. Return None if the slice does not have exactly one nibble
§Examples
use quartet::{NibSlice, Exclude};
let orig_s = NibSlice::from_bytes_exclude(&[0x02, 0x34], Exclude::First);
let nib_s = orig_s.index(..1);
let nib = nib_s.try_nibble();
assert_eq!(nib, Some(0x2));
// more than 1 nibble
assert_eq!(orig_s.index(1..3).try_nibble(), None);Sourcepub fn iter(&self) -> Iter<'a> ⓘ
pub fn iter(&self) -> Iter<'a> ⓘ
Create an iterator over the NibSlice, where each item is a nibble