Struct sqrid::Qa

source ·
pub struct Qa<const WIDTH: u16, const HEIGHT: u16> { /* private fields */ }
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 Qa = sqrid::Qa<4, 4>;

We can only generate Qa instances that are valid - i.e. inside the grid. Some of the ways to create instances:

  • Using one of the const associated items: Qa::FIRST and Qa::LAST; Qa::TOP_LEFT, etc.; Qa::CENTER.
  • Using Qa::new with X and Y coordinates and handling the Result; can also be used in const contexts.
    let qa = Qa::new(3, 3)?;
  • Using try_from with a (u16, u16) tuple or a tuple reference. It’s equivalent to Qa::new:
    use std::convert::{TryFrom, TryInto};
    let qa1 = Qa::try_from((3, 3))?;
    let qa2 : Qa = (3_u16, 3_u16).try_into()?;
  • Using Qa::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 qa : Qa = Qa::new_unwrap(3, 3);
  • Using Qa::new_static to create an instance at compile time, which is also when the validity of the coordinates is checked.
    const QA : Qa = Qa::new_static::<3, 3>();
    The following, for instance, doesn’t compile:
    const QA : Qa = Qa::new_static::<3, 30>();

Implementations§

source§

impl<const W: u16, const H: u16> Qa<W, H>

source

pub const WIDTH: u16 = W

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

source

pub const HEIGHT: u16 = H

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: Qa<W, H> = _

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

source

pub const LAST: Qa<W, H> = _

Coordinates of the last element of the grid.

source

pub const CENTER: Qa<W, H> = _

Center the (approximate) center coordinate.

source

pub const TOP_LEFT: Qa<W, H> = _

Coordinates of the top-left coordinate.

source

pub const TOP_RIGHT: Qa<W, H> = _

Coordinates of the top-right coordinate.

source

pub const BOTTOM_LEFT: Qa<W, H> = _

Coordinates of the bottom-left coordinate.

source

pub const BOTTOM_RIGHT: Qa<W, H> = _

Coordinates of the bottom-right coordinate.

source

pub const fn new(x: u16, y: u16) -> Result<Self, Error>

Create a new Qa instance; returns error if a coordinate is out-of-bounds.

source

pub const fn new_unwrap(x: u16, y: u16) -> Self

Create a new Qa 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 Qa instance at compile time.

Checks arguments at compile time - for instance, the following doesn’t compile:

const QA : sqrid::Qa<5,5> = sqrid::Qa::<5,5>::new_static::<9,9>();
source

pub fn is_corner(&self) -> bool

Return true if self is a corner of the grid.

source

pub fn is_side(&self) -> bool

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

source

pub fn flip_h(&self) -> Qa<W, H>

Flip the coordinate vertically

source

pub fn flip_v(&self) -> Qa<W, H>

Flip the coordinate horizontally

source

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

Return the corresponding (u16, u16) tuple.

source

pub fn tryfrom_tuple(xyref: impl Borrow<(u16, u16)>) -> Result<Qa<W, H>, Error>

Create a new Qa from the provided (u16, u16), if possible; return an error otherwise.

source

pub fn tryfrom_qa<const W2: u16, const H2: u16>( qa2: Qa<W2, H2> ) -> Result<Qa<W, H>, Error>

Create a new Qa from the provided Qa with different dimensions, if possible; return an error otherwise.

source

pub fn tryfrom_usize(iref: impl Borrow<usize>) -> Result<Qa<W, H>, Error>

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

source

pub fn tlbr_of( iter: impl Iterator<Item = Qa<W, H>> ) -> Result<(Qa<W, H>, Qa<W, H>), Error>

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

source

pub fn to_usize(&self) -> usize

Return a usize index corresponding to the Qa.

source

pub fn next(self) -> Option<Self>

Return the next Qa in sequence (English read sequence), or None if self is the last one.

source

pub fn iter() -> QaIter<W, H>

Return an iterator that returns all Qa’s within the grid dimensions.

source

pub fn iter_horizontal() -> QaIter<W, H>

Return an iterator that returns all Qa’s within the grid dimensions horizontally.

source

pub fn iter_vertical() -> QaIter<W, H>

Return an iterator that returns all Qa’s within the grid dimensions vertically.

source

pub fn iter_range(topleft: Self, botright: Self) -> QaIterRange<W, H>

Return an iterator that returns all Qa’s within the grid coordinates.

source

pub fn iter_in_x(x: u16) -> Option<QaIterInX<W, H>>

Return an iterator that returns all Qa’s in a column.

source

pub fn iter_in_y(y: u16) -> Option<QaIterInY<W, H>>

Return an iterator that returns all Qa’s in a line.

source

pub fn manhattan(qa1: &Qa<W, H>, qa2: &Qa<W, H>) -> usize

Return the manhattan distance between 2 Qas of the same type

source

pub fn inside(&self, qa1: &Qa<W, H>, qa2: &Qa<W, H>) -> bool

Check that the Qa is inside the provided limits

source§

impl<const W: u16> Qa<W, W>

source

pub fn rotate_cw(&self) -> Qa<W, W>

Rotate the square grid coordinate 90 degrees clockwise

source

pub fn rotate_cc(&self) -> Qa<W, W>

Rotate the square grid coordinate 90 degrees counter-clockwise

Trait Implementations§

source§

impl<const W: u16, const H: u16> Add<&Qr> for Qa<W, H>

§

type Output = Result<Qa<W, H>, Error>

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl<const W: u16, const H: u16> Add<Qr> for Qa<W, H>

§

type Output = Result<Qa<W, H>, Error>

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl<const WIDTH: u16, const HEIGHT: u16> Clone for Qa<WIDTH, HEIGHT>

source§

fn clone(&self) -> Qa<WIDTH, HEIGHT>

Returns a copy 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 WIDTH: u16, const HEIGHT: u16> Debug for Qa<WIDTH, HEIGHT>

source§

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

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

impl<const WIDTH: u16, const HEIGHT: u16> Default for Qa<WIDTH, HEIGHT>

source§

fn default() -> Qa<WIDTH, HEIGHT>

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

impl<const W: u16, const H: u16> Display for Qa<W, H>

source§

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

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

impl<const W: u16, const H: u16> From<&Qa<W, H>> for (i32, i32)

source§

fn from(qa: &Qa<W, H>) -> Self

Converts to this type from the input type.
source§

impl<const W: u16, const H: u16> From<&Qa<W, H>> for (u16, u16)

source§

fn from(qa: &Qa<W, H>) -> Self

Converts to this type from the input type.
source§

impl<const W: u16, const H: u16> From<&Qa<W, H>> for usize

source§

fn from(qa: &Qa<W, H>) -> Self

Converts to this type from the input type.
source§

impl<const W: u16, const H: u16> From<Qa<W, H>> for (i32, i32)

source§

fn from(qa: Qa<W, H>) -> Self

Converts to this type from the input type.
source§

impl<const W: u16, const H: u16> From<Qa<W, H>> for (u16, u16)

source§

fn from(qa: Qa<W, H>) -> Self

Converts to this type from the input type.
source§

impl<const W: u16, const H: u16> From<Qa<W, H>> for usize

source§

fn from(qa: Qa<W, H>) -> Self

Converts to this type from the input type.
source§

impl<const WIDTH: u16, const HEIGHT: u16> Hash for Qa<WIDTH, HEIGHT>

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 WIDTH: u16, const HEIGHT: u16> Ord for Qa<WIDTH, HEIGHT>

source§

fn cmp(&self, other: &Qa<WIDTH, HEIGHT>) -> 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 + PartialOrd,

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

impl<const WIDTH: u16, const HEIGHT: u16> PartialEq for Qa<WIDTH, HEIGHT>

source§

fn eq(&self, other: &Qa<WIDTH, HEIGHT>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const WIDTH: u16, const HEIGHT: u16> PartialOrd for Qa<WIDTH, HEIGHT>

source§

fn partial_cmp(&self, other: &Qa<WIDTH, HEIGHT>) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<const W: u16, const H: u16> TryFrom<&(i32, i32)> for Qa<W, H>

§

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 W: u16, const H: u16> TryFrom<&(u16, u16)> for Qa<W, H>

§

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 W: u16, const H: u16> TryFrom<(i32, i32)> for Qa<W, H>

§

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 W: u16, const H: u16> TryFrom<(u16, u16)> for Qa<W, H>

§

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 W: u16, const H: u16> TryFrom<usize> for Qa<W, H>

§

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 WIDTH: u16, const HEIGHT: u16> Copy for Qa<WIDTH, HEIGHT>

source§

impl<const WIDTH: u16, const HEIGHT: u16> Eq for Qa<WIDTH, HEIGHT>

source§

impl<const WIDTH: u16, const HEIGHT: u16> StructuralEq for Qa<WIDTH, HEIGHT>

source§

impl<const WIDTH: u16, const HEIGHT: u16> StructuralPartialEq for Qa<WIDTH, HEIGHT>

Auto Trait Implementations§

§

impl<const WIDTH: u16, const HEIGHT: u16> RefUnwindSafe for Qa<WIDTH, HEIGHT>

§

impl<const WIDTH: u16, const HEIGHT: u16> Send for Qa<WIDTH, HEIGHT>

§

impl<const WIDTH: u16, const HEIGHT: u16> Sync for Qa<WIDTH, HEIGHT>

§

impl<const WIDTH: u16, const HEIGHT: u16> Unpin for Qa<WIDTH, HEIGHT>

§

impl<const WIDTH: u16, const HEIGHT: u16> UnwindSafe for Qa<WIDTH, HEIGHT>

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

§

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§

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

§

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

§

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.