Add 'NUM_VARIANTS' constant to all 'board' enums

This commit is contained in:
Bruno BELANYI 2022-07-20 19:19:38 +02:00
parent 3f93417092
commit 42126e1c88
5 changed files with 23 additions and 8 deletions

View file

@ -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) }
}