use core::fmt;
use core::mem::transmute;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug, Hash)]
#[repr(u8)]
pub enum Rank {
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
}
impl fmt::Display for Rank {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::One => write!(f, "One"),
Self::Two => write!(f, "Two"),
Self::Three => write!(f, "Three"),
Self::Four => write!(f, "Four"),
Self::Five => write!(f, "Five"),
Self::Six => write!(f, "Six"),
Self::Seven => write!(f, "Seven"),
Self::Eight => write!(f, "Eight"),
}
}
}
impl Rank {
pub const NUM_RANKS: usize = 8;
pub const ALL: [Self; 8] = [
Self::One,
Self::Two,
Self::Three,
Self::Four,
Self::Five,
Self::Six,
Self::Seven,
Self::Eight,
];
#[inline(always)]
pub const fn from_index(index: usize) -> Rank {
unsafe { transmute(index as u8 & 7) }
}
#[inline(always)]
pub const fn to_index(self) -> usize {
self as usize
}
#[inline(always)]
pub const fn up(self) -> Self {
unsafe { transmute((self as u8 + 1) & 7) }
}
#[inline(always)]
pub const fn down(self) -> Self {
unsafe { transmute((self as u8).wrapping_sub(1) & 7) }
}
}