Add clone
This commit is contained in:
parent
902b36a27f
commit
b02d4d211a
1 changed files with 19 additions and 0 deletions
19
src/lib.rs
19
src/lib.rs
|
@ -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]];
|
||||||
|
|
Loading…
Reference in a new issue