NibSlice

Struct NibSlice 

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

Source

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

Source

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

Source

pub fn from_bytes(inner: &'a [u8]) -> Self

Create a NibSlice from a slice of bytes, including all nibbles in the given bytes

The resulting NibSlice will have .len() equal to 2 * inner.len().

Source

pub fn len(&self) -> usize

The number of nibbles in the NibSlice

Source

pub fn split_at(&self, offset: usize) -> (NibSlice<'a>, NibSlice<'a>)

Split the NibSlice into 2 NibSlices at the nibble offset given

The nibble at offset is included in the second slice returned

Source

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]));
Source

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

Source

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);
Source

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);
Source

pub fn iter(&self) -> Iter<'a>

Create an iterator over the NibSlice, where each item is a nibble

Source

pub fn byte_parts(&self) -> (Option<u8>, &[u8], Option<u8>)

Decompose the NibSlice into byte-oriented parts

The first and last members of the tuple are the non-byte aligned nibbles optionally at the start and end of the NibSlice. The middle member is the byte-aligned nibbles organized into bytes

Trait Implementations§

Source§

impl<'a> Clone for NibSlice<'a>

Source§

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

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

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for NibSlice<'a>

Source§

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

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

impl<'a> Display for NibSlice<'a>

Source§

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

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

impl PartialEq<NibSlice<'_>> for NibSlice<'_>

Source§

fn eq(&self, other: &NibSlice<'_>) -> 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> Copy for NibSlice<'a>

Source§

impl Eq for NibSlice<'_>

Auto Trait Implementations§

§

impl<'a> Freeze for NibSlice<'a>

§

impl<'a> RefUnwindSafe for NibSlice<'a>

§

impl<'a> Send for NibSlice<'a>

§

impl<'a> Sync for NibSlice<'a>

§

impl<'a> Unpin for NibSlice<'a>

§

impl<'a> UnwindSafe for NibSlice<'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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.