use std::cmp::Ordering;
use std::fmt::{self, Debug, Display, Formatter};
use std::mem::transmute;
#[repr(transparent)]
#[derive(Copy)]
#[derive_const(Clone)]
pub struct InstructionLen(pattern_type!(usize is 1..=6));
impl InstructionLen {
pub const MIN: Self = Self::new(1).unwrap();
pub const MAX: Self = Self::new(6).unwrap();
#[inline]
#[must_use]
pub const fn new(value: usize) -> Option<Self> {
if let 1..=6 = value {
Some(unsafe { transmute::<usize, Self>(value) })
} else {
None
}
}
#[inline(always)]
#[must_use]
pub const fn get(self) -> usize {
unsafe { transmute::<Self, usize>(self) }
}
}
impl Debug for InstructionLen {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.get(), f)
}
}
impl Display for InstructionLen {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.get(), f)
}
}
const impl Eq for InstructionLen {}
const impl From<InstructionLen> for usize {
#[inline(always)]
fn from(value: InstructionLen) -> Self {
value.get()
}
}
const impl Ord for InstructionLen {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.get().cmp(&other.get())
}
}
const impl PartialEq for InstructionLen {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.get().eq(&other.get())
}
}
const impl PartialOrd for InstructionLen {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}