Skip to content

Commit

Permalink
const RoomCoordinate::new() (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
smessmer authored Oct 25, 2021
1 parent 0a4a202 commit 1c5a3c6
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/local/room_coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ pub fn linear_index_to_xy(idx: usize) -> RoomXY {
pub struct RoomCoordinate(u8);

impl RoomCoordinate {
#[inline]
pub const fn new(coord: u8) -> Result<Self, OutOfBoundsError> {
if coord < ROOM_SIZE {
Ok(RoomCoordinate(coord))
} else {
Err(OutOfBoundsError(coord))
}
}

// # Safety
// Calling this method with `coord >= ROOM_SIZE` can result in undefined behaviour when the
// resulting `RoomCoordinate` is used.
Expand Down Expand Up @@ -90,11 +99,7 @@ impl TryFrom<u8> for RoomCoordinate {
type Error = OutOfBoundsError;

fn try_from(coord: u8) -> Result<Self, Self::Error> {
if coord < ROOM_SIZE {
Ok(RoomCoordinate(coord))
} else {
Err(OutOfBoundsError(coord))
}
RoomCoordinate::new(coord)
}
}

Expand Down

0 comments on commit 1c5a3c6

Please sign in to comment.