From f27d22d57f84d41fa378a7738a4ed3382e7583c4 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 17 Jan 2024 08:35:21 -0800 Subject: [PATCH] [board] Implement some TryInto traits for Square for u8, u16, u32, and u64 --- board/src/square.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/board/src/square.rs b/board/src/square.rs index d380b29..0ad9d09 100644 --- a/board/src/square.rs +++ b/board/src/square.rs @@ -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 { + 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 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(