pub struct Grid<T> { /* private fields */ }
๐Deprecated since 0.1.4: Use
DynamicGrid
insteadExpand 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));
Sourcepub fn get(&self, coord: impl Into<Coord>) -> Option<&T>
pub fn get(&self, coord: impl Into<Coord>) -> Option<&T>
Access the cell at coord
Returns None
if the coord
is out of bounds
Sourcepub fn get_mut(&mut self, coord: impl Into<Coord>) -> Option<&mut T>
pub fn get_mut(&mut self, coord: impl Into<Coord>) -> Option<&mut T>
Access mutably the cell at coord
Returns None
if the coord
is out of bounds
Trait Implementationsยง
impl<T: Eq> Eq for Grid<T>
impl<T> StructuralPartialEq for Grid<T>
Auto Trait Implementationsยง
impl<T> Freeze for Grid<T>
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