use std::fmt::{self, Debug, Display, Formatter};
#[repr(transparent)]
#[derive(Copy)]
#[derive_const(
Clone,
Default,
Eq,
Ord,
PartialEq,
PartialOrd,
)]
pub struct Byte(u8);
impl Byte {
pub const BITS: u32 = 8;
#[inline(always)]
#[must_use]
pub const fn from_u8(value: u8) -> Self {
Self(value)
}
#[inline]
#[must_use]
pub const fn from_i8(value: i8) -> Self {
Self(value.cast_unsigned())
}
#[inline(always)]
#[must_use]
pub const fn as_u8(self) -> u8 {
self.0
}
#[inline]
#[must_use]
pub const fn as_i8(self) -> i8 {
self.0.cast_signed()
}
}
impl Debug for Byte {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let prefix = match self.as_u8().max(1).ilog(16) {
0 => "$.",
1 => "$",
_ => {
unreachable!();
}
};
write!(f, "{prefix}{:x}", self.as_u8())
}
}
impl Display for Byte {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(self, f)
}
}
const impl From<i8> for Byte {
#[inline(always)]
fn from(value: i8) -> Self {
Self::from_i8(value)
}
}
const impl From<u8> for Byte {
#[inline(always)]
fn from(value: u8) -> Self {
Self::from_u8(value)
}
}