pub struct RoomOffset(/* private fields */);
Expand description
Implementations§
Source§impl RoomOffset
impl RoomOffset
pub const MAX: Self
pub const MIN: Self
Sourcepub const fn new(offset: i8) -> Result<Self, OffsetOutOfBoundsError>
pub const fn new(offset: i8) -> Result<Self, OffsetOutOfBoundsError>
Create a RoomOffset
from an i8
, returning an error if it’s not
within the valid range.
Sourcepub unsafe fn unchecked_new(offset: i8) -> Self
pub unsafe fn unchecked_new(offset: i8) -> Self
Create a RoomOffset
from an i8
, without checking whether it’s in the
range of valid values.
§Safety
Calling this method with offset.abs() >= ROOM_SIZE_I8
can result in
undefined behaviour when the resulting RoomOffset
is used.
Sourcepub fn assume_bounds_constraint(self)
pub fn assume_bounds_constraint(self)
Provides a hint to the compiler that the contained i8
is within
(-ROOM_SIZE_I8, ROOM_SIZE_I8)
. Allows for better optimized safe code
that uses this property.
Sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Add two offsets together, returning None
if the result would be
outside the valid range.
Example usage:
use screeps::local::RoomOffset;
let zero = RoomOffset::new(0).unwrap();
let one = RoomOffset::new(1).unwrap();
assert_eq!(RoomOffset::MIN.checked_add(RoomOffset::MAX), Some(zero));
assert_eq!(RoomOffset::MAX.checked_add(one), None);
assert_eq!(RoomOffset::MIN.checked_add(-one), None);
Sourcepub fn saturating_add(self, rhs: Self) -> Self
pub fn saturating_add(self, rhs: Self) -> Self
Add two offsets together, saturating at the boundaries of the valid range if the result would be outside.
Example usage:
use screeps::local::RoomOffset;
let zero = RoomOffset::new(0).unwrap();
let one = RoomOffset::new(1).unwrap();
assert_eq!(RoomOffset::MIN.saturating_add(RoomOffset::MAX), zero);
assert_eq!(RoomOffset::MAX.saturating_add(one), RoomOffset::MAX);
assert_eq!(RoomOffset::MIN.saturating_add(-one), RoomOffset::MIN);
Sourcepub fn overflowing_add(self, rhs: Self) -> (Self, bool)
pub fn overflowing_add(self, rhs: Self) -> (Self, bool)
Add two offsets together, wrapping around at the ends of the valid
range. Returns a bool
indicating whether there was wrapping.
Example usage:
use screeps::local::RoomOffset;
let zero = RoomOffset::new(0).unwrap();
let one = RoomOffset::new(1).unwrap();
assert_eq!(
RoomOffset::MAX.overflowing_add(one),
(RoomOffset::MIN, true)
);
assert_eq!(
RoomOffset::MIN.overflowing_add(-one),
(RoomOffset::MAX, true)
);
assert_eq!(
RoomOffset::MIN.overflowing_add(RoomOffset::MAX),
(zero, false)
);
Sourcepub fn wrapping_add(self, rhs: Self) -> Self
pub fn wrapping_add(self, rhs: Self) -> Self
Add two offsets together, wrapping around at the ends of the valid range.
Example usage:
use screeps::local::RoomOffset;
let zero = RoomOffset::new(0).unwrap();
let one = RoomOffset::new(1).unwrap();
assert_eq!(RoomOffset::MAX.wrapping_add(one), RoomOffset::MIN);
assert_eq!(RoomOffset::MIN.wrapping_add(-one), RoomOffset::MAX);
assert_eq!(RoomOffset::MIN.wrapping_add(RoomOffset::MAX), zero);
Sourcepub unsafe fn unchecked_add(self, rhs: Self) -> Self
pub unsafe fn unchecked_add(self, rhs: Self) -> Self
Add two offsets together, without checking that the result is in the valid range.
§Safety
The result of adding the two offsets as integers must lie within
(-ROOM_SIZE_I8, ROOM_SIZE_I8)
.
Trait Implementations§
Source§impl AsRef<i8> for RoomOffset
impl AsRef<i8> for RoomOffset
Source§impl Clone for RoomOffset
impl Clone for RoomOffset
Source§fn clone(&self) -> RoomOffset
fn clone(&self) -> RoomOffset
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more