[board] Implement some TryInto traits for Square for u8, u16, u32, and u64

This commit is contained in:
Eryn Wells 2024-01-17 08:35:21 -08:00
parent 3a15fca10a
commit f27d22d57f

View file

@ -237,6 +237,23 @@ impl FromStr for Square {
} }
} }
macro_rules! try_from_integer {
($int_type:ident) => {
impl TryFrom<$int_type> for Square {
type Error = SquareOutOfBoundsError;
fn try_from(value: $int_type) -> Result<Self, Self::Error> {
Square::try_index(value as usize).ok_or(SquareOutOfBoundsError)
}
}
};
}
try_from_integer!(u8);
try_from_integer!(u16);
try_from_integer!(u32);
try_from_integer!(u64);
impl fmt::Display for Square { impl fmt::Display for Square {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(