pub struct Nibble(/* private fields */);
Expand description
A byte-width nibble, representing a 4-bit unit of data.
While this type does not explicitly guarantee any
particular memory layout, it does guarantee that the
null pointer optimization
applies: Option<Nibble>
will always have the same size
and alignment as Nibble
.
Implementations§
source§impl Nibble
impl Nibble
sourcepub const BITS: u32 = 4u32
pub const BITS: u32 = 4u32
The minimum number of bits required to represent a nibble.
As opposed to the primitive integer types, this value is not
the same as std::mem::sizeof<Nibble>() * 8
; instead it reflects
the smallest possible size that a nibble could be packed into.
sourcepub const unsafe fn new_unchecked(value: u8) -> Self
pub const unsafe fn new_unchecked(value: u8) -> Self
sourcepub const fn new(value: u8) -> Option<Self>
pub const fn new(value: u8) -> Option<Self>
Constructs a new Nibble
representing the given value,
or returns None
if it is greater than 15
.
sourcepub const fn get(&self) -> u8
pub const fn get(&self) -> u8
Consumes self
and returns a u8
representing its value, guaranteed
to be at most 15.
sourcepub const fn pair_from_byte(value: u8) -> (Self, Self)
pub const fn pair_from_byte(value: u8) -> (Self, Self)
Converts a byte (u8
) into a pair of nibbles, where
the upper nibble is on the left and the lower nibble is
on the right.
§Example
use halfling::Nibble;
let byte: u8 = 0x4A;
let (upper, lower) = Nibble::pair_from_byte(byte);
assert_eq!(upper.get(), 0x4);
assert_eq!(lower.get(), 0xA);
sourcepub const fn byte_from_pair(upper: Self, lower: Self) -> u8
pub const fn byte_from_pair(upper: Self, lower: Self) -> u8
Converts a pair of Nibble
values into a byte, where the
upper nibble is the first argument and the lower nibble is
the second.
§Example
use halfling::Nibble;
let upper = Nibble::new(0xB).unwrap();
let lower = Nibble::new(0x2).unwrap();
let byte = Nibble::byte_from_pair(upper, lower);
assert_eq!(byte, 0xB2);
Trait Implementations§
source§impl BitAndAssign for Nibble
impl BitAndAssign for Nibble
source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&=
operation. Read moresource§impl BitOrAssign for Nibble
impl BitOrAssign for Nibble
source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read moresource§impl BitXorAssign for Nibble
impl BitXorAssign for Nibble
source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^=
operation. Read moresource§impl Ord for Nibble
impl Ord for Nibble
source§impl PartialEq for Nibble
impl PartialEq for Nibble
source§impl PartialOrd for Nibble
impl PartialOrd for Nibble
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 moresource§impl ShlAssign<u8> for Nibble
impl ShlAssign<u8> for Nibble
source§fn shl_assign(&mut self, rhs: u8)
fn shl_assign(&mut self, rhs: u8)
<<=
operation. Read moresource§impl ShrAssign<u8> for Nibble
impl ShrAssign<u8> for Nibble
source§fn shr_assign(&mut self, rhs: u8)
fn shr_assign(&mut self, rhs: u8)
>>=
operation. Read more