Add get mutable reference

This commit is contained in:
Armin Becher 2020-04-04 21:54:44 +02:00
parent b02d4d211a
commit ae329ef765

View file

@ -143,6 +143,17 @@ impl<T: Clone> Grid<T> {
} }
} }
/// Mutable access to a certain element in the grid.
/// Returns None if an element beyond the grid bounds is tried to be accessed.
pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> {
if row < self.rows && col < self.cols() {
let cols = self.cols();
unsafe { Some(self.data.get_unchecked_mut(row * cols + col)) }
} else {
None
}
}
/// Returns the size of the gird as a two element tuple. /// Returns the size of the gird as a two element tuple.
/// First element are the number of rows and the second the columns. /// First element are the number of rows and the second the columns.
pub fn size(&self) -> (usize, usize) { pub fn size(&self) -> (usize, usize) {
@ -307,6 +318,21 @@ mod test {
assert_eq!(grid.get(1, 0), None); assert_eq!(grid.get(1, 0), None);
} }
#[test]
fn get_mut() {
let mut grid = Grid::init(1, 2, 3);
let mut_ref = grid.get_mut(0, 0).unwrap();
*mut_ref = 5;
assert_eq!(grid[0][0], 5);
}
#[test]
fn get_mut_none() {
let mut grid = Grid::init(1, 2, 3);
let mut_ref = grid.get_mut(1, 4);
assert_eq!(mut_ref, None);
}
#[test] #[test]
fn idx() { fn idx() {
let grid = Grid::init(1, 2, 3); let grid = Grid::init(1, 2, 3);