Add 'try_from_index' implementations

This commit is contained in:
Bruno BELANYI 2024-04-03 20:44:15 +01:00
parent 753f1590d1
commit 8e688a0cac
6 changed files with 70 additions and 18 deletions

View file

@ -24,9 +24,18 @@ impl CastleRights {
/// Panics if the index is out of bounds. /// Panics if the index is out of bounds.
#[inline(always)] #[inline(always)]
pub fn from_index(index: usize) -> Self { pub fn from_index(index: usize) -> Self {
assert!(index < Self::NUM_VARIANTS); Self::try_from_index(index).expect("index out of bouds")
}
/// Convert from a castle rights index into a [CastleRights] type. Returns [None] if the index
/// is out of bounds.
pub fn try_from_index(index: usize) -> Option<Self> {
if index < Self::NUM_VARIANTS {
// SAFETY: we know the value is in-bounds // SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(index) } Some(unsafe { Self::from_index_unchecked(index) })
} else {
None
}
} }
/// Convert from a castle rights index into a [CastleRights] type, no bounds checking. /// Convert from a castle rights index into a [CastleRights] type, no bounds checking.

View file

@ -25,9 +25,18 @@ impl Color {
/// Panics if the index is out of bounds. /// Panics if the index is out of bounds.
#[inline(always)] #[inline(always)]
pub fn from_index(index: usize) -> Self { pub fn from_index(index: usize) -> Self {
assert!(index < Self::NUM_VARIANTS); Self::try_from_index(index).expect("index out of bouds")
}
/// Convert from a color index into a [Color] type. Returns [None] if the index is out of
/// bounds.
pub fn try_from_index(index: usize) -> Option<Self> {
if index < Self::NUM_VARIANTS {
// SAFETY: we know the value is in-bounds // SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(index) } Some(unsafe { Self::from_index_unchecked(index) })
} else {
None
}
} }
/// Convert from a color index into a [Color] type, no bounds checking. /// Convert from a color index into a [Color] type, no bounds checking.

View file

@ -41,9 +41,17 @@ impl File {
/// Panics if the index is out of bounds. /// Panics if the index is out of bounds.
#[inline(always)] #[inline(always)]
pub fn from_index(index: usize) -> Self { pub fn from_index(index: usize) -> Self {
assert!(index < Self::NUM_VARIANTS); Self::try_from_index(index).expect("index out of bouds")
}
/// Convert from a file index into a [File] type. Returns [None] if the index is out of bounds.
pub fn try_from_index(index: usize) -> Option<Self> {
if index < Self::NUM_VARIANTS {
// SAFETY: we know the value is in-bounds // SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(index) } Some(unsafe { Self::from_index_unchecked(index) })
} else {
None
}
} }
/// Convert from a file index into a [File] type, no bounds checking. /// Convert from a file index into a [File] type, no bounds checking.

View file

@ -34,9 +34,18 @@ impl Piece {
/// Panics if the index is out of bounds. /// Panics if the index is out of bounds.
#[inline(always)] #[inline(always)]
pub fn from_index(index: usize) -> Self { pub fn from_index(index: usize) -> Self {
assert!(index < Self::NUM_VARIANTS); Self::try_from_index(index).expect("index out of bouds")
}
/// Convert from a piece index into a [Piece] type. Returns [None] if the index is out of
/// bounds.
pub fn try_from_index(index: usize) -> Option<Self> {
if index < Self::NUM_VARIANTS {
// SAFETY: we know the value is in-bounds // SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(index) } Some(unsafe { Self::from_index_unchecked(index) })
} else {
None
}
} }
/// Convert from a piece index into a [Piece] type, no bounds checking. /// Convert from a piece index into a [Piece] type, no bounds checking.

View file

@ -41,9 +41,17 @@ impl Rank {
/// Panics if the index is out of bounds. /// Panics if the index is out of bounds.
#[inline(always)] #[inline(always)]
pub fn from_index(index: usize) -> Self { pub fn from_index(index: usize) -> Self {
assert!(index < Self::NUM_VARIANTS); Self::try_from_index(index).expect("index out of bouds")
}
/// Convert from a rank index into a [Rank] type. Returns [None] if the index is out of bounds.
pub fn try_from_index(index: usize) -> Option<Self> {
if index < Self::NUM_VARIANTS {
// SAFETY: we know the value is in-bounds // SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(index) } Some(unsafe { Self::from_index_unchecked(index) })
} else {
None
}
} }
/// Convert from a rank index into a [Rank] type, no bounds checking. /// Convert from a rank index into a [Rank] type, no bounds checking.

View file

@ -57,9 +57,18 @@ impl Square {
/// Convert from a square index into a [Square] type. /// Convert from a square index into a [Square] type.
#[inline(always)] #[inline(always)]
pub fn from_index(index: usize) -> Self { pub fn from_index(index: usize) -> Self {
assert!(index < Self::NUM_VARIANTS); Self::try_from_index(index).expect("index out of bouds")
}
/// Convert from a square index into a [Square] type. Returns [None] if the index is out of
/// bounds.
pub fn try_from_index(index: usize) -> Option<Self> {
if index < Self::NUM_VARIANTS {
// SAFETY: we know the value is in-bounds // SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(index) } Some(unsafe { Self::from_index_unchecked(index) })
} else {
None
}
} }
/// Convert from a square index into a [Square] type, no bounds checking. /// Convert from a square index into a [Square] type, no bounds checking.