Add 'try_from_index' implementations
This commit is contained in:
parent
753f1590d1
commit
8e688a0cac
|
@ -24,9 +24,18 @@ impl CastleRights {
|
|||
/// Panics if the index is out of bounds.
|
||||
#[inline(always)]
|
||||
pub fn from_index(index: usize) -> Self {
|
||||
assert!(index < Self::NUM_VARIANTS);
|
||||
// SAFETY: we know the value is in-bounds
|
||||
unsafe { Self::from_index_unchecked(index) }
|
||||
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
|
||||
Some(unsafe { Self::from_index_unchecked(index) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from a castle rights index into a [CastleRights] type, no bounds checking.
|
||||
|
|
|
@ -25,9 +25,18 @@ impl Color {
|
|||
/// Panics if the index is out of bounds.
|
||||
#[inline(always)]
|
||||
pub fn from_index(index: usize) -> Self {
|
||||
assert!(index < Self::NUM_VARIANTS);
|
||||
// SAFETY: we know the value is in-bounds
|
||||
unsafe { Self::from_index_unchecked(index) }
|
||||
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
|
||||
Some(unsafe { Self::from_index_unchecked(index) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from a color index into a [Color] type, no bounds checking.
|
||||
|
|
|
@ -41,9 +41,17 @@ impl File {
|
|||
/// Panics if the index is out of bounds.
|
||||
#[inline(always)]
|
||||
pub fn from_index(index: usize) -> Self {
|
||||
assert!(index < Self::NUM_VARIANTS);
|
||||
// SAFETY: we know the value is in-bounds
|
||||
unsafe { Self::from_index_unchecked(index) }
|
||||
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
|
||||
Some(unsafe { Self::from_index_unchecked(index) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from a file index into a [File] type, no bounds checking.
|
||||
|
|
|
@ -34,9 +34,18 @@ impl Piece {
|
|||
/// Panics if the index is out of bounds.
|
||||
#[inline(always)]
|
||||
pub fn from_index(index: usize) -> Self {
|
||||
assert!(index < Self::NUM_VARIANTS);
|
||||
// SAFETY: we know the value is in-bounds
|
||||
unsafe { Self::from_index_unchecked(index) }
|
||||
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
|
||||
Some(unsafe { Self::from_index_unchecked(index) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from a piece index into a [Piece] type, no bounds checking.
|
||||
|
|
|
@ -41,9 +41,17 @@ impl Rank {
|
|||
/// Panics if the index is out of bounds.
|
||||
#[inline(always)]
|
||||
pub fn from_index(index: usize) -> Self {
|
||||
assert!(index < Self::NUM_VARIANTS);
|
||||
// SAFETY: we know the value is in-bounds
|
||||
unsafe { Self::from_index_unchecked(index) }
|
||||
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
|
||||
Some(unsafe { Self::from_index_unchecked(index) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from a rank index into a [Rank] type, no bounds checking.
|
||||
|
|
|
@ -57,9 +57,18 @@ impl Square {
|
|||
/// Convert from a square index into a [Square] type.
|
||||
#[inline(always)]
|
||||
pub fn from_index(index: usize) -> Self {
|
||||
assert!(index < Self::NUM_VARIANTS);
|
||||
// SAFETY: we know the value is in-bounds
|
||||
unsafe { Self::from_index_unchecked(index) }
|
||||
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
|
||||
Some(unsafe { Self::from_index_unchecked(index) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from a square index into a [Square] type, no bounds checking.
|
||||
|
|
Loading…
Reference in a new issue