pub struct Pos<const XMAX: u16, const YMAX: u16>(pub (BoundedU16<0, XMAX>, BoundedU16<0, YMAX>));Expand description
Square grid absolute coordinate
This generic type receives the dimensions of the square grid as const generic parameters, and prevents the cration of instances outside the grid.
Recommended usage is through a type alias; for instance, to create a 4x4 grid coordinate type:
type Pos = sqrid::Pos<4, 4>;We can only generate Pos instances that are valid - i.e. inside
the grid. Some of the ways to create instances:
- Using one of the const associated items:
Pos::FIRSTandPos::LAST;Pos::TOP_LEFT, etc.;Pos::CENTER. - Using
Pos::newwith X and Y coordinates and handling theResult; can also be used in const contexts.let pos = Pos::new(3, 3)?; - Using
try_fromwith a(u16, u16)tuple or a tuple reference. It’s equivalent toPos::new:use std::convert::{TryFrom, TryInto}; let pos1 = Pos::try_from((3, 3))?; let pos2 : Pos = (3_u16, 3_u16).try_into()?; - Using
Pos::new_unwrap, be be aware that it panics if the coordinates are not valid. This is convenient in const contexts, asunwrapis not a const fn method.const pos : Pos = Pos::new_unwrap(3, 3); - Using
Pos::new_staticto create an instance at compile time, which is also when the validity of the coordinates is checked.The following, for instance, doesn’t compile:const POS : Pos = Pos::new_static::<3, 3>();ⓘconst POS : Pos = Pos::new_static::<3, 30>();
Tuple Fields§
§0: (BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)Implementations§
Source§impl<const XMAX: u16, const YMAX: u16> Pos<XMAX, YMAX>
impl<const XMAX: u16, const YMAX: u16> Pos<XMAX, YMAX>
Sourcepub const FIRST: Pos<XMAX, YMAX>
pub const FIRST: Pos<XMAX, YMAX>
Coordinates of the first element of the grid: (0, 0).
Also known as origin.
Sourcepub const BOTTOM_LEFT: Pos<XMAX, YMAX>
pub const BOTTOM_LEFT: Pos<XMAX, YMAX>
Coordinates of the bottom-left coordinate.
Sourcepub const BOTTOM_RIGHT: Pos<XMAX, YMAX> = Self::LAST
pub const BOTTOM_RIGHT: Pos<XMAX, YMAX> = Self::LAST
Coordinates of the bottom-right coordinate.
Sourcepub const fn new(x: u16, y: u16) -> Result<Self, Error>
pub const fn new(x: u16, y: u16) -> Result<Self, Error>
Create a new Pos instance; returns error if a coordinate is
out-of-bounds.
Sourcepub const fn new_unwrap(x: u16, y: u16) -> Self
pub const fn new_unwrap(x: u16, y: u16) -> Self
Create a new Pos instance, supports being called in const
context; panics if a coordinate is out-of-bounds.
Sourcepub const fn new_static<const X: u16, const Y: u16>() -> Self
pub const fn new_static<const X: u16, const Y: u16>() -> Self
Create a new Pos instance at compile time.
Checks arguments at compile time - for instance, the following doesn’t compile:
const POS : sqrid::Pos<5,5> = sqrid::Pos::<5,5>::new_static::<9,9>();Trait Implementations§
Source§impl<const XMAX: u16, const YMAX: u16> Add<Dir> for &Pos<XMAX, YMAX>where
(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>): Add<Dir, Output = Result<(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>), Error>>,
impl<const XMAX: u16, const YMAX: u16> Add<Dir> for &Pos<XMAX, YMAX>where
(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>): Add<Dir, Output = Result<(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>), Error>>,
Source§impl<const XMAX: u16, const YMAX: u16> Add<Dir> for Pos<XMAX, YMAX>where
(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>): Add<Dir, Output = Result<(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>), Error>>,
impl<const XMAX: u16, const YMAX: u16> Add<Dir> for Pos<XMAX, YMAX>where
(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>): Add<Dir, Output = Result<(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>), Error>>,
Source§impl<const XMAX: u16, const YMAX: u16> From<&(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)> for Pos<XMAX, YMAX>
impl<const XMAX: u16, const YMAX: u16> From<&(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)> for Pos<XMAX, YMAX>
Source§fn from(xy: &(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)) -> Self
fn from(xy: &(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)) -> Self
Source§impl<const XMAX: u16, const YMAX: u16> From<(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)> for Pos<XMAX, YMAX>
impl<const XMAX: u16, const YMAX: u16> From<(BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)> for Pos<XMAX, YMAX>
Source§fn from(xy: (BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)) -> Self
fn from(xy: (BoundedU16<0, XMAX>, BoundedU16<0, YMAX>)) -> Self
Source§impl<const XMAX: u16, const YMAX: u16> Ord for Pos<XMAX, YMAX>
impl<const XMAX: u16, const YMAX: u16> Ord for Pos<XMAX, YMAX>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const XMAX: u16, const YMAX: u16> PartialOrd for Pos<XMAX, YMAX>
impl<const XMAX: u16, const YMAX: u16> PartialOrd for Pos<XMAX, YMAX>
Source§impl<const XMAX: u16, const YMAX: u16> PosT for Pos<XMAX, YMAX>
impl<const XMAX: u16, const YMAX: u16> PosT for Pos<XMAX, YMAX>
Source§type Xtype = BoundedU16<0, XMAX>
type Xtype = BoundedU16<0, XMAX>
Source§type Ytype = BoundedU16<0, YMAX>
type Ytype = BoundedU16<0, YMAX>
Source§fn new_(xy: (Self::Xtype, Self::Ytype)) -> Self
fn new_(xy: (Self::Xtype, Self::Ytype)) -> Self
new_ that creates the Pos type from the provided tuple.Source§fn inner_tuple(
self,
) -> (<Self::Xtype as BoundedInt>::Inner, <Self::Ytype as BoundedInt>::Inner)
fn inner_tuple( self, ) -> (<Self::Xtype as BoundedInt>::Inner, <Self::Ytype as BoundedInt>::Inner)
Source§fn tryfrom_pos<P>(pos: P) -> Result<Self, Error>where
P: PosT,
Self::Xtype: TryFrom<<P::Xtype as BoundedInt>::Inner>,
Self::Ytype: TryFrom<<P::Ytype as BoundedInt>::Inner>,
<P::Xtype as BoundedInt>::Inner: BoundedInt,
<P::Ytype as BoundedInt>::Inner: BoundedInt,
Self: Sized,
fn tryfrom_pos<P>(pos: P) -> Result<Self, Error>where
P: PosT,
Self::Xtype: TryFrom<<P::Xtype as BoundedInt>::Inner>,
Self::Ytype: TryFrom<<P::Ytype as BoundedInt>::Inner>,
<P::Xtype as BoundedInt>::Inner: BoundedInt,
<P::Ytype as BoundedInt>::Inner: BoundedInt,
Self: Sized,
Source§fn dimensions() -> usize
fn dimensions() -> usize
Source§fn inside(&self, pos1: &Self, pos2: &Self) -> bool
fn inside(&self, pos1: &Self, pos2: &Self) -> bool
Source§fn tryfrom_usize(i: usize) -> Result<Self, Error>where
Self: Sized,
fn tryfrom_usize(i: usize) -> Result<Self, Error>where
Self: Sized,
usize, if possible;
return an error otherwise.Source§fn next(&self) -> Option<Self>where
Self: Sized,
fn next(&self) -> Option<Self>where
Self: Sized,
self is the last one.Source§fn next_y(&self) -> Option<Self>where
Self: Sized,
fn next_y(&self) -> Option<Self>where
Self: Sized,
self is the last one.Source§fn prev(&self) -> Option<Self>where
Self: Sized,
fn prev(&self) -> Option<Self>where
Self: Sized,
self is the first one.Source§fn prev_y(&self) -> Option<Self>where
Self: Sized,
fn prev_y(&self) -> Option<Self>where
Self: Sized,
self is the first one.Source§fn iter_orientation<const XFIRST: bool>() -> PosTIter<XFIRST, Self> ⓘwhere
Self: Sized,
fn iter_orientation<const XFIRST: bool>() -> PosTIter<XFIRST, Self> ⓘwhere
Self: Sized,
true for horizontally,
false for vertically.