use std::fmt::{self, Display, Formatter};
#[repr(u8)]
#[derive(Copy, Debug)]
#[derive_const(
Clone,
Eq,
Ord,
PartialEq,
PartialOrd,
)]
pub enum Dn {
D0 = 0,
D1 = 1,
D2 = 2,
D3 = 3,
D4 = 4,
D5 = 5,
D6 = 6,
D7 = 7,
}
impl Dn {
pub const MIN: Self = Self::D0;
pub const MAX: Self = Self::D7;
#[inline]
#[must_use]
pub const fn from_u16(mut index: u16) -> Self {
index &= 0b111;
match index {
0 => Self::D0,
1 => Self::D1,
2 => Self::D2,
3 => Self::D3,
4 => Self::D4,
5 => Self::D5,
6 => Self::D6,
7 => Self::D7,
_ => {
unreachable!();
}
}
}
#[inline(always)]
#[must_use]
pub const fn as_u16(self) -> u16 {
self as u16
}
#[inline]
#[must_use]
pub const fn as_usize(self) -> usize {
self.as_u16().into()
}
#[inline]
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::D0 => "D0",
Self::D1 => "D1",
Self::D2 => "D2",
Self::D3 => "D3",
Self::D4 => "D4",
Self::D5 => "D5",
Self::D6 => "D6",
Self::D7 => "D7",
}
}
}
impl Display for Dn {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}