summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-11 19:58:01 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-11 19:58:01 -0500
commit284facad24cc64fa00d5b7d5f1cec8d1732183fa (patch)
tree13d083d6ac0d7870603e1e1a5c5f59f7e4a79285
parentgraphics: load_texture! can be simplified (diff)
downloadDungeonCrawl-284facad24cc64fa00d5b7d5f1cec8d1732183fa.tar.gz
DungeonCrawl-284facad24cc64fa00d5b7d5f1cec8d1732183fa.tar.bz2
DungeonCrawl-284facad24cc64fa00d5b7d5f1cec8d1732183fa.zip
dungeon: downcast! -> try_from!
-rw-r--r--dungeon/src/pos.rs8
1 files 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<Self> {
- 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,