summaryrefslogtreecommitdiff
path: root/dungeon/src/pos.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dungeon/src/pos.rs')
-rw-r--r--dungeon/src/pos.rs22
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)
}