From 77b15edc36c3f36d9ecf39df29afc85c367ff88e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 16 Jul 2022 14:20:33 +0200 Subject: [PATCH] Don't return 'Bitboard' from 'Square::{file,rank}' --- src/board/square.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/board/square.rs b/src/board/square.rs index 78ea625..3cae19e 100644 --- a/src/board/square.rs +++ b/src/board/square.rs @@ -1,4 +1,4 @@ -use super::Bitboard; +use super::{Bitboard, File, Rank}; /// Represent a square on a chessboard. Defined in the same order as the /// [Bitboard](crate::board::Bitboard) squares. @@ -65,16 +65,18 @@ impl Square { (self as usize) / 8 } - /// Return a bitboard representing the rank of this square. + /// Return a [Rank] representing the rank of this square. #[inline(always)] - pub fn rank(self) -> Bitboard { - Bitboard::RANKS[self.rank_index()] + pub fn rank(self) -> Rank { + // SAFETY: we know the value is in-bounds + unsafe { Rank::from_index_unchecked(self.rank_index()) } } - /// Return a bitboard representing the rank of this square. + /// Return a [File] representing the rank of this square. #[inline(always)] - pub fn file(self) -> Bitboard { - Bitboard::FILES[self.file_index()] + pub fn file(self) -> File { + // SAFETY: we know the value is in-bounds + unsafe { File::from_index_unchecked(self.file_index()) } } /// Turn a square into a singleton bitboard. @@ -168,6 +170,24 @@ impl std::ops::Sub for Square { mod test { use super::*; use crate::board::bitboard::*; + use crate::board::file::*; + use crate::board::rank::*; + + #[test] + fn file() { + assert_eq!(Square::A1.file(), File::A); + assert_eq!(Square::A2.file(), File::A); + assert_eq!(Square::B1.file(), File::B); + assert_eq!(Square::H8.file(), File::H); + } + + #[test] + fn rank() { + assert_eq!(Square::A1.rank(), Rank::First); + assert_eq!(Square::A2.rank(), Rank::Second); + assert_eq!(Square::B1.rank(), Rank::First); + assert_eq!(Square::H8.rank(), Rank::Eighth); + } #[test] fn left_shift() {