pub struct Grid<T> { /* private fields */ }Expand description
A 2d fixed-size grid containers for cells of type T
Implementations§
source§impl<T: Default> Grid<T>
impl<T: Default> Grid<T>
sourcepub fn new(width: usize, height: usize) -> Self
pub fn new(width: usize, height: usize) -> Self
Create a grid using the default value of T for each cell
Example
let grid: Grid<i32> = Grid::new(2, 2);
assert_eq!(grid.get([0, 0]), Some(&0));
assert_eq!(grid.get([1, 0]), Some(&0));
assert_eq!(grid.get([0, 1]), Some(&0));
assert_eq!(grid.get([1, 1]), Some(&0));source§impl<T> Grid<T>
impl<T> Grid<T>
sourcepub fn new_with(
width: usize,
height: usize,
init_cell: impl FnMut(Coord) -> T
) -> Self
pub fn new_with( width: usize, height: usize, init_cell: impl FnMut(Coord) -> T ) -> Self
Create a grid using init_cell function for each cell
Example
let grid = Grid::new_with(2, 2, |coord| coord);
assert_eq!(grid.get([0, 0]), Some(&Coord { x: 0, y: 0 }));
assert_eq!(grid.get([1, 0]), Some(&Coord { x: 1, y: 0 }));
assert_eq!(grid.get([0, 1]), Some(&Coord { x: 0, y: 1 }));
assert_eq!(grid.get([1, 1]), Some(&Coord { x: 1, y: 1 }));sourcepub fn from_row_major_iter(
width: usize,
iter: impl IntoIterator<Item = T>
) -> Self
pub fn from_row_major_iter( width: usize, iter: impl IntoIterator<Item = T> ) -> Self
Create a grid using the row-major iter
Example
let grid = Grid::from_row_major_iter(2, [1, 2, 3, 4]);
assert_eq!(grid.get([0, 0]), Some(&1));
assert_eq!(grid.get([1, 0]), Some(&2));
assert_eq!(grid.get([0, 1]), Some(&3));
assert_eq!(grid.get([1, 1]), Some(&4));Trait Implementations§
source§impl<T: PartialEq> PartialEq for Grid<T>
impl<T: PartialEq> PartialEq for Grid<T>
impl<T: Eq> Eq for Grid<T>
impl<T> StructuralEq for Grid<T>
impl<T> StructuralPartialEq for Grid<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Grid<T>where
T: RefUnwindSafe,
impl<T> Send for Grid<T>where
T: Send,
impl<T> Sync for Grid<T>where
T: Sync,
impl<T> Unpin for Grid<T>where
T: Unpin,
impl<T> UnwindSafe for Grid<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more