diff options
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 } } |