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>
impl<T, P: PosT, const SIZE: usize> Grid<T, P, SIZE>
Sourcepub fn repeat(item: T) -> Selfwhere
T: Copy,
pub fn repeat(item: T) -> Selfwhere
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”.
Sourcepub fn into_inner(self) -> [T; SIZE]
pub fn into_inner(self) -> [T; SIZE]
“Dismantle” a Grid into the inner array; consumes self.
Sourcepub fn as_array_mut(&mut self) -> &mut [T; SIZE]
pub fn as_array_mut(&mut self) -> &mut [T; SIZE]
Return a mut reference to the inner array
Sourcepub fn line(&self, lineno: P::Ytype) -> &[T]
pub fn line(&self, lineno: P::Ytype) -> &[T]
Return a specific grid line as a reference to a slice
Sourcepub fn line_mut(&mut self, lineno: P::Ytype) -> &mut [T]
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.
Sourcepub fn get(&self, pos: &P) -> &T
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.
Sourcepub fn get_mut(&mut self, pos: &P) -> &mut T
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.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns an iterator that allows modifying each value
Sourcepub fn iter_pos(&self) -> impl Iterator<Item = (P, &T)>where
P: Copy,
pub fn iter_pos(&self) -> impl Iterator<Item = (P, &T)>where
P: Copy,
Returns an iterator over the grid coordinates and values
Sourcepub fn extend_from_vecvec(&mut self, v: Vec<Vec<T>>) -> Result<(), Error>
pub fn extend_from_vecvec(&mut self, v: Vec<Vec<T>>) -> Result<(), Error>
Set values from nested vectors
Sourcepub fn from_iter_values<It>(iter: It) -> Result<Self, Error>where
It: IntoIterator<Item = T>,
T: Default,
pub fn from_iter_values<It>(iter: It) -> Result<Self, Error>where
It: IntoIterator<Item = T>,
T: Default,
Creates a Grid from an iterator that returns items
Sourcepub fn from_iter_ref<'a, It>(iter: It) -> Result<Self, Error>
pub fn from_iter_ref<'a, It>(iter: It) -> Result<Self, Error>
Creates a Grid from an iterator that returns references
Trait Implementations§
Source§impl<T: Display, P: PosT, const SIZE: usize> Display for Grid<T, P, SIZE>
Pretty-printer Grid display implementation
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§impl<'a, T: 'a + Copy, P, const SIZE: usize> Extend<&'a (P, T)> for Grid<T, P, SIZE>
impl<'a, T: 'a + Copy, P, const SIZE: usize> Extend<&'a (P, T)> for Grid<T, P, SIZE>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a (P, T)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a (P, T)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, T: 'a + Copy, P: PosT, const SIZE: usize> Extend<(P, &'a T)> for Grid<T, P, SIZE>
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)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (P, &'a T)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T, P: PosT, const SIZE: usize> Extend<(P, T)> for Grid<T, P, SIZE>
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)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (P, T)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, T: 'a + Clone + Default, P: PosT, const SIZE: usize> FromIterator<&'a T> for Grid<T, P, SIZE>
Creates a Grid from an iterator that returns references
impl<'a, T: 'a + Clone + 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§impl<T: Default, P: PosT, const SIZE: usize> FromIterator<T> for Grid<T, P, SIZE>
Creates a Grid from an iterator that returns values
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.