From d919b956ed6fc5137ab137fc46b26016cc5564a8 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 20 Jul 2022 19:19:38 +0200 Subject: [PATCH] Add 'NUM_VARIANTS' constant to all 'board' enums --- src/board/castle_rights.rs | 5 ++++- src/board/color.rs | 5 ++++- src/board/file.rs | 7 +++++-- src/board/rank.rs | 7 +++++-- src/board/square.rs | 7 +++++-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/board/castle_rights.rs b/src/board/castle_rights.rs index b398b57..01f0235 100644 --- a/src/board/castle_rights.rs +++ b/src/board/castle_rights.rs @@ -14,10 +14,13 @@ pub enum CastleRights { } impl CastleRights { + /// The number of [CastleRights] variants. + pub const NUM_VARIANTS: usize = 4; + /// Convert from a castle rights index into a [CastleRights] type. #[inline(always)] pub fn from_index(index: usize) -> Self { - assert!(index < 4); + assert!(index < Self::NUM_VARIANTS); // SAFETY: we know the value is in-bounds unsafe { Self::from_index_unchecked(index) } } diff --git a/src/board/color.rs b/src/board/color.rs index d5c66d3..62fdd13 100644 --- a/src/board/color.rs +++ b/src/board/color.rs @@ -8,10 +8,13 @@ pub enum Color { } impl Color { + /// The number of [Color] variants. + pub const NUM_VARIANTS: usize = 2; + /// Convert from a color index into a [Color] type. #[inline(always)] pub fn from_index(index: usize) -> Self { - assert!(index < 2); + assert!(index < Self::NUM_VARIANTS); // SAFETY: we know the value is in-bounds unsafe { Self::from_index_unchecked(index) } } diff --git a/src/board/file.rs b/src/board/file.rs index 1601397..1475e9a 100644 --- a/src/board/file.rs +++ b/src/board/file.rs @@ -15,7 +15,10 @@ pub enum File { } impl File { - const ALL: [Self; 8] = [ + /// The number of [File] variants. + pub const NUM_VARIANTS: usize = 8; + + const ALL: [Self; Self::NUM_VARIANTS] = [ Self::A, Self::B, Self::C, @@ -34,7 +37,7 @@ impl File { /// Convert from a file index into a [File] type. #[inline(always)] pub fn from_index(index: usize) -> Self { - assert!(index < 8); + assert!(index < Self::NUM_VARIANTS); // SAFETY: we know the value is in-bounds unsafe { Self::from_index_unchecked(index) } } diff --git a/src/board/rank.rs b/src/board/rank.rs index c679278..f448df5 100644 --- a/src/board/rank.rs +++ b/src/board/rank.rs @@ -15,7 +15,10 @@ pub enum Rank { } impl Rank { - const ALL: [Self; 8] = [ + /// The number of [Rank] variants. + pub const NUM_VARIANTS: usize = 8; + + const ALL: [Self; Self::NUM_VARIANTS] = [ Self::First, Self::Second, Self::Third, @@ -34,7 +37,7 @@ impl Rank { /// Convert from a rank index into a [Rank] type. #[inline(always)] pub fn from_index(index: usize) -> Self { - assert!(index < 8); + assert!(index < Self::NUM_VARIANTS); // SAFETY: we know the value is in-bounds unsafe { Self::from_index_unchecked(index) } } diff --git a/src/board/square.rs b/src/board/square.rs index 9c0178e..c164320 100644 --- a/src/board/square.rs +++ b/src/board/square.rs @@ -23,8 +23,11 @@ impl std::fmt::Display for Square { } impl Square { + /// The number of [Square] variants. + pub const NUM_VARIANTS: usize = 64; + #[rustfmt::skip] - const ALL: [Self; 64] = [ + const ALL: [Self; Self::NUM_VARIANTS] = [ Self::A1, Self::A2, Self::A3, Self::A4, Self::A5, Self::A6, Self::A7, Self::A8, Self::B1, Self::B2, Self::B3, Self::B4, Self::B5, Self::B6, Self::B7, Self::B8, Self::C1, Self::C2, Self::C3, Self::C4, Self::C5, Self::C6, Self::C7, Self::C8, @@ -50,7 +53,7 @@ impl Square { /// Convert from a square index into a [Square] type. #[inline(always)] pub fn from_index(index: usize) -> Self { - assert!(index < 64); + assert!(index < Self::NUM_VARIANTS); // SAFETY: we know the value is in-bounds unsafe { Self::from_index_unchecked(index) } }