diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-10-08 19:24:16 -0400 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-10-08 19:37:16 -0400 |
| commit | fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a (patch) | |
| tree | 6fa99679e697828347af1e15d41cbd2ebb2106e3 /dungeon/src/pos.rs | |
| parent | add some functionality to Pos (diff) | |
| download | DungeonCrawl-fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a.tar.gz DungeonCrawl-fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a.tar.bz2 DungeonCrawl-fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a.zip | |
further improve const fns for Pos
Diffstat (limited to 'dungeon/src/pos.rs')
| -rw-r--r-- | dungeon/src/pos.rs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 1d4526b..8695d0b 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -15,6 +15,13 @@ macro_rules! downcast { }; } +#[macro_export] +macro_rules! const_pos { + ($name:ident, $x:expr, $y:expr) => { + const $name: Pos = Pos::new_unchecked($x, $y); + }; +} + /// The `Direction` type represents a direction an entity /// or any position object is facing inside the dungeon map. /// Since the dungeon lives on a grid, there are only four @@ -62,23 +69,23 @@ impl Pos { } } - /// Creates a new position from a given x and y position at compile time. + /// Creates a new position from a given x and y position. /// - /// Bounds checks are run at compile time so it is gurenteeed to return a valid `Pos`. + /// Bounds checks are asserted at runtime and will panic if out of bounds. /// /// # Examples /// /// ``` /// use dungeon::Pos; /// - /// let pos = Pos::new_const::<1,1>(); + /// let pos = Pos::new_unchecked(1, 1); /// assert_eq!(pos.xy(), (1,1)); /// ``` #[must_use] - pub const fn new_const<const X: u16, const Y: u16>() -> Self { - assert!(X < MAP_SIZE); - assert!(Y < MAP_SIZE); - Self(X, Y) + pub const fn new_unchecked(x: u16, y: u16) -> Self { + assert!(x < MAP_SIZE, "Positions must be smaller then MAP_SIZE"); + assert!(y < MAP_SIZE, "Positions must be smaller then MAP_SIZE"); + Self(x, y) } /// Returns the x and y positions of `Pos`. @@ -220,7 +227,7 @@ impl Default for Pos { /// ``` /// fn default() -> Self { - const DEFAULT: Pos = Pos::new_const::<0, 0>(); + const_pos!(DEFAULT, 0, 0); DEFAULT } } |