use std::fmt::{self, Display, Formatter};
#[repr(usize)]
#[derive(Copy, Debug)]
#[derive_const(
Clone,
Eq,
Ord,
PartialOrd,
PartialEq,
)]
pub enum Data {
D0 = 0,
D1 = 1,
D2 = 2,
D3 = 3,
D4 = 4,
D5 = 5,
D6 = 6,
D7 = 7,
}
impl Data {
#[inline]
#[must_use]
#[track_caller]
pub const fn from_usize(mut index: usize) -> Self {
if cfg!(overflow_checks) {
assert!(
index <= 7,
"cannot construct data register name with index greater than `7`",
);
}
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]
#[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",
}
}
#[inline]
#[must_use]
pub const fn as_usize(self) -> usize {
self as usize
}
}
impl Display for Data {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}