From 270ac66db47b6e227828d565fbedf47c0d1c2384 Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Wed, 26 Nov 2025 23:03:23 +0100 Subject: [PATCH] update vocabulary --- src/board.rs | 90 +++++++++++++++++++--------------------------------- src/setup.rs | 3 +- 2 files changed, 35 insertions(+), 58 deletions(-) diff --git a/src/board.rs b/src/board.rs index c5b1787..6e7b98b 100644 --- a/src/board.rs +++ b/src/board.rs @@ -4,7 +4,10 @@ use crate::bitboard::*; macro_rules! container { ($v:vis, $a:ident, $b:ident, $n:literal) => { - #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] + #[doc = "Container with values for each [`"] + #[doc = stringify!($a)] + #[doc = "`]."] + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] $v struct $b(pub(crate) [T; $n]); #[allow(unused)] impl $b { @@ -123,9 +126,7 @@ impl File { } } - /// ## Safety - /// - /// The caller must ensure that `index < 8`. + /// **Safety:** The caller must ensure that `index < 8`. #[inline] pub const unsafe fn new_unchecked(index: u8) -> Self { debug_assert!(index < 8); @@ -216,9 +217,7 @@ impl Rank { } } - /// ## Safety - /// - /// The caller must ensure that `index < 8`. + /// **Safety:** The caller must ensure that `index < 8`. #[inline] pub const unsafe fn new_unchecked(index: u8) -> Self { debug_assert!(index < 8); @@ -257,13 +256,13 @@ impl Rank { } #[inline] - pub const fn mirror(self) -> Self { - unsafe { Self::new_unchecked(!(self as u8)) } + pub const fn bitboard(self) -> Bitboard { + Bitboard(0xFF << ((self as u64) << 3)) } #[inline] - pub const fn bitboard(self) -> Bitboard { - Bitboard(0xFF << ((self as u64) << 3)) + pub const fn mirror(self) -> Self { + unsafe { Self::new_unchecked(!(self as u8)) } } } @@ -315,9 +314,7 @@ impl Square { } } - /// ## Safety - /// - /// The caller must ensure that `index < 64`. + /// **Safety:** The caller must ensure that `index < 64`. #[inline] pub const unsafe fn new_unchecked(index: u8) -> Self { debug_assert!(index < 64); @@ -339,47 +336,15 @@ impl Square { unsafe { Rank::new_unchecked((self as u8) >> 3) } } - #[inline] - pub const fn mirror(self) -> Self { - let sq = self as u8; - unsafe { Self::new_unchecked(sq & 0b000111 | (!sq & 0b111000)) } - } - #[inline] pub const fn bitboard(self) -> Bitboard { Bitboard(1 << self as u8) } #[inline] - #[rustfmt::skip] - pub(crate) fn to_str(self) -> &'static str { - match self { - Self::A1 => "a1", Self::B1 => "b1", Self::C1 => "c1", Self::D1 => "d1", Self::E1 => "e1", Self::F1 => "f1", Self::G1 => "g1", Self::H1 => "h1", - Self::A2 => "a2", Self::B2 => "b2", Self::C2 => "c2", Self::D2 => "d2", Self::E2 => "e2", Self::F2 => "f2", Self::G2 => "g2", Self::H2 => "h2", - Self::A3 => "a3", Self::B3 => "b3", Self::C3 => "c3", Self::D3 => "d3", Self::E3 => "e3", Self::F3 => "f3", Self::G3 => "g3", Self::H3 => "h3", - Self::A4 => "a4", Self::B4 => "b4", Self::C4 => "c4", Self::D4 => "d4", Self::E4 => "e4", Self::F4 => "f4", Self::G4 => "g4", Self::H4 => "h4", - Self::A5 => "a5", Self::B5 => "b5", Self::C5 => "c5", Self::D5 => "d5", Self::E5 => "e5", Self::F5 => "f5", Self::G5 => "g5", Self::H5 => "h5", - Self::A6 => "a6", Self::B6 => "b6", Self::C6 => "c6", Self::D6 => "d6", Self::E6 => "e6", Self::F6 => "f6", Self::G6 => "g6", Self::H6 => "h6", - Self::A7 => "a7", Self::B7 => "b7", Self::C7 => "c7", Self::D7 => "d7", Self::E7 => "e7", Self::F7 => "f7", Self::G7 => "g7", Self::H7 => "h7", - Self::A8 => "a8", Self::B8 => "b8", Self::C8 => "c8", Self::D8 => "d8", Self::E8 => "e8", Self::F8 => "f8", Self::G8 => "g8", Self::H8 => "h8", - } - } - - #[inline] - pub(crate) fn from_str(s: &str) -> Option { - match s.as_bytes() { - [f, r] => Self::from_ascii(&[*f, *r]), - _ => None, - } - } - - #[inline] - pub(crate) fn from_ascii(s: &[u8; 2]) -> Option { - let [f, r] = *s; - Some(Self::from_coords( - File::from_ascii(f)?, - Rank::from_ascii(r)?, - )) + pub const fn mirror(self) -> Self { + let sq = self as u8; + unsafe { Self::new_unchecked(sq & 0b000111 | (!sq & 0b111000)) } } #[inline] @@ -431,7 +396,10 @@ impl Square { impl core::fmt::Display for Square { #[inline] fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - f.write_str(self.to_str()) + use core::fmt::Write; + f.write_char(self.file().to_char())?; + f.write_char(self.rank().to_char())?; + Ok(()) } } @@ -449,7 +417,14 @@ impl core::str::FromStr for Square { type Err = ParseSquareError; #[inline] fn from_str(s: &str) -> Result { - Self::from_str(s).ok_or(ParseSquareError) + (|| match s.as_bytes() { + [f, r] => Some(Self::from_coords( + File::from_ascii(*f)?, + Rank::from_ascii(*r)?, + )), + _ => None, + })() + .ok_or(ParseSquareError) } } @@ -568,7 +543,8 @@ impl Role { } } -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +/// Container with values for each [`Role`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ByRole(pub(crate) [T; 6]); #[allow(unused)] impl ByRole { @@ -578,15 +554,15 @@ impl ByRole { } #[inline] - pub const fn get(&self, role: Role) -> &T { - let i = unsafe { (role as usize).unchecked_sub(1) }; + pub const fn get(&self, k: Role) -> &T { + let i = unsafe { (k as usize).unchecked_sub(1) }; unsafe { core::hint::assert_unchecked(i < 6) }; &self.0[i] } #[inline] - pub const fn get_mut(&mut self, role: Role) -> &mut T { - let i = unsafe { (role as usize).unchecked_sub(1) }; + pub const fn get_mut(&mut self, k: Role) -> &mut T { + let i = unsafe { (k as usize).unchecked_sub(1) }; unsafe { core::hint::assert_unchecked(i < 6) }; &mut self.0[i] } @@ -701,7 +677,7 @@ pub enum CastlingSide { Long, } -container!(pub(crate), CastlingSide, ByCastlingSide, 2); +container!(pub, CastlingSide, ByCastlingSide, 2); impl CastlingSide { #[inline] diff --git a/src/setup.rs b/src/setup.rs index 5174d42..cf4c57b 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -210,7 +210,8 @@ impl Setup { match self.en_passant.try_into_square() { Some(sq) => { - w.write_str(sq.to_str())?; + w.write_char(sq.file().to_char())?; + w.write_char(sq.rank().to_char())?; } None => { w.write_char('-')?;