Skip to content

Commit

Permalink
fix: Fix wrapping arithmetic for overflowing_add.
Browse files Browse the repository at this point in the history
  • Loading branch information
khoover committed Oct 29, 2024
1 parent 394a303 commit 2f10f6f
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/local/room_coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,14 @@ impl RoomOffset {
/// );
/// ```
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
const RANGE_WIDTH: i8 = 2 * ROOM_SIZE_I8 - 1;
self.assume_bounds_constraint();
rhs.assume_bounds_constraint();
let raw = self.0 + rhs.0;
if raw <= -ROOM_SIZE_I8 {
(Self::new(raw + ROOM_SIZE_I8).unwrap_throw(), true)
(Self::new(raw + RANGE_WIDTH).unwrap_throw(), true)
} else if raw >= ROOM_SIZE_I8 {
(Self::new(raw - ROOM_SIZE_I8).unwrap_throw(), true)
(Self::new(raw - RANGE_WIDTH).unwrap_throw(), true)
} else {
(Self::new(raw).unwrap_throw(), false)
}
Expand Down

0 comments on commit 2f10f6f

Please sign in to comment.