Don't return 'Bitboard' from 'Square::{file,rank}'

This commit is contained in:
Bruno BELANYI 2022-07-16 14:20:33 +02:00
parent ef15da41ea
commit c3c3684150

View file

@ -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 /// Represent a square on a chessboard. Defined in the same order as the
/// [Bitboard](crate::board::Bitboard) squares. /// [Bitboard](crate::board::Bitboard) squares.
@ -69,16 +69,18 @@ impl Square {
(self as usize) / 8 (self as usize) / 8
} }
/// Return a bitboard representing the rank of this square. /// Return a [Rank] representing the rank of this square.
#[inline(always)] #[inline(always)]
pub fn rank(self) -> Bitboard { pub fn rank(self) -> Rank {
Bitboard::RANKS[self.rank_index()] // 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)] #[inline(always)]
pub fn file(self) -> Bitboard { pub fn file(self) -> File {
Bitboard::FILES[self.file_index()] // SAFETY: we know the value is in-bounds
unsafe { File::from_index_unchecked(self.file_index()) }
} }
/// Turn a square into a singleton bitboard. /// Turn a square into a singleton bitboard.
@ -174,6 +176,24 @@ impl std::ops::Sub<Bitboard> for Square {
mod test { mod test {
use super::*; use super::*;
use crate::board::bitboard::*; 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] #[test]
fn left_shift() { fn left_shift() {