rust_grid/
index.rs

1use crate::*;
2use std::ops::{ Index, IndexMut };
3
4impl<T: Clone> Index<usize> for Row<T> {
5    type Output = T;
6
7    fn index(&self, index: usize) -> &Self::Output {
8        &self.cells[index]
9    }
10}
11
12impl<T: Clone> IndexMut<usize> for Row<T> {
13    fn index_mut(&mut self, index: usize) -> &mut T {
14        &mut self.cells[index]
15    }
16}
17
18impl<T: Clone> Index<usize> for Grid<T> {
19    type Output = Row<T>;
20
21    fn index(&self, index: usize) -> &Self::Output {
22        &self.rows[index]
23    }
24}
25
26impl<T: Clone> Index<(usize, usize)> for Grid<T> {
27    type Output = T;
28
29    fn index(&self, (x, y): (usize, usize)) -> &Self::Output {
30        &self.rows[y][x]
31    }
32}
33
34impl<T: Clone> IndexMut<usize> for Grid<T> {
35    fn index_mut(&mut self, index: usize) -> &mut Row<T> {
36        &mut self.rows[index]
37    }
38}
39
40impl<T: Clone> IndexMut<(usize, usize)> for Grid<T> {
41    fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut T {
42        &mut self.rows[y][x]
43    }
44}