From 17b8ee5eb30a5df8d844dd8b2d96042afb725b47 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 17 Jul 2022 21:30:23 +0200 Subject: [PATCH] Add 'Square::index' --- src/board/square.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/board/square.rs b/src/board/square.rs index 1b8e932..7f1c822 100644 --- a/src/board/square.rs +++ b/src/board/square.rs @@ -60,6 +60,12 @@ impl Square { 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). #[inline(always)] pub fn rank_index(self) -> usize { @@ -188,6 +194,14 @@ mod test { 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] fn file() { assert_eq!(Square::A1.file(), File::A);