use std::fmt::{self, Display, Formatter};
#[repr(usize)]
#[derive(Copy, Debug)]
#[derive_const(
Clone,
Eq,
Ord,
PartialOrd,
PartialEq,
)]
pub enum Address {
A0 = 0,
A1 = 1,
A2 = 2,
A3 = 3,
A4 = 4,
A5 = 5,
A6 = 6,
A7 = 7,
}
impl Address {
#[inline]
#[must_use]
#[track_caller]
pub const fn from_usize(mut index: usize) -> Self {
if cfg!(overflow_checks) {
assert!(
index <= 7,
"cannot construct address register name with index greater than `7`",
);
}
index &= 0b111;
match index {
0 => Self::A0,
1 => Self::A1,
2 => Self::A2,
3 => Self::A3,
4 => Self::A4,
5 => Self::A5,
6 => Self::A6,
7 => Self::A7,
_ => {
unreachable!();
}
}
}
#[inline]
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::A0 => "%a0",
Self::A1 => "%a1",
Self::A2 => "%a2",
Self::A3 => "%a3",
Self::A4 => "%a4",
Self::A5 => "%a5",
Self::A6 => "%a6",
Self::A7 => "%a7",
}
}
#[inline]
#[must_use]
pub const fn as_usize(self) -> usize {
self as usize
}
}
impl Display for Address {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}