[board, core, bitboard] Clean up casts between Rank, File and BitBoard

Let BitBoard::rank and BitBoard::file take a Rank and File directly, instead of a
u8 by reference. And then make the Rank/File::as_index const and return a value
rather than a reference.

All this allows you to convert between Rank, File, and BitBoard at compile tile
(i.e. as a const method) rather than needing to do runtime stuff.
This commit is contained in:
Eryn Wells 2025-05-23 18:32:18 -07:00
parent 588f049290
commit 3684e9b425
3 changed files with 24 additions and 25 deletions

View file

@ -58,16 +58,12 @@ impl BitBoard {
BitBoard(bits) BitBoard(bits)
} }
// TODO: Is &u8 really necessary here? pub const fn rank(rank: Rank) -> BitBoard {
pub fn rank(rank: &u8) -> BitBoard { library::RANKS[rank.as_index()]
debug_assert!(*rank < 8);
library::RANKS[*rank as usize]
} }
// TODO: Is &u8 really necessary here? pub const fn file(file: File) -> BitBoard {
pub fn file(file: &u8) -> BitBoard { library::FILES[file.as_index()]
debug_assert!(*file < 8);
library::FILES[*file as usize]
} }
pub fn ray(sq: Square, dir: Direction) -> BitBoard { pub fn ray(sq: Square, dir: Direction) -> BitBoard {
@ -313,7 +309,7 @@ impl From<BitBoard> for u64 {
impl From<File> for BitBoard { impl From<File> for BitBoard {
fn from(value: File) -> Self { fn from(value: File) -> Self {
library::FILES[*value.as_index() as usize] library::FILES[value.as_index()]
} }
} }
@ -325,7 +321,7 @@ impl From<Option<Square>> for BitBoard {
impl From<Rank> for BitBoard { impl From<Rank> for BitBoard {
fn from(value: Rank) -> Self { fn from(value: Rank) -> Self {
library::FILES[*value.as_index() as usize] library::FILES[value.as_index()]
} }
} }
@ -466,21 +462,24 @@ mod tests {
#[test] #[test]
#[ignore] #[ignore]
fn display_and_debug() { fn display_and_debug() {
let bb = BitBoard::file(&0) | BitBoard::file(&3) | BitBoard::rank(&7) | BitBoard::rank(&4); let bb = BitBoard::file(File::A)
| BitBoard::file(File::D)
| BitBoard::rank(Rank::FIVE)
| BitBoard::rank(Rank::EIGHT);
println!("{}", &bb); println!("{}", &bb);
} }
#[test] #[test]
#[allow(clippy::unreadable_literal)] #[allow(clippy::unreadable_literal)]
fn rank() { fn rank() {
assert_eq!(BitBoard::rank(&0).0, 0xFF, "Rank 1"); assert_eq!(BitBoard::rank(Rank::ONE).0, 0xFF, "Rank 1");
assert_eq!(BitBoard::rank(&1).0, 0xFF00, "Rank 2"); assert_eq!(BitBoard::rank(Rank::TWO).0, 0xFF00, "Rank 2");
assert_eq!(BitBoard::rank(&2).0, 0xFF0000, "Rank 3"); assert_eq!(BitBoard::rank(Rank::THREE).0, 0xFF0000, "Rank 3");
assert_eq!(BitBoard::rank(&3).0, 0xFF000000, "Rank 4"); assert_eq!(BitBoard::rank(Rank::FOUR).0, 0xFF000000, "Rank 4");
assert_eq!(BitBoard::rank(&4).0, 0xFF00000000, "Rank 5"); assert_eq!(BitBoard::rank(Rank::FIVE).0, 0xFF00000000, "Rank 5");
assert_eq!(BitBoard::rank(&5).0, 0xFF0000000000, "Rank 6"); assert_eq!(BitBoard::rank(Rank::SIX).0, 0xFF0000000000, "Rank 6");
assert_eq!(BitBoard::rank(&6).0, 0xFF000000000000, "Rank 7"); assert_eq!(BitBoard::rank(Rank::SEVEN).0, 0xFF000000000000, "Rank 7");
assert_eq!(BitBoard::rank(&7).0, 0xFF00000000000000, "Rank 8"); assert_eq!(BitBoard::rank(Rank::EIGHT).0, 0xFF00000000000000, "Rank 8");
} }
#[test] #[test]

View file

@ -63,10 +63,10 @@ fn pawn_pushes(pawn: BitBoard, color: Color, occupancy: BitBoard) -> BitBoard {
match color { match color {
Color::White => { Color::White => {
let second_rank = BitBoard::rank(&Rank::TWO.into()); const SECOND_RANK: BitBoard = BitBoard::rank(Rank::TWO);
let mut pushes = pawn.shift_north_one() & vacancy; let mut pushes = pawn.shift_north_one() & vacancy;
if !(pawn & second_rank).is_empty() { if !(pawn & SECOND_RANK).is_empty() {
// Double push // Double push
pushes = pushes | (pushes.shift_north_one() & vacancy); pushes = pushes | (pushes.shift_north_one() & vacancy);
} }
@ -74,10 +74,10 @@ fn pawn_pushes(pawn: BitBoard, color: Color, occupancy: BitBoard) -> BitBoard {
pushes pushes
} }
Color::Black => { Color::Black => {
let seventh_rank = BitBoard::rank(&Rank::SEVEN.into()); const SEVENTH_RANK: BitBoard = BitBoard::rank(Rank::SEVEN);
let mut pushes = pawn.shift_south_one() & vacancy; let mut pushes = pawn.shift_south_one() & vacancy;
if !(pawn & seventh_rank).is_empty() { if !(pawn & SEVENTH_RANK).is_empty() {
// Double push // Double push
pushes = pushes | (pushes.shift_south_one() & vacancy); pushes = pushes | (pushes.shift_south_one() & vacancy);
} }

View file

@ -88,8 +88,8 @@ macro_rules! range_bound_struct {
} }
#[must_use] #[must_use]
$vis fn as_index(&self) -> &$repr { $vis const fn as_index(&self) -> usize {
&self.0 self.0 as usize
} }
$vis fn iter(&self) -> impl Iterator<Item = Self> { $vis fn iter(&self) -> impl Iterator<Item = Self> {