Struct sqrid::grid::Grid

source ·
pub struct Grid<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize>(/* private fields */);
Expand description

A grid is a generic array that can be indexed by a Pos

We can also interact with specific lines with Grid::line and Grid::line_mut, or with the whole underlying array with as_ref and as_mut.

At the moment we have to provide a SIZE argument = WIDTH * HEIGHT. This value is checked at compile time, but can’t be ellided at the moment, due to rust const generics limitations.

We can use the grid_create macro to use a Pos as a source of these values.

Implementations§

source§

impl<T, const W: u16, const H: u16, const SIZE: usize> Grid<T, W, H, SIZE>

source

pub const SIZE: usize = SIZE

Number of elements in the grid.

source

pub fn repeat(item: T) -> Self
where T: Copy,

Create a grid filled with copies of the provided item

This is the fastest of the repeat_* functions, that’s why it’s the “default”.

source

pub fn into_inner(self) -> [T; SIZE]

“Dismantle” a Grid into the inner array; consumes self.

source

pub fn as_array(&self) -> &[T; SIZE]

Return a reference to the inner array

source

pub fn as_array_mut(&mut self) -> &mut [T; SIZE]

Return a mut reference to the inner array

source

pub fn line(&self, lineno: u16) -> &[T]

Return a specific grid line as a reference to a slice

source

pub fn line_mut(&mut self, lineno: u16) -> &mut [T]

Return a specific grid line as a mut reference to a slice

Allows quick assignment operations on whole lines.

source

pub fn get(&self, pos: impl Borrow<Pos<W, H>>) -> &T

Get a reference to an element of the grid.

We use get_unchecked internally, because we guarantee the validity of the Pos index on construction.

source

pub fn get_mut(&mut self, pos: impl Borrow<Pos<W, H>>) -> &mut T

Get a mut reference to an element of the grid.

We use get_unchecked internally, because we guarantee the validity of the Pos index on construction.

source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the grid values

source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns an iterator that allows modifying each value

source

pub fn iter_pos(&self) -> impl Iterator<Item = (Pos<W, H>, &T)>

Returns an iterator over the grid coordinates and values

source

pub fn flip_h(&mut self)

Flip all elements horizontally.

source

pub fn flip_v(&mut self)

Flip all elements vertically.

source§

impl<T, const W: u16, const SIZE: usize> Grid<T, W, W, SIZE>

source

pub fn rotate_cw(&mut self)

Rotate all elements 90 degrees clockwise

source

pub fn rotate_cc(&mut self)

Rotate all elements 90 degrees counterclockwise

Trait Implementations§

source§

impl<T, const W: u16, const H: u16, const SIZE: usize> AsMut<[T; SIZE]> for Grid<T, W, H, SIZE>

source§

fn as_mut(&mut self) -> &mut [T; SIZE]

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T, const W: u16, const H: u16, const SIZE: usize> AsRef<[T; SIZE]> for Grid<T, W, H, SIZE>

source§

fn as_ref(&self) -> &[T; SIZE]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: Clone, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Clone for Grid<T, WIDTH, HEIGHT, SIZE>

source§

fn clone(&self) -> Grid<T, WIDTH, HEIGHT, SIZE>

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<T: Debug, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Debug for Grid<T, WIDTH, HEIGHT, SIZE>

source§

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

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

impl<T: Default, const W: u16, const H: u16, const SIZE: usize> Default for Grid<T, W, H, SIZE>

source§

fn default() -> Self

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

impl<T: Display, const W: u16, const H: u16, const SIZE: usize> Display for Grid<T, W, H, SIZE>

Pretty-printer Grid display implementation

The Display implementation of grid was made to print an ascii-like grid. It does that in one pass, and uses the padding parameter as the size to reserve for each member.

source§

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

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

impl<'a, T: 'a + Copy, const W: u16, const H: u16, const SIZE: usize> Extend<&'a (Pos<W, H>, T)> for Grid<T, W, H, SIZE>

source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = &'a (Pos<W, H>, T)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<'a, T: 'a + Copy, const W: u16, const H: u16, const SIZE: usize> Extend<(Pos<W, H>, &'a T)> for Grid<T, W, H, SIZE>

source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = (Pos<W, H>, &'a T)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T, const W: u16, const H: u16, const SIZE: usize> Extend<(Pos<W, H>, T)> for Grid<T, W, H, SIZE>

source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = (Pos<W, H>, T)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<'a, T: 'a + Copy + Default, const W: u16, const H: u16, const SIZE: usize> FromIterator<&'a T> for Grid<T, W, H, SIZE>

Creates a Grid from an iterator that returns references

Assumes we are getting exactly all grid elements; it panics otherwise.

source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = &'a T>,

Creates a value from an iterator. Read more
source§

impl<T: Default, const W: u16, const H: u16, const SIZE: usize> FromIterator<T> for Grid<T, W, H, SIZE>

Creates a Grid from an iterator that returns values

Assumes we are getting exactly all grid elements; it panics otherwise.

source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
source§

impl<T: Hash, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Hash for Grid<T, WIDTH, HEIGHT, SIZE>

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<T, APOS, const W: u16, const H: u16, const SIZE: usize> Index<APOS> for Grid<T, W, H, SIZE>
where APOS: Borrow<Pos<W, H>>,

§

type Output = T

The returned type after indexing.
source§

fn index(&self, pos: APOS) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T, APOS, const W: u16, const H: u16, const SIZE: usize> IndexMut<APOS> for Grid<T, W, H, SIZE>
where APOS: Borrow<Pos<W, H>>,

source§

fn index_mut(&mut self, pos: APOS) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a, T, const W: u16, const H: u16, const SIZE: usize> IntoIterator for &'a Grid<T, W, H, SIZE>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T, const W: u16, const H: u16, const SIZE: usize> IntoIterator for &'a mut Grid<T, W, H, SIZE>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, const W: u16, const H: u16, const SIZE: usize> IntoIterator for Grid<T, W, H, SIZE>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T, SIZE>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<Item, const W: u16, const H: u16, const WORDS: usize, const SIZE: usize> MapPos<Item, W, H, WORDS, SIZE> for Grid<Item, W, H, SIZE>
where Item: Copy,

source§

fn new(item: Item) -> Self

Create a new MapPos with the provided value for all items
source§

fn get(&self, pos: &Pos<W, H>) -> &Item

Get the item corresponding to the provided Pos
source§

fn set(&mut self, pos: Pos<W, H>, item: Item)

Set the item corresponding to the provided Pos
source§

impl<T, const W: u16, const H: u16, const SIZE: usize> Neg for Grid<T, W, H, SIZE>
where T: Neg<Output = T> + Default + Copy,

§

type Output = Grid<T, W, H, SIZE>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: Ord, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Ord for Grid<T, WIDTH, HEIGHT, SIZE>

source§

fn cmp(&self, other: &Grid<T, WIDTH, HEIGHT, SIZE>) -> 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<T: PartialEq, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> PartialEq for Grid<T, WIDTH, HEIGHT, SIZE>

source§

fn eq(&self, other: &Grid<T, WIDTH, HEIGHT, SIZE>) -> 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<T: PartialOrd, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> PartialOrd for Grid<T, WIDTH, HEIGHT, SIZE>

source§

fn partial_cmp(&self, other: &Grid<T, WIDTH, HEIGHT, SIZE>) -> 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<T: Default, const W: u16, const H: u16, const SIZE: usize> TryFrom<Vec<Vec<T>>> for Grid<T, W, H, SIZE>

§

type Error = Error

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

fn try_from(vec: Vec<Vec<T>>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: Copy, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Copy for Grid<T, WIDTH, HEIGHT, SIZE>

source§

impl<T: Eq, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Eq for Grid<T, WIDTH, HEIGHT, SIZE>

source§

impl<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> StructuralEq for Grid<T, WIDTH, HEIGHT, SIZE>

source§

impl<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> StructuralPartialEq for Grid<T, WIDTH, HEIGHT, SIZE>

Auto Trait Implementations§

§

impl<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> RefUnwindSafe for Grid<T, WIDTH, HEIGHT, SIZE>
where T: RefUnwindSafe,

§

impl<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Send for Grid<T, WIDTH, HEIGHT, SIZE>
where T: Send,

§

impl<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Sync for Grid<T, WIDTH, HEIGHT, SIZE>
where T: Sync,

§

impl<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> Unpin for Grid<T, WIDTH, HEIGHT, SIZE>
where T: Unpin,

§

impl<T, const WIDTH: u16, const HEIGHT: u16, const SIZE: usize> UnwindSafe for Grid<T, WIDTH, HEIGHT, SIZE>
where T: UnwindSafe,

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.