Struct halfling::Nibble

source ·
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

source

pub const MIN: Self = _

0 as a Nibble.

source

pub const ZERO: Self = _

0 as a Nibble.

source

pub const ONE: Self = _

1 as a Nibble.

source

pub const TWO: Self = _

2 as a Nibble.

source

pub const THREE: Self = _

3 as a Nibble.

source

pub const FOUR: Self = _

4 as a Nibble.

source

pub const FIVE: Self = _

5 as a Nibble.

source

pub const SIX: Self = _

6 as a Nibble.

source

pub const SEVEN: Self = _

7 as a Nibble.

source

pub const EIGHT: Self = _

8 as a Nibble.

source

pub const NINE: Self = _

9 as a Nibble.

source

pub const TEN: Self = _

10 as a Nibble.

source

pub const ELEVEN: Self = _

11 as a Nibble.

source

pub const TWELVE: Self = _

12 as a Nibble.

source

pub const THIRTEEN: Self = _

13 as a Nibble.

source

pub const FOURTEEN: Self = _

14 as a Nibble.

source

pub const FIFTEEN: Self = _

15 as a Nibble.

source

pub const MAX: Self = _

15 as a Nibble.

source§

impl Nibble

source

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.

source

pub const unsafe fn new_unchecked(value: u8) -> Self

Constructs a new Nibble representing the given value without checking invariants.

§Safety

value must be strictly less than 16.

source

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.

source

pub const fn get(&self) -> u8

Consumes self and returns a u8 representing its value, guaranteed to be at most 15.

source

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

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 Binary for Nibble

source§

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

Formats the value using the given formatter.
source§

impl BitAnd for Nibble

§

type Output = Nibble

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
source§

impl BitAndAssign for Nibble

source§

fn bitand_assign(&mut self, rhs: Self)

Performs the &= operation. Read more
source§

impl BitOr for Nibble

§

type Output = Nibble

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
source§

impl BitOrAssign for Nibble

source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
source§

impl BitXor for Nibble

§

type Output = Nibble

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
source§

impl BitXorAssign for Nibble

source§

fn bitxor_assign(&mut self, rhs: Self)

Performs the ^= operation. Read more
source§

impl Clone for Nibble

source§

fn clone(&self) -> Nibble

Returns a copy 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 Debug for Nibble

source§

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

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

impl Display for Nibble

source§

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

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

impl From<Nibble> for char

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for i128

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for i16

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for i32

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for i64

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for isize

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for u128

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for u16

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for u32

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for u64

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for u8

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl From<Nibble> for usize

source§

fn from(value: Nibble) -> Self

Converts to this type from the input type.
source§

impl LowerHex for Nibble

source§

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

Formats the value using the given formatter.
source§

impl Not for Nibble

§

type Output = Nibble

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl Octal for Nibble

source§

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

Formats the value using the given formatter.
source§

impl Ord for Nibble

source§

fn cmp(&self, other: &Nibble) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Nibble

source§

fn eq(&self, other: &Nibble) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Nibble

source§

fn partial_cmp(&self, other: &Nibble) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Shl<u8> for Nibble

§

type Output = Nibble

The resulting type after applying the << operator.
source§

fn shl(self, rhs: u8) -> Self::Output

Performs the << operation. Read more
source§

impl ShlAssign<u8> for Nibble

source§

fn shl_assign(&mut self, rhs: u8)

Performs the <<= operation. Read more
source§

impl Shr<u8> for Nibble

§

type Output = Nibble

The resulting type after applying the >> operator.
source§

fn shr(self, rhs: u8) -> Self::Output

Performs the >> operation. Read more
source§

impl ShrAssign<u8> for Nibble

source§

fn shr_assign(&mut self, rhs: u8)

Performs the >>= operation. Read more
source§

impl TryFrom<Nibble> for NonZeroU8

§

type Error = <NonZero<u8> as TryFrom<u8>>::Error

The type returned in the event of a conversion error.
source§

fn try_from(value: Nibble) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<Nibble> for i8

§

type Error = <i8 as TryFrom<u8>>::Error

The type returned in the event of a conversion error.
source§

fn try_from(value: Nibble) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NonZero<u8>> for Nibble

§

type Error = NibbleTryFromIntError<NonZero<u8>>

The type returned in the event of a conversion error.
source§

fn try_from(value: NonZeroU8) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<bool> for Nibble

§

type Error = NibbleTryFromIntError<bool>

The type returned in the event of a conversion error.
source§

fn try_from(value: bool) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<char> for Nibble

§

type Error = NibbleTryFromIntError<char>

The type returned in the event of a conversion error.
source§

fn try_from(value: char) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<i128> for Nibble

§

type Error = NibbleTryFromIntError<i128>

The type returned in the event of a conversion error.
source§

fn try_from(value: i128) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<i16> for Nibble

§

type Error = NibbleTryFromIntError<i16>

The type returned in the event of a conversion error.
source§

fn try_from(value: i16) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<i32> for Nibble

§

type Error = NibbleTryFromIntError<i32>

The type returned in the event of a conversion error.
source§

fn try_from(value: i32) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<i64> for Nibble

§

type Error = NibbleTryFromIntError<i64>

The type returned in the event of a conversion error.
source§

fn try_from(value: i64) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<i8> for Nibble

§

type Error = NibbleTryFromIntError<i8>

The type returned in the event of a conversion error.
source§

fn try_from(value: i8) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<isize> for Nibble

§

type Error = NibbleTryFromIntError<isize>

The type returned in the event of a conversion error.
source§

fn try_from(value: isize) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u128> for Nibble

§

type Error = NibbleTryFromIntError<u128>

The type returned in the event of a conversion error.
source§

fn try_from(value: u128) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u16> for Nibble

§

type Error = NibbleTryFromIntError<u16>

The type returned in the event of a conversion error.
source§

fn try_from(value: u16) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u32> for Nibble

§

type Error = NibbleTryFromIntError<u32>

The type returned in the event of a conversion error.
source§

fn try_from(value: u32) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u64> for Nibble

§

type Error = NibbleTryFromIntError<u64>

The type returned in the event of a conversion error.
source§

fn try_from(value: u64) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u8> for Nibble

§

type Error = NibbleTryFromIntError<u8>

The type returned in the event of a conversion error.
source§

fn try_from(value: u8) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<usize> for Nibble

§

type Error = NibbleTryFromIntError<usize>

The type returned in the event of a conversion error.
source§

fn try_from(value: usize) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl UpperHex for Nibble

source§

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

Formats the value using the given formatter.
source§

impl Copy for Nibble

source§

impl Eq for Nibble

source§

impl StructuralPartialEq for Nibble

Auto Trait Implementations§

§

impl Freeze for Nibble

§

impl RefUnwindSafe for Nibble

§

impl Send for Nibble

§

impl Sync for Nibble

§

impl Unpin for Nibble

§

impl UnwindSafe for Nibble

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

§

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§

default 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>,

§

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

§

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.