From 284facad24cc64fa00d5b7d5f1cec8d1732183fa Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 11 Nov 2025 19:58:01 -0500 Subject: dungeon: downcast! -> try_from! --- dungeon/src/pos.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 47732a0..b7a977c 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -17,7 +17,9 @@ use std::{ use crate::{MAP_SIZE_USIZE, map::MAP_SIZE}; -macro_rules! downcast { +/// This allows us have a u16::try_from(usize) +/// like expr in a const context +macro_rules! try_from { ($usize:expr, $type:ty) => { if $usize > <$type>::MAX as usize { None @@ -233,8 +235,8 @@ impl Pos { /// ``` #[must_use] pub const fn from_idx(idx: usize) -> Option { - let x = downcast!(idx % MAP_SIZE_USIZE, u16); - let y = downcast!(idx / MAP_SIZE_USIZE, u16); + let x = try_from!(idx % MAP_SIZE_USIZE, u16); + let y = try_from!(idx / MAP_SIZE_USIZE, u16); match (x, y) { (Some(a), Some(b)) => Self::new(a, b), _ => None, -- cgit v1.2.3-freya