WIP
This commit is contained in:
parent
d5cdf273c8
commit
091cc99cb3
42 changed files with 805 additions and 1662 deletions
|
|
@ -217,6 +217,43 @@ coordinate_enum!(Square, [
|
|||
A8, B8, C8, D8, E8, F8, G8, H8
|
||||
]);
|
||||
|
||||
/// Generate an enum that maps its values to variants of [Square].
|
||||
macro_rules! to_square_enum {
|
||||
($vis:vis $name:ident { $($variant:ident)* }) => {
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||
$vis enum $name {
|
||||
$($variant = Square::$variant as u8,)*
|
||||
}
|
||||
|
||||
impl From<$name> for Square {
|
||||
fn from(value: $name) -> Self {
|
||||
unsafe { Square::from_index_unchecked(value as u8) }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
to_square_enum!(
|
||||
pub EnPassantTargetSquare {
|
||||
A3 B3 C3 D3 E3 F3 G3 H3
|
||||
A6 B6 C6 D6 E6 F6 G6 H6
|
||||
}
|
||||
);
|
||||
|
||||
// impl TryFrom<Square> for EnPassantTargetSquare {
|
||||
// type Error = ();
|
||||
|
||||
// fn try_from(value: Square) -> Result<Self, Self::Error> {
|
||||
// let square = Self::ALL[value as usize];
|
||||
// if square as usize == value as usize {
|
||||
// Ok(square)
|
||||
// } else {
|
||||
// Err(())
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Square {
|
||||
/// # Safety
|
||||
///
|
||||
|
|
@ -240,20 +277,24 @@ impl Square {
|
|||
s.parse()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn file(self) -> File {
|
||||
unsafe { File::new_unchecked((self as u8) & 0b000_00111) }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn rank(self) -> Rank {
|
||||
unsafe { Rank::new_unchecked((self as u8) >> 3) }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn file_rank(&self) -> (File, Rank) {
|
||||
(self.file(), self.rank())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn neighbor(self, direction: Direction) -> Option<Square> {
|
||||
let index: u8 = self as u8;
|
||||
let dir: i8 = direction.to_offset();
|
||||
|
|
@ -347,10 +388,9 @@ impl From<File> for char {
|
|||
}
|
||||
}
|
||||
|
||||
impl Into<char> for Rank {
|
||||
fn into(self) -> char {
|
||||
let value: u8 = self.into();
|
||||
(value + b'1') as char
|
||||
impl From<Rank> for char {
|
||||
fn from(value: Rank) -> Self {
|
||||
Self::from(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue