Struct Grid

Source
pub struct Grid<T, P: PosT, 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, P: PosT, const SIZE: usize> Grid<T, P, 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: P::Ytype) -> &[T]

Return a specific grid line as a reference to a slice

Source

pub fn line_mut(&mut self, lineno: P::Ytype) -> &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: &P) -> &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: &P) -> &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 = (P, &T)>
where P: Copy,

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

pub fn extend_from_vecvec(&mut self, v: Vec<Vec<T>>) -> Result<(), Error>

Set values from nested vectors

Source§

impl<T, const W: u16, const SIZE: usize> Grid<T, Pos<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, P: PosT, const SIZE: usize> AsMut<[T; SIZE]> for Grid<T, P, 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, P: PosT, const SIZE: usize> AsRef<[T; SIZE]> for Grid<T, P, 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, P: Clone + PosT, const SIZE: usize> Clone for Grid<T, P, SIZE>

Source§

fn clone(&self) -> Grid<T, P, 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, P: Debug + PosT, const SIZE: usize> Debug for Grid<T, P, SIZE>

Source§

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

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

impl<T: Default, P: PosT, const SIZE: usize> Default for Grid<T, P, SIZE>

Source§

fn default() -> Self

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

impl<T: Display, P: PosT, const SIZE: usize> Display for Grid<T, P, 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, P, const SIZE: usize> Extend<&'a (P, T)> for Grid<T, P, SIZE>
where P: Copy + PosT,

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = &'a (P, 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, P: PosT, const SIZE: usize> Extend<(P, &'a T)> for Grid<T, P, SIZE>

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = (P, &'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, P: PosT, const SIZE: usize> Extend<(P, T)> for Grid<T, P, SIZE>

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = (P, 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, P: PosT, const SIZE: usize> FromIterator<&'a T> for Grid<T, P, 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, P: PosT, const SIZE: usize> FromIterator<T> for Grid<T, P, 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, P: Hash + PosT, const SIZE: usize> Hash for Grid<T, P, 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, P: PosT, const SIZE: usize> Index<&P> for Grid<T, P, SIZE>

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T, P: PosT, const SIZE: usize> Index<P> for Grid<T, P, SIZE>

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T, P: PosT, const SIZE: usize> IndexMut<&P> for Grid<T, P, SIZE>

Source§

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

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

impl<T, P: PosT, const SIZE: usize> IndexMut<P> for Grid<T, P, SIZE>

Source§

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

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

impl<'a, T, P: PosT, const SIZE: usize> IntoIterator for &'a Grid<T, P, SIZE>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

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, P: PosT, const SIZE: usize> IntoIterator for &'a mut Grid<T, P, SIZE>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

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, P: PosT, const SIZE: usize> IntoIterator for Grid<T, P, SIZE>

Source§

type Item = T

The type of the elements being iterated over.
Source§

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, P: PosT, const WORDS: usize, const SIZE: usize> MapPos<Item, P, WORDS, SIZE> for Grid<Item, P, 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: &P) -> &Item

Get the item corresponding to the provided super::pos::Pos
Source§

fn set(&mut self, pos: P, item: Item)

Set the item corresponding to the provided super::pos::Pos
Source§

impl<T, P: PosT, const SIZE: usize> Neg for Grid<T, P, SIZE>
where T: Neg<Output = T> + Default + Copy,

Source§

type Output = Grid<T, P, SIZE>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: Ord, P: Ord + PosT, const SIZE: usize> Ord for Grid<T, P, SIZE>

Source§

fn cmp(&self, other: &Grid<T, P, 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,

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

impl<T: PartialEq, P: PartialEq + PosT, const SIZE: usize> PartialEq for Grid<T, P, SIZE>

Source§

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

Source§

fn partial_cmp(&self, other: &Grid<T, P, 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

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<T: Default, P: PosT, const SIZE: usize> TryFrom<Vec<Vec<T>>> for Grid<T, P, SIZE>

Source§

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, P: Copy + PosT, const SIZE: usize> Copy for Grid<T, P, SIZE>

Source§

impl<T: Eq, P: Eq + PosT, const SIZE: usize> Eq for Grid<T, P, SIZE>

Source§

impl<T, P: PosT, const SIZE: usize> StructuralPartialEq for Grid<T, P, SIZE>

Auto Trait Implementations§

§

impl<T, P, const SIZE: usize> Freeze for Grid<T, P, SIZE>
where T: Freeze,

§

impl<T, P, const SIZE: usize> RefUnwindSafe for Grid<T, P, SIZE>

§

impl<T, P, const SIZE: usize> Send for Grid<T, P, SIZE>
where T: Send, P: Send,

§

impl<T, P, const SIZE: usize> Sync for Grid<T, P, SIZE>
where T: Sync, P: Sync,

§

impl<T, P, const SIZE: usize> Unpin for Grid<T, P, SIZE>
where T: Unpin, P: Unpin,

§

impl<T, P, const SIZE: usize> UnwindSafe for Grid<T, P, SIZE>
where T: UnwindSafe, P: 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> 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.