1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
//! Basic utilities and structures for handling nibbles.
//!
//! # Nibbles
//! A [nibble](https://en.wikipedia.org/wiki/Nibble) (sometimes also *nybble* or *nybl*)
//! is a 4-bit unit of data, equivalent in size to a single-digit hexadecimal number.
//! Historically, nibbles were used in early computers to represent small enumerations, e.g.
//! the individual digits of a base-10 number, but today they are largely API details (as
//! opposed to genuinely necessary memory-saving constructs).
//!
//! `halfling`'s [`Nibble`] is a byte-width struct containing a single nibble, which enables
//! the [niche value optimization](https://www.noahlev.org/papers/popl22src-filling-a-niche.pdf)
//! (a [`Nibble`] has 4 unused bits, and hence 240 such niches are available).
//! They are byte-width due to [Rust's fundamental expectation that all types are at least
//! byte-aligned](https://doc.rust-lang.org/reference/type-layout.html), which prevents us
//! from constructing a single type that genuinely consumes only a nibble of memory.
#[warn(missing_docs)]
#[warn(rustdoc::all)]
#[warn(clippy::all)]
use thiserror::Error;
mod internal;
/// The error produced if a conversion from an integral type to a [`Nibble`] fails.
#[derive(Debug, Error)]
#[error("failed to convert {0:?} into a nibble.")]
pub struct NibbleTryFromIntError<T>(T);
/// 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](std::option#representation)
/// applies: [`Option<Nibble>`](std::option) will always have the same size
/// and alignment as `Nibble`.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Nibble(internal::UnsignedNibbleValue);
// CONVERSION TRAITS
/// Generates `TryFrom` impls for `Nibble`.
macro_rules! nibble_try_from_impls {
($($int:ty),+) => {
$(
impl std::convert::TryFrom<$int> for crate::Nibble {
type Error = crate::NibbleTryFromIntError<$int>;
fn try_from(value: $int) -> Result<Self, Self::Error> {
let byte: u8 = value.try_into().map_err(|_| crate::NibbleTryFromIntError(value))?;
match Self::can_represent(byte) {
true => Ok(unsafe { Self::new_unchecked(byte) }),
false => Err(crate::NibbleTryFromIntError(value)),
}
}
}
)+
};
}
/// Generates `From<Nibble>` impls for the given types.
macro_rules! nibble_into_impls {
($($target:ty),+) => {
$(
impl std::convert::From<crate::Nibble> for $target {
fn from(value: crate::Nibble) -> Self {
value.get().into()
}
}
)+
};
}
/// Generates `TryFrom<Nibble>` impls for the given types.
macro_rules! nibble_try_into_impls {
($($target:ty),+) => {
$(
impl std::convert::TryFrom<crate::Nibble> for $target {
type Error = <$target as std::convert::TryFrom<u8>>::Error;
fn try_from(value: crate::Nibble) -> Result<Self, Self::Error> {
value.get().try_into()
}
}
)+
};
}
nibble_try_from_impls!(
u8, i8, std::num::NonZeroU8,
u16, i16,
u32, i32,
u64, i64,
u128, i128,
usize, isize,
char, bool
);
nibble_into_impls!(
u8,
u16, i16,
u32, i32,
u64, i64,
u128, i128,
usize, isize,
char
);
nibble_try_into_impls!(
i8, std::num::NonZeroU8
);
// DISPLAY TRAITS
/// Generates impls for the given formatting traits.
macro_rules! nibble_fmt_impls {
($($name:path),+) => {
$(
impl $name for crate::Nibble {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<u8 as $name>::fmt(&self.get(), f)
}
}
)+
};
}
nibble_fmt_impls!(
std::fmt::Binary,
std::fmt::Octal,
std::fmt::LowerHex,
std::fmt::UpperHex,
std::fmt::Display,
std::fmt::Debug
);
// CONSTANTS
macro_rules! nibble_constants {
($($name:ident := $value:literal),+) => {
impl crate::Nibble {
$(
#[doc = concat!(stringify!($value), " as a [`Nibble`].")]
pub const $name: Self = unsafe { Self::new_unchecked($value) };
)+
}
};
}
nibble_constants!(
MIN := 0,
ZERO := 0,
ONE := 1,
TWO := 2,
THREE := 3,
FOUR := 4,
FIVE := 5,
SIX := 6,
SEVEN := 7,
EIGHT := 8,
NINE := 9,
TEN := 10,
ELEVEN := 11,
TWELVE := 12,
THIRTEEN := 13,
FOURTEEN := 14,
FIFTEEN := 15,
MAX := 15
);
// OPERATOR TRAITS
impl std::ops::BitAnd for Nibble {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
let left: u8 = self.into();
let right: u8 = rhs.into();
// upper bits are all zero,
// so no need to mask them off
let result: u8 = left & right;
unsafe { Nibble::new_unchecked(result) }
}
}
impl std::ops::BitAndAssign for Nibble {
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs;
}
}
impl std::ops::BitOr for Nibble {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
let left: u8 = self.into();
let right: u8 = rhs.into();
// the upper 4 bits are all zero,
// so no need to mask them off.
let result = left | right;
unsafe { Nibble::new_unchecked(result) }
}
}
impl std::ops::BitOrAssign for Nibble {
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}
impl std::ops::BitXor for Nibble {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
let left: u8 = self.into();
let right: u8 = rhs.into();
let result = left ^ right;
unsafe { Nibble::new_unchecked(result) }
}
}
impl std::ops::BitXorAssign for Nibble {
fn bitxor_assign(&mut self, rhs: Self) {
*self = *self ^ rhs;
}
}
impl std::ops::Not for Nibble {
type Output = Self;
fn not(self) -> Self::Output {
let value: u8 = self.into();
let mask: u8 = 0b00001111;
// a bitwise not will flip the upper four bits
// to ones, so we mask them off.
let result: u8 = !value & mask;
unsafe { Nibble::new_unchecked(result) }
}
}
impl std::ops::Shl<u8> for Nibble {
type Output = Nibble;
fn shl(self, rhs: u8) -> Self::Output {
let lhs: u8 = self.into();
let result = lhs << rhs;
match Nibble::try_from(result) {
Ok(nibble) => nibble,
Err(_) => panic!(
"the value {:#x} (created by {} << {}) cannot be represented as a nibble",
result, lhs, rhs
),
}
}
}
impl std::ops::ShlAssign<u8> for Nibble {
fn shl_assign(&mut self, rhs: u8) {
*self = *self << rhs;
}
}
impl std::ops::Shr<u8> for Nibble {
type Output = Nibble;
fn shr(self, rhs: u8) -> Self::Output {
let lhs: u8 = self.into();
let result = lhs >> rhs;
match Nibble::try_from(result) {
Ok(nibble) => nibble,
Err(_) => panic!(
"the value {:#x} (created by {} >> {}) cannot be represented as a nibble.",
result, lhs, rhs
),
}
}
}
impl std::ops::ShrAssign<u8> for Nibble {
fn shr_assign(&mut self, rhs: u8) {
*self = *self >> rhs;
}
}
// OTHER IMPLS
impl Nibble {
/// 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.
pub const BITS: u32 = 4u32;
/// Constructs a new [`Nibble`] representing the given value
/// without checking invariants.
///
/// # Safety
/// `value` must be strictly less than 16.
#[inline]
pub const unsafe fn new_unchecked(value: u8) -> Self {
std::mem::transmute(value)
}
/// Constructs a new [`Nibble`] representing the given value,
/// or panics if this is not possible. Prefer using `try_from`
/// instead if you do not need the construction to be `const`.
///
/// # Panics
/// This function will panic if `value >= 16`.
#[inline]
pub const fn new_checked(value: u8) -> Self {
assert!(Nibble::can_represent(value));
unsafe { Nibble::new_unchecked(value) }
}
/// Consumes `self` and returns a `u8` representing its value, guaranteed
/// to be at most 15.
#[inline]
pub const fn get(&self) -> u8 {
self.0.get()
}
/// 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.
#[inline]
pub const fn pair_from_byte(value: u8) -> (Self, Self) {
let upper = unsafe { Self::new_unchecked(value >> 4) };
let lower = unsafe { Self::new_unchecked(value & 0x0F) };
(upper, lower)
}
/// Checks whether the given `u8` can be safely converted into a [`Nibble`],
/// returning this information as a `bool`.
///
/// Prefer using this check over an ad-hoc implementation before making calls
/// to `Nibble::new_unchecked`, since it is faster than the naive `x < 16` and
/// can be tested independently.
#[inline]
pub(crate) const fn can_represent(value: u8) -> bool {
(value & 0xF0) == 0x00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_of_option_nibble_equals_size_of_nibble() {
assert_eq!(
std::mem::size_of::<Option<Nibble>>(),
std::mem::size_of::<Nibble>()
)
}
#[test]
fn representable_nibble_values_transmute_correctly() {
for i in 0..=15u8 {
let nibble: Nibble = unsafe { Nibble::new_unchecked(i) };
assert_eq!(nibble.get(), i);
}
}
#[test]
fn nibble_try_from_u8_is_correct() {
// acceptable nibble values
for value in 0..=15 {
assert!(Nibble::try_from(value as u8).is_ok());
}
// unacceptable nibble values
for value in 16..=u8::MAX {
assert!(Nibble::try_from(value).is_err());
}
}
#[test]
fn nibble_into_u8_is_correct() {
for i in 0..=15 {
let nibble = Nibble::try_from(i as u8).unwrap();
let byte: u8 = nibble.into();
assert_eq!(byte, i);
}
}
#[test]
fn unsafe_nibble_new_unchecked_is_valid_given_invariants() {
for i in 0..=15 {
let nibble = unsafe { Nibble::new_unchecked(i) };
let byte: u8 = nibble.into();
assert_eq!(byte, i);
}
}
#[test]
fn bitwise_not_produces_valid_nibbles() {
for value in 0u8..15u8 {
let nibble = Nibble::try_from(value).unwrap();
let complement = !nibble;
eprintln!("{:#06b} --> {:#06b}", nibble, complement);
assert!(nibble.get() + complement.get() == 15u8);
}
}
#[test]
fn shl_produces_correct_values() {
let one = Nibble::ONE;
let two = one << 1;
let four = one << 2;
let eight = one << 3;
assert_eq!(one.get(), 0b0001);
assert_eq!(two.get(), 0b0010);
assert_eq!(four.get(), 0b0100);
assert_eq!(eight.get(), 0b1000);
}
#[test]
fn shr_produces_correct_values() {
let fifteen = Nibble::try_from(15u8).unwrap();
let seven = fifteen >> 1;
let three = fifteen >> 2;
let one = fifteen >> 3;
assert_eq!(fifteen.get(), 0b1111);
assert_eq!(seven.get(), 0b0111);
assert_eq!(three.get(), 0b0011);
assert_eq!(one.get(), 0b0001);
}
#[test]
fn nibble_can_represent_u8_check_is_correct() {
// valid cases
for i in 0..=15u8 {
assert!(Nibble::can_represent(i));
}
// invalid cases
for i in 16..=u8::MAX {
assert!(!Nibble::can_represent(i));
}
}
}