pub struct RoomCoordinate(/* private fields */);
Expand description
An X or Y coordinate in a room, restricted to the valid range of
coordinates. This restriction can be used in safety constraints, and is
enforced by all safe RoomCoordinate
constructors.
Implementations§
Source§impl RoomCoordinate
impl RoomCoordinate
pub const MAX: Self
pub const MIN: Self
Sourcepub const fn new(coord: u8) -> Result<Self, OutOfBoundsError>
pub const fn new(coord: u8) -> Result<Self, OutOfBoundsError>
Create a RoomCoordinate
from a u8
, returning an error if the
coordinate is not in the valid room size range
Sourcepub unsafe fn unchecked_new(coord: u8) -> Self
pub unsafe fn unchecked_new(coord: u8) -> Self
Create a RoomCoordinate
from a u8
, without checking whether it’s in
the range of valid values.
§Safety
Calling this method with coord >= ROOM_SIZE
can result in undefined
behaviour when the resulting RoomCoordinate
is used.
Sourcepub fn assume_bounds_constraint(self)
pub fn assume_bounds_constraint(self)
Provides a hint to the compiler that the contained u8
is smaller than
ROOM_SIZE
. Allows for better optimized safe code that uses this
property.
Sourcepub const fn is_room_edge(self) -> bool
pub const fn is_room_edge(self) -> bool
Get whether this coordinate represents an edge position (0 or 49)
Sourcepub fn checked_add(self, rhs: i8) -> Option<RoomCoordinate>
pub fn checked_add(self, rhs: i8) -> Option<RoomCoordinate>
Get the coordinate adjusted by a certain value, returning None
if the
result is outside the valid range.
Example usage:
use screeps::local::RoomCoordinate;
let zero = RoomCoordinate::new(0).unwrap();
let forty_nine = RoomCoordinate::new(49).unwrap();
assert_eq!(zero.checked_add(1), Some(RoomCoordinate::new(1).unwrap()));
assert_eq!(zero.checked_add(-1), None);
assert_eq!(zero.checked_add(49), Some(forty_nine));
assert_eq!(forty_nine.checked_add(1), None);
Sourcepub fn checked_add_offset(self, rhs: RoomOffset) -> Option<RoomCoordinate>
pub fn checked_add_offset(self, rhs: RoomOffset) -> Option<RoomCoordinate>
checked_add
that accepts a RoomOffset
.
Sourcepub fn saturating_add(self, rhs: i8) -> RoomCoordinate
pub fn saturating_add(self, rhs: i8) -> RoomCoordinate
Get the coordinate adjusted by a certain value, saturating at the edges of the room if the result would be outside of the valid range.
Example usage:
use screeps::local::RoomCoordinate;
let zero = RoomCoordinate::new(0).unwrap();
let forty_nine = RoomCoordinate::new(49).unwrap();
assert_eq!(zero.saturating_add(1), RoomCoordinate::new(1).unwrap());
assert_eq!(zero.saturating_add(-1), zero);
assert_eq!(zero.saturating_add(i8::MAX), forty_nine);
assert_eq!(forty_nine.saturating_add(1), forty_nine);
assert_eq!(forty_nine.saturating_add(i8::MIN), zero);
Sourcepub fn saturating_add_offset(self, rhs: RoomOffset) -> Self
pub fn saturating_add_offset(self, rhs: RoomOffset) -> Self
saturating_add
that accepts a RoomOffset
.
Sourcepub fn overflowing_add(self, rhs: i8) -> (RoomCoordinate, bool)
pub fn overflowing_add(self, rhs: i8) -> (RoomCoordinate, bool)
Get the coordinate adjusted by a certain value, wrapping around ta the
edges of the room if the result would be outside of the valid range.
Returns a bool
indicating whether there was wrapping.
Can be used to e.g. implement addition for
Position
s.
Example usage:
use screeps::local::RoomCoordinate;
assert_eq!(
RoomCoordinate::MIN.overflowing_add(1),
(RoomCoordinate::new(1).unwrap(), false)
);
assert_eq!(
RoomCoordinate::MIN.overflowing_add(-1),
(RoomCoordinate::MAX, true)
);
assert_eq!(
RoomCoordinate::MAX.overflowing_add(1),
(RoomCoordinate::MIN, true)
);
Sourcepub fn overflowing_add_offset(self, rhs: RoomOffset) -> (RoomCoordinate, bool)
pub fn overflowing_add_offset(self, rhs: RoomOffset) -> (RoomCoordinate, bool)
overflowing_add
that accepts a
RoomOffset
.
Sourcepub fn wrapping_add(self, rhs: i8) -> Self
pub fn wrapping_add(self, rhs: i8) -> Self
Get the coordinate adjusted by a certain value, wrapping around ta the edges of the room if the result would be outside of the valid range.
Example usage:
use screeps::local::RoomCoordinate;
assert_eq!(
RoomCoordinate::MIN.wrapping_add(1),
RoomCoordinate::new(1).unwrap()
);
assert_eq!(RoomCoordinate::MIN.wrapping_add(-1), RoomCoordinate::MAX);
assert_eq!(RoomCoordinate::MAX.wrapping_add(1), RoomCoordinate::MIN);
Sourcepub fn wrapping_add_offset(self, rhs: RoomOffset) -> Self
pub fn wrapping_add_offset(self, rhs: RoomOffset) -> Self
wrapping_add
that accepts a RoomOffset
.
Sourcepub unsafe fn unchecked_add(self, rhs: i8) -> Self
pub unsafe fn unchecked_add(self, rhs: i8) -> Self
Get the coordinate adjusted by a certain value.
§Safety
After adding rhs to the integer coordinate of self, the result must lie
within [0, ROOM_SIZE)
.
Sourcepub unsafe fn unchecked_add_offset(self, rhs: RoomOffset) -> Self
pub unsafe fn unchecked_add_offset(self, rhs: RoomOffset) -> Self
unchecked_add
that accepts a RoomOffset
.
§Safety
The result of adding the integer coordinate of self and the integer
offset in rhs
must lie within [0, ROOM_SIZE)
.
Trait Implementations§
Source§impl AsRef<u8> for RoomCoordinate
impl AsRef<u8> for RoomCoordinate
Source§impl Clone for RoomCoordinate
impl Clone for RoomCoordinate
Source§fn clone(&self) -> RoomCoordinate
fn clone(&self) -> RoomCoordinate
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more