summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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,