Add clone

This commit is contained in:
Armin Becher 2020-04-04 21:11:45 +02:00
parent 902b36a27f
commit b02d4d211a

View file

@ -160,6 +160,16 @@ impl<T: Clone> Grid<T> {
} }
} }
impl<T: Clone> Clone for Grid<T> {
fn clone(&self) -> Self {
Grid {
rows: self.rows,
cols: self.cols,
data: self.data.clone(),
}
}
}
impl<T: Clone> Index<usize> for Grid<T> { impl<T: Clone> Index<usize> for Grid<T> {
type Output = [T]; type Output = [T];
@ -185,6 +195,15 @@ impl<T: Clone> IndexMut<usize> for Grid<T> {
mod test { mod test {
use super::*; use super::*;
#[test]
fn clone() {
let grid = grid![[1, 2, 3][4, 5, 6]];
let mut clone = grid.clone();
clone[0][2] = 10;
assert_eq!(grid[0][2], 3);
assert_eq!(clone[0][2], 10);
}
#[test] #[test]
fn macro_init() { fn macro_init() {
let grid = grid![[1, 2, 3][4, 5, 6]]; let grid = grid![[1, 2, 3][4, 5, 6]];