diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-11-10 13:46:00 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-11-10 13:46:00 -0500 |
| commit | 038c5c2ee81b6ad1b03b437540c0035a8ab1b673 (patch) | |
| tree | 3331cef18bb4b3cef90073d0b019b4312f5d1ce4 /dungeon/src/pos.rs | |
| parent | graphics: have some window arguments passed in through cmd args, fix camera pos (diff) | |
| download | DungeonCrawl-038c5c2ee81b6ad1b03b437540c0035a8ab1b673.tar.gz DungeonCrawl-038c5c2ee81b6ad1b03b437540c0035a8ab1b673.tar.bz2 DungeonCrawl-038c5c2ee81b6ad1b03b437540c0035a8ab1b673.zip | |
dungeon: new_unchecked should be unsafe
Diffstat (limited to 'dungeon/src/pos.rs')
| -rw-r--r-- | dungeon/src/pos.rs | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 0cad387..8d84d3f 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -31,7 +31,15 @@ macro_rules! downcast { #[macro_export] macro_rules! const_pos { ($x:expr, $y:expr) => {{ - const CONST_POS: Pos = Pos::new_unchecked($x, $y); + assert!( + $x < $crate::map::MAP_SIZE, + "Positions must be smaller then MAP_SIZE" + ); + assert!( + $y < $crate::map::MAP_SIZE, + "Positions must be smaller then MAP_SIZE" + ); + const CONST_POS: Pos = unsafe { Pos::new_unchecked($x, $y) }; CONST_POS }}; } @@ -124,13 +132,17 @@ impl Pos { /// ``` /// use dungeon::Pos; /// - /// let pos = Pos::new_unchecked(1, 1); + /// let pos = unsafe { Pos::new_unchecked(1, 1) }; /// assert_eq!(pos.xy(), (1,1)); /// ``` + /// + /// # Safety + /// + /// Library code and crates that use it expect the `Pos` x and y positions + /// to be within a gurenteed bound. When they are not this can cause + /// undefined behaviour, or crashes. #[must_use] - 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"); + pub const unsafe fn new_unchecked(x: u16, y: u16) -> Self { Self(x, y) } |