Skip to main content

grid_map/map/
access.rs

1use std::ops::{Index, IndexMut};
2
3use crate::{GridMap, Loc};
4
5impl<T> GridMap<T> {
6    //! Access
7
8    /// Gets the cell at the location
9    pub fn get(&self, loc: Loc) -> &T {
10        debug_assert!(self.dims.contains(loc));
11
12        let index: usize = self.index(loc);
13        &self.cells[index]
14    }
15
16    /// Gets the mutable cell at the location
17    pub fn get_mut(&mut self, loc: Loc) -> &mut T {
18        debug_assert!(self.dims.contains(loc));
19
20        let index: usize = self.index(loc);
21        &mut self.cells[index]
22    }
23
24    /// Sets the cell at the location
25    pub fn set(&mut self, loc: Loc, cell: T) {
26        debug_assert!(self.dims.contains(loc));
27
28        let index: usize = self.index(loc);
29        self.cells[index] = cell;
30    }
31}
32
33impl<T> Index<Loc> for GridMap<T> {
34    type Output = T;
35
36    fn index(&self, loc: Loc) -> &T {
37        self.get(loc)
38    }
39}
40
41impl<T> IndexMut<Loc> for GridMap<T> {
42    fn index_mut(&mut self, loc: Loc) -> &mut T {
43        self.get_mut(loc)
44    }
45}