Pos

Struct Pos 

Source
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::FIRST and Pos::LAST; Pos::TOP_LEFT, etc.; Pos::CENTER.
  • Using Pos::new with X and Y coordinates and handling the Result; can also be used in const contexts.
    let pos = Pos::new(3, 3)?;
  • Using try_from with a (u16, u16) tuple or a tuple reference. It’s equivalent to Pos::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, as unwrap is not a const fn method.
    const pos : Pos = Pos::new_unwrap(3, 3);
  • Using Pos::new_static to create an instance at compile time, which is also when the validity of the coordinates is checked.
    const POS : Pos = Pos::new_static::<3, 3>();
    The following, for instance, doesn’t compile:
    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>

Source

pub const XMAX: u16 = XMAX

Max x value

Source

pub const YMAX: u16 = YMAX

Max y value

Source

pub const WIDTH: u16

Width of the grid: exclusive max of the x coordinate.

Source

pub const HEIGHT: u16

Height of the grid: exclusive max of the y coordinate.

Source

pub const SIZE: usize

Size of the grid, i.e. how many squares.

Source

pub const FIRST: Pos<XMAX, YMAX>

Coordinates of the first element of the grid: (0, 0). Also known as origin.

Source

pub const LAST: Pos<XMAX, YMAX>

Coordinates of the last element of the grid.

Source

pub const CENTER: Pos<XMAX, YMAX>

Center the (approximate) center coordinate.

Source

pub const TOP_LEFT: Pos<XMAX, YMAX> = Self::FIRST

Coordinates of the top-left coordinate.

Source

pub const TOP_RIGHT: Pos<XMAX, YMAX>

Coordinates of the top-right coordinate.

Source

pub const BOTTOM_LEFT: Pos<XMAX, YMAX>

Coordinates of the bottom-left coordinate.

Source

pub const BOTTOM_RIGHT: Pos<XMAX, YMAX> = Self::LAST

Coordinates of the bottom-right coordinate.

Source

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.

Source

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.

Source

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>();
Source

pub const fn x(&self) -> u16

Returns the x coordinate

Source

pub const fn y(&self) -> u16

Returns the y coordinate

Source

pub const fn tuple(&self) -> (u16, u16)

Return the corresponding (u16, u16) tuple.

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>>,

Source§

type Output = Result<Pos<XMAX, YMAX>, Error>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Dir) -> Self::Output

Performs the + operation. Read more
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>>,

Source§

type Output = Result<Pos<XMAX, YMAX>, Error>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Dir) -> Self::Output

Performs the + operation. Read more
Source§

impl<const XMAX: u16, const YMAX: u16> Clone for Pos<XMAX, YMAX>

Source§

fn clone(&self) -> Pos<XMAX, YMAX>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const XMAX: u16, const YMAX: u16> Debug for Pos<XMAX, YMAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const XMAX: u16, const YMAX: u16> Default for Pos<XMAX, YMAX>

Source§

fn default() -> Pos<XMAX, YMAX>

Returns the “default value” for a type. Read more
Source§

impl<const XMAX: u16, const YMAX: u16> Display for Pos<XMAX, YMAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Converts to this type from the input type.
Source§

impl<const XMAX: u16, const YMAX: u16> From<&Pos<XMAX, YMAX>> for (i32, i32)

Source§

fn from(pos: &Pos<XMAX, YMAX>) -> Self

Converts to this type from the input type.
Source§

impl<const XMAX: u16, const YMAX: u16> From<&Pos<XMAX, YMAX>> for (u16, u16)

Source§

fn from(pos: &Pos<XMAX, YMAX>) -> Self

Converts to this type from the input type.
Source§

impl<const XMAX: u16, const YMAX: u16> From<&Pos<XMAX, YMAX>> for usize

Source§

fn from(pos: &Pos<XMAX, YMAX>) -> Self

Converts to this type from the input type.
Source§

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

Converts to this type from the input type.
Source§

impl<const XMAX: u16, const YMAX: u16> From<Pos<XMAX, YMAX>> for (i32, i32)

Source§

fn from(pos: Pos<XMAX, YMAX>) -> Self

Converts to this type from the input type.
Source§

impl<const XMAX: u16, const YMAX: u16> From<Pos<XMAX, YMAX>> for (u16, u16)

Source§

fn from(pos: Pos<XMAX, YMAX>) -> Self

Converts to this type from the input type.
Source§

impl<const XMAX: u16, const YMAX: u16> From<Pos<XMAX, YMAX>> for usize

Source§

fn from(pos: Pos<XMAX, YMAX>) -> Self

Converts to this type from the input type.
Source§

impl<const XMAX: u16, const YMAX: u16> Hash for Pos<XMAX, YMAX>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const XMAX: u16, const YMAX: u16> Ord for Pos<XMAX, YMAX>

Source§

fn cmp(&self, other: &Pos<XMAX, YMAX>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const XMAX: u16, const YMAX: u16> PartialEq for Pos<XMAX, YMAX>

Source§

fn eq(&self, other: &Pos<XMAX, YMAX>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const XMAX: u16, const YMAX: u16> PartialOrd for Pos<XMAX, YMAX>

Source§

fn partial_cmp(&self, other: &Pos<XMAX, YMAX>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const XMAX: u16, const YMAX: u16> PosT for Pos<XMAX, YMAX>

Source§

const WIDTH: usize

Width
Source§

const HEIGHT: usize

Height
Source§

type Xtype = BoundedU16<0, XMAX>

The type of the X coordinate
Source§

type Ytype = BoundedU16<0, YMAX>

The type of the Y coordinate
Source§

fn new_(xy: (Self::Xtype, Self::Ytype)) -> Self

Internal new_ that creates the Pos type from the provided tuple.
Source§

fn into_tuple(self) -> (Self::Xtype, Self::Ytype)

Return the corresponding tuple Read more
Source§

fn tuple(&self) -> (Self::Xtype, Self::Ytype)

Return the corresponding tuple Read more
Source§

fn x(&self) -> Self::Xtype

Get the X component Read more
Source§

fn y(&self) -> Self::Ytype

Get the Y component Read more
Source§

fn new<X, Y>(x: X, y: Y) -> Result<Self, Error>
where X: BoundedInt, Y: BoundedInt, Self::Xtype: TryFrom<X>, Self::Ytype: TryFrom<Y>, Self: Sized,

Create a new Pos with the given parameters
Source§

fn inner_tuple( self, ) -> (<Self::Xtype as BoundedInt>::Inner, <Self::Ytype as BoundedInt>::Inner)

Return the corresponding inner tuple
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,

Create a position from another position
Source§

fn width() -> usize

Return the width (x) supported by the position type
Source§

fn height() -> usize

Return the height (y) supported by the position type
Source§

fn dimensions() -> usize

Return the total dimension supported by the position type
Source§

fn first() -> Self
where Self: Sized,

First coordinate, top left, origin
Source§

fn last() -> Self
where Self: Sized,

Last coordinate, bottom right
Source§

fn is_corner(&self) -> bool

Return true if self is a corner of the grid.
Source§

fn is_side(&self) -> bool

Return true if self is on the side of the grid.
Source§

fn flip_h(&self) -> Self
where Self: Sized,

Flip the coordinate vertically
Source§

fn flip_v(&self) -> Self
where Self: Sized,

Flip the coordinate horizontally
Source§

fn manhattan(&self, pos: &Self) -> usize

Return the manhattan distance
Source§

fn inside(&self, pos1: &Self, pos2: &Self) -> bool

Check that the position is inside the provided limits
Source§

fn to_usize(&self) -> usize

Return a usize index corresponding to the position.
Source§

fn tryfrom_usize(i: usize) -> Result<Self, Error>
where Self: Sized,

Create a new position from the provided usize, if possible; return an error otherwise.
Source§

fn next(&self) -> Option<Self>
where Self: Sized,

Return the next position horizontally (English read sequence), or None if self is the last one.
Source§

fn next_y(&self) -> Option<Self>
where Self: Sized,

Return the next position vertically, or None if self is the last one.
Source§

fn prev(&self) -> Option<Self>
where Self: Sized,

Return the previous position horizontally (English read sequence), or None if self is the first one.
Source§

fn prev_y(&self) -> Option<Self>
where Self: Sized,

Return the previous position vertically, or None if self is the first one.
Source§

fn iter_x() -> impl Iterator<Item = Self::Xtype>

Returns an iterator over valid X values
Source§

fn iter_y() -> impl Iterator<Item = Self::Ytype>

Returns an iterator over valid Y values
Source§

fn iter_orientation<const XFIRST: bool>() -> PosTIter<XFIRST, Self>
where Self: Sized,

Return an iterator that returns all positions within the grid dimensions in the given orientation - true for horizontally, false for vertically.
Source§

fn iter() -> PosTIter<true, Self>
where Self: Sized,

Return an iterator that returns all positions within the grid dimensions.
Source§

fn iter_horizontal() -> PosTIter<true, Self>
where Self: Sized,

Return an iterator that returns all positions within the grid dimensions horizontally.
Source§

fn iter_vertical() -> PosTIter<false, Self>
where Self: Sized,

Return an iterator that returns all positions within the grid dimensions vertically.
Source§

fn iter_range(topleft: Self, botright: Self) -> PosTIterRange<Self>
where Self: Sized + Copy,

Return an iterator that returns all positions within the grid coordinates.
Source§

fn iter_in_x(x: Self::Xtype) -> PosTIterInX<Self>
where Self: Sized,

Return an iterator that returns all positions in a column.
Source§

fn iter_in_y(y: Self::Ytype) -> PosTIterInY<Self>
where Self: Sized,

Return an iterator that returns all positions in a line.
Source§

fn tlbr_of(iter: impl Iterator<Item = Self>) -> Result<(Self, Self), Error>
where Self: Sized,

Calculate a top-left and a bottom-right Pos’s that contains all iterated points.
Source§

fn rotate_cw(&self) -> Self
where <<Self as PosT>::Ytype as BoundedInt>::Inner: Sub<Output = <<Self as PosT>::Ytype as BoundedInt>::Inner>, Self::Xtype: TryFrom<<<Self as PosT>::Ytype as BoundedInt>::Inner>, Self::Ytype: TryFrom<<<Self as PosT>::Xtype as BoundedInt>::Inner>,

Rotate the square grid coordinate 90 degrees clockwise
Source§

fn rotate_cc(&self) -> Self
where <<Self as PosT>::Xtype as BoundedInt>::Inner: Sub<Output = <<Self as PosT>::Xtype as BoundedInt>::Inner>, Self::Xtype: TryFrom<<<Self as PosT>::Ytype as BoundedInt>::Inner>, Self::Ytype: TryFrom<<<Self as PosT>::Xtype as BoundedInt>::Inner>,

Rotate the square grid coordinate 90 degrees counter-clockwise
Source§

impl<const XMAX: u16, const YMAX: u16> TryFrom<&(i32, i32)> for Pos<XMAX, YMAX>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xy: &(i32, i32)) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const XMAX: u16, const YMAX: u16> TryFrom<&(u16, u16)> for Pos<XMAX, YMAX>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xy: &(u16, u16)) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const XMAX: u16, const YMAX: u16> TryFrom<(i32, i32)> for Pos<XMAX, YMAX>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xy: (i32, i32)) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const XMAX: u16, const YMAX: u16> TryFrom<(u16, u16)> for Pos<XMAX, YMAX>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(xy: (u16, u16)) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const XMAX: u16, const YMAX: u16> TryFrom<usize> for Pos<XMAX, YMAX>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(i: usize) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const XMAX: u16, const YMAX: u16> Copy for Pos<XMAX, YMAX>

Source§

impl<const XMAX: u16, const YMAX: u16> Eq for Pos<XMAX, YMAX>

Source§

impl<const XMAX: u16, const YMAX: u16> StructuralPartialEq for Pos<XMAX, YMAX>

Auto Trait Implementations§

§

impl<const XMAX: u16, const YMAX: u16> Freeze for Pos<XMAX, YMAX>

§

impl<const XMAX: u16, const YMAX: u16> RefUnwindSafe for Pos<XMAX, YMAX>

§

impl<const XMAX: u16, const YMAX: u16> Send for Pos<XMAX, YMAX>

§

impl<const XMAX: u16, const YMAX: u16> Sync for Pos<XMAX, YMAX>

§

impl<const XMAX: u16, const YMAX: u16> Unpin for Pos<XMAX, YMAX>

§

impl<const XMAX: u16, const YMAX: u16> UnwindSafe for Pos<XMAX, YMAX>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.