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 | |
| 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')
| -rw-r--r-- | dungeon/src/map.rs | 36 | ||||
| -rw-r--r-- | dungeon/src/pos.rs | 22 |
2 files changed, 19 insertions, 39 deletions
diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index ade485e..fef0750 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -212,41 +212,8 @@ impl Floor { *hash } - /// Display the floor as a string for debugging - /// - /// # Examples - /// ```no_run - /// use dungeon::Floor; - /// let floor = Floor::generate(); - /// println!("{}", floor.display()); - /// ``` - #[must_use] - pub fn display(&self) -> String { - let mut output = String::new(); - for pos in Pos::values() { - // If it's the player start, show 'P' - if self.player_start == pos { - output.push('P'); - continue; - } - // Otherwise, show the tile character - let tile = self.get(pos); - let char = match tile { - Tile::Wall => '#', - Tile::Room => '.', - Tile::Hallway => ',', - Tile::Stairs => '>', - }; - output.push(char); - // Newline at the end of each row - if pos.xy().0 == MAP_SIZE - 1 { - output.push('\n'); - } - } - output - } - /// Returns a random open (no wall) position + #[must_use] pub fn random_pos(&mut self) -> Pos { loop { let pos = self.rand().random(); @@ -258,6 +225,7 @@ impl Floor { } /// Returns the random number gen for the `Floor` + #[must_use] pub fn rand(&mut self) -> &mut impl Rng { &mut self.rng } 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) } |