diff --git a/src/lib.rs b/src/lib.rs index f600503..37f1b0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,7 +124,7 @@ pub struct Grid { rows: usize, } -impl Grid { +impl Grid { /// Init a grid of size rows x columns with default values of the given type. /// For example this will generate a 2x3 grid of zeros: /// ``` @@ -139,15 +139,16 @@ impl Grid { if rows < 1 || cols < 1 { panic!("Grid size of rows and columns must be greater than zero."); } - Grid { - data: vec![T::default(); rows * cols], - cols, - rows, - } + let mut data = Vec::new(); + data.resize_with(rows * cols, T::default); + Grid { data, cols, rows } } /// Init a grid of size rows x columns with the given data element. - pub fn init(rows: usize, cols: usize, data: T) -> Grid { + pub fn init(rows: usize, cols: usize, data: T) -> Grid + where + T: Clone, + { if rows < 1 || cols < 1 { panic!("Grid size of rows and columns must be greater than zero."); } @@ -503,9 +504,9 @@ impl Grid { ) } self.data.reserve(col.len()); - for (idx, d) in col.iter().enumerate() { + for (idx, d) in col.into_iter().enumerate() { let vec_idx = (idx + 1) * self.cols + idx; - self.data.insert(vec_idx, d.to_owned()); + self.data.insert(vec_idx, d); } self.cols += 1; self.rows = input_col_len; @@ -586,7 +587,7 @@ impl Grid { } self.rows += 1; let data_idx = index * self.cols; - self.data.splice(data_idx..data_idx, row.iter().cloned()); + self.data.splice(data_idx..data_idx, row.into_iter()); } /// Insert a new column at the index. @@ -610,9 +611,9 @@ impl Grid { if index > self.cols { panic!("Out of range. Index was {}, but must be less or equal to {}.", index, self.rows); } - for (row_iter, col_val) in col.iter().enumerate() { + for (row_iter, col_val) in col.into_iter().enumerate() { let data_idx = row_iter * self.cols + index + row_iter; - self.data.insert(data_idx, col_val.clone()); + self.data.insert(data_idx, col_val); } self.cols += 1; } @@ -639,7 +640,10 @@ impl Grid { } /// Transpose the grid so that columns become rows in new grid. - pub fn transpose(&self) -> Grid { + pub fn transpose(&self) -> Grid + where + T: Clone, + { let mut data = Vec::with_capacity(self.data.len()); for c in 0..self.cols { for r in 0..self.rows { @@ -664,7 +668,7 @@ impl Clone for Grid { } } -impl Index for Grid { +impl Index for Grid { type Output = [T]; fn index(&self, idx: usize) -> &Self::Output { @@ -680,7 +684,7 @@ impl Index for Grid { } } -impl IndexMut for Grid { +impl IndexMut for Grid { fn index_mut(&mut self, idx: usize) -> &mut Self::Output { &mut self.data[(idx * self.cols)..] }