Add 'Square::index'

This commit is contained in:
Bruno BELANYI 2022-07-17 21:30:23 +02:00
parent 8261b0c06b
commit 281c79556a

View file

@ -64,6 +64,12 @@ impl Square {
std::mem::transmute(index as u8) std::mem::transmute(index as u8)
} }
/// Return the index of a given [Square].
#[inline(always)]
pub fn index(self) -> usize {
self as usize
}
/// Return the index of the rank of this square (0 -> rank 1, ..., 7 -> rank 8). /// Return the index of the rank of this square (0 -> rank 1, ..., 7 -> rank 8).
#[inline(always)] #[inline(always)]
pub fn rank_index(self) -> usize { pub fn rank_index(self) -> usize {
@ -194,6 +200,14 @@ mod test {
assert_eq!(Square::new(File::H, Rank::Eighth), Square::H8); assert_eq!(Square::new(File::H, Rank::Eighth), Square::H8);
} }
#[test]
fn index() {
assert_eq!(Square::A1.index(), 0);
assert_eq!(Square::A2.index(), 1);
assert_eq!(Square::B1.index(), 8);
assert_eq!(Square::H8.index(), 63);
}
#[test] #[test]
fn file() { fn file() {
assert_eq!(Square::A1.file(), File::A); assert_eq!(Square::A1.file(), File::A);