diff --git a/Cargo.toml b/Cargo.toml index 9c789c7..be4582a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,8 @@ version = "0.12.0" authors = ["Armin Becher "] edition = "2018" description = "Dynamic generic 2D data structure." -keywords = [ "2D", "array", "matrix", "data-structure", "2D-vector"] -categories = [ "science", "data-structures",] +keywords = ["2D", "array", "matrix", "data-structure", "2D-vector"] +categories = ["science", "data-structures"] readme = "README.md" license = "MIT" repository = "https://github.com/becheran/grid" @@ -23,7 +23,7 @@ serde = { version = "1.0.188", features = ["derive"], optional = true } [dev-dependencies] criterion = "0.3.6" -rand="0.8.5" +rand = "0.8.5" serde_json = "1.0.106" [[bench]] @@ -31,6 +31,6 @@ name = "benches" harness = false [features] -default = [ "std" ] # Default to using the std +default = ["std"] # Default to using the std std = [] -serde = [ "std", "dep:serde" ] +serde = ["std", "dep:serde"] diff --git a/src/lib.rs b/src/lib.rs index cfeccfb..25e4641 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,7 +52,6 @@ use serde::{ ser::{Serialize, SerializeStruct, Serializer}, }; -use core::cmp; use core::cmp::Eq; use core::fmt; use core::hash; @@ -61,6 +60,7 @@ use core::ops::Index; use core::ops::IndexMut; use core::slice::Iter; use core::slice::IterMut; +use core::{cmp, convert::TryInto}; #[doc(hidden)] #[macro_export] @@ -496,8 +496,8 @@ impl Grid { /// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used. #[inline] #[must_use] - pub unsafe fn get_unchecked(&self, row: usize, col: usize) -> &T { - let index = self.get_index(row, col); + pub unsafe fn get_unchecked(&self, row: impl Into, col: impl Into) -> &T { + let index = self.get_index(row.into(), col.into()); self.data.get_unchecked(index) } @@ -509,17 +509,23 @@ impl Grid { /// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used. #[inline] #[must_use] - pub unsafe fn get_unchecked_mut(&mut self, row: usize, col: usize) -> &mut T { - let index = self.get_index(row, col); + pub unsafe fn get_unchecked_mut( + &mut self, + row: impl Into, + col: impl Into, + ) -> &mut T { + let index = self.get_index(row.into(), col.into()); self.data.get_unchecked_mut(index) } /// Access a certain element in the grid. /// Returns `None` if an element beyond the grid bounds is tried to be accessed. #[must_use] - pub fn get(&self, row: usize, col: usize) -> Option<&T> { - if row < self.rows && col < self.cols { - unsafe { Some(self.get_unchecked(row, col)) } + pub fn get(&self, row: impl TryInto, col: impl TryInto) -> Option<&T> { + let row_usize = row.try_into().ok()?; + let col_usize = col.try_into().ok()?; + if row_usize < self.rows && col_usize < self.cols { + unsafe { Some(self.get_unchecked(row_usize, col_usize)) } } else { None } @@ -528,9 +534,15 @@ impl Grid { /// Mutable access to a certain element in the grid. /// Returns `None` if an element beyond the grid bounds is tried to be accessed. #[must_use] - pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> { - if row < self.rows && col < self.cols { - unsafe { Some(self.get_unchecked_mut(row, col)) } + pub fn get_mut( + &mut self, + row: impl TryInto, + col: impl TryInto, + ) -> Option<&mut T> { + let row_usize = row.try_into().ok()?; + let col_usize = col.try_into().ok()?; + if row_usize < self.rows && col_usize < self.cols { + unsafe { Some(self.get_unchecked_mut(row_usize, col_usize)) } } else { None } @@ -1526,6 +1538,59 @@ impl PartialEq for Grid { impl Eq for Grid {} +impl From>> for Grid { + #[allow(clippy::redundant_closure_for_method_calls)] + fn from(vec: Vec>) -> Self { + let cols = vec.first().map_or(0, |row| row.len()); + Self::from_vec_with_order(vec.into_iter().flatten().collect(), cols, Order::default()) + } +} + +impl From<&Vec>> for Grid { + #[allow(clippy::redundant_closure_for_method_calls)] + fn from(vec: &Vec>) -> Self { + let cols = vec.first().map_or(0, |row| row.len()); + Self::from_vec_with_order( + vec.clone().into_iter().flatten().collect(), + cols, + Order::default(), + ) + } +} + +impl From<&Vec<&Vec>> for Grid { + #[allow(clippy::redundant_closure_for_method_calls)] + fn from(vec: &Vec<&Vec>) -> Self { + let cols = vec.first().map_or(0, |row| row.len()); + Self::from_vec_with_order( + vec.clone() + .into_iter() + .flat_map(|inner| inner.clone()) + .collect(), + cols, + Order::default(), + ) + } +} + +impl From<(Vec, usize)> for Grid { + fn from(value: (Vec, usize)) -> Self { + Self::from_vec_with_order(value.0, value.1, Order::default()) + } +} + +impl From<(&Vec, usize)> for Grid { + fn from(value: (&Vec, usize)) -> Self { + Self::from_vec_with_order(value.0.clone(), value.1, Order::default()) + } +} + +impl From<(&Vec, &usize)> for Grid { + fn from(value: (&Vec, &usize)) -> Self { + Self::from_vec_with_order(value.0.clone(), *value.1, Order::default()) + } +} + #[derive(Clone)] pub struct GridRowIter<'a, T> { grid: &'a Grid, @@ -1587,6 +1652,100 @@ mod test { assert_eq!(grid.data, data, "internal data is unexpected"); } + #[test] + fn from_1d_vec() { + let grid: Grid = Grid::from((vec![1, 2, 3], 1)); + test_grid(&grid, 3, 1, Order::RowMajor, &[1, 2, 3]); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn from_1d_vec_panic() { + let _: Grid = Grid::from((vec![1, 2, 3], 2)); + } + + #[test] + fn from_1d_vec_reference() { + let vec = vec![1, 2, 3]; + let grid: Grid = Grid::from((&vec, 1)); + test_grid(&grid, 3, 1, Order::RowMajor, &[1, 2, 3]); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn from_1d_vec_reference_panic() { + let vec = vec![1, 2, 3]; + let _: Grid = Grid::from((&vec, 2)); + } + + #[test] + fn from_1d_vec_reference_and_reference() { + let vec = vec![1, 2, 3]; + let cols = 1; + let grid: Grid = Grid::from((&vec, &cols)); + test_grid(&grid, 3, 1, Order::RowMajor, &[1, 2, 3]); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn from_1d_vec_reference_and_reference_panic() { + let vec = vec![1, 2, 3]; + let cols = 2; + let _: Grid = Grid::from((&vec, &cols)); + } + + #[test] + fn from_2d_vec() { + let grid: Grid = Grid::from(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]); + test_grid(&grid, 3, 3, Order::RowMajor, &[1, 2, 3, 4, 5, 6, 7, 8, 9]); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn from_2d_vec_panic() { + let _: Grid = Grid::from(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8]]); + } + + #[test] + fn from_2d_vec_reference() { + let vec = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]; + let grid: Grid = Grid::from(&vec); + test_grid(&grid, 3, 3, Order::RowMajor, &[1, 2, 3, 4, 5, 6, 7, 8, 9]); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn from_2d_vec_reference_panic() { + let vec = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8]]; + let _: Grid = Grid::from(&vec); + } + + #[test] + fn from_2d_vec_reference_of_references() { + let inner_vec1 = vec![1, 2, 3]; + let inner_vec2 = vec![4, 5, 6]; + let inner_vec3 = vec![7, 8, 9]; + let vec = vec![&inner_vec1, &inner_vec2, &inner_vec3]; + let grid: Grid = Grid::from(&vec); + test_grid(&grid, 3, 3, Order::RowMajor, &[1, 2, 3, 4, 5, 6, 7, 8, 9]); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn from_2d_vec_reference_of_references_panic() { + let inner_vec1 = vec![1, 2, 3]; + let inner_vec2 = vec![4, 5, 6]; + let inner_vec3 = vec![7, 8]; + let vec = vec![&inner_vec1, &inner_vec2, &inner_vec3]; + let _: Grid = Grid::from(&vec); + } + #[test] fn from_vec_zero_with_cols() { let grid: Grid = Grid::from_vec(vec![], 1); @@ -1601,12 +1760,14 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn from_vec_panics_1() { let _: Grid = Grid::from_vec(vec![1, 2, 3], 0); } #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn from_vec_panics_2() { let _: Grid = Grid::from_vec(vec![1, 2, 3], 2); } @@ -1633,12 +1794,14 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn from_vec_with_order_panics_1() { let _: Grid = Grid::from_vec_with_order(vec![1, 2, 3], 0, Order::ColumnMajor); } #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn from_vec_with_order_panics_2() { let _: Grid = Grid::from_vec_with_order(vec![1, 2, 3], 2, Order::ColumnMajor); } @@ -1660,6 +1823,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn insert_col_out_of_idx() { let mut grid: Grid = Grid::from_vec_with_order(vec![1, 2, 3, 4], 2, Order::RowMajor); grid.insert_col(3, vec![4, 5]); @@ -1681,6 +1845,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn insert_col_out_of_idx_column_major() { let mut grid: Grid = Grid::from_vec_with_order(vec![1, 3, 2, 4], 2, Order::ColumnMajor); grid.insert_col(3, vec![4, 5]); @@ -1709,6 +1874,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn insert_row_out_of_idx() { let mut grid: Grid = Grid::from_vec_with_order(vec![1, 2, 3, 4], 2, Order::RowMajor); grid.insert_row(3, vec![4, 5]); @@ -1716,6 +1882,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn insert_row_wrong_size_of_idx() { let mut grid: Grid = Grid::from_vec_with_order(vec![1, 2, 3, 4], 2, Order::RowMajor); grid.insert_row(1, vec![4, 5, 4]); @@ -1744,6 +1911,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn insert_row_out_of_idx_column_major() { let mut grid: Grid = Grid::from_vec_with_order(vec![1, 2, 3, 4], 2, Order::ColumnMajor); grid.insert_row(3, vec![4, 5]); @@ -1751,6 +1919,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn insert_row_wrong_size_of_idx_column_major() { let mut grid: Grid = Grid::from_vec_with_order(vec![1, 2, 3, 4], 2, Order::ColumnMajor); grid.insert_row(1, vec![4, 5, 4]); @@ -1959,6 +2128,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn idx_tup_out_of_col_bounds() { let grid: Grid = grid![['a', 'b', 'c', 'd']['a', 'b', 'c', 'd']['a', 'b', 'c', 'd']]; let _ = grid[(0, 5)]; @@ -2002,6 +2172,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_col_wrong_size() { let mut grid: Grid = grid![['a','a','a']['a','a','a']]; grid.push_col(vec!['b']); @@ -2010,6 +2181,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_col_zero_len() { let mut grid: Grid = grid![]; grid.push_col(vec![]); @@ -2052,6 +2224,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_col_wrong_size_column_major() { let mut grid: Grid = Grid::init_with_order(2, 3, Order::ColumnMajor, 'a'); grid.push_col(vec!['b']); @@ -2060,6 +2233,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_col_zero_len_column_major() { let mut grid: Grid = Grid::new_with_order(0, 0, Order::ColumnMajor); grid.push_col(vec![]); @@ -2081,6 +2255,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_empty_row() { let mut grid = Grid::init(0, 1, 0); grid.push_row(vec![]); @@ -2088,6 +2263,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_row_wrong_size() { let mut grid: Grid = grid![['a','a','a']['a','a','a']]; grid.push_row(vec!['b']); @@ -2110,6 +2286,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_empty_row_column_major() { let mut grid = Grid::init_with_order(0, 1, Order::ColumnMajor, 0); grid.push_row(vec![]); @@ -2117,6 +2294,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn push_row_wrong_size_column_major() { let mut grid: Grid = Grid::from_vec_with_order(vec!['a', 'a', 'a', 'a', 'a', 'a'], 3, Order::ColumnMajor); @@ -2133,6 +2311,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_out_of_bound() { let grid = Grid::from_vec_with_order(vec![1, 2, 3, 4, 5, 6], 3, Order::RowMajor); let _ = grid.iter_row(3); @@ -2140,6 +2319,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_zero() { let grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::RowMajor); let _ = grid.iter_row(0); @@ -2154,6 +2334,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_rowumn_major_out_of_bound() { let grid = Grid::from_vec_with_order(vec![1, 4, 2, 5, 3, 6], 3, Order::ColumnMajor); let _ = grid.iter_row(3); @@ -2161,6 +2342,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_rowumn_major_zero() { let grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::ColumnMajor); let _ = grid.iter_row(0); @@ -2175,6 +2357,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_mut_out_of_bound() { let mut grid = Grid::from_vec_with_order(vec![1, 2, 3, 4, 5, 6], 3, Order::RowMajor); let _ = grid.iter_row_mut(3); @@ -2182,6 +2365,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_mut_zero() { let mut grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::RowMajor); let _ = grid.iter_row_mut(0); @@ -2196,6 +2380,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_mut_rowumn_major_out_of_bound() { let mut grid = Grid::from_vec_with_order(vec![1, 4, 2, 5, 3, 6], 3, Order::ColumnMajor); let _ = grid.iter_row_mut(3); @@ -2203,6 +2388,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_row_mut_rowumn_major_zero() { let mut grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::ColumnMajor); let _ = grid.iter_row_mut(0); @@ -2217,6 +2403,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_out_of_bound() { let grid = Grid::from_vec_with_order(vec![1, 2, 3, 4, 5, 6], 3, Order::RowMajor); let _ = grid.iter_col(3); @@ -2224,6 +2411,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_zero() { let grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::RowMajor); let _ = grid.iter_col(0); @@ -2238,6 +2426,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_column_major_out_of_bound() { let grid = Grid::from_vec_with_order(vec![1, 4, 2, 5, 3, 6], 3, Order::ColumnMajor); let _ = grid.iter_col(3); @@ -2245,6 +2434,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_column_major_zero() { let grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::ColumnMajor); let _ = grid.iter_col(0); @@ -2259,6 +2449,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_mut_out_of_bound() { let mut grid = Grid::from_vec_with_order(vec![1, 2, 3, 4, 5, 6], 3, Order::RowMajor); let _ = grid.iter_col_mut(3); @@ -2266,6 +2457,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_mut_zero() { let mut grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::RowMajor); let _ = grid.iter_col_mut(0); @@ -2280,6 +2472,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_mut_column_major_out_of_bound() { let mut grid = Grid::from_vec_with_order(vec![1, 4, 2, 5, 3, 6], 3, Order::ColumnMajor); let _ = grid.iter_col_mut(3); @@ -2287,6 +2480,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn iter_col_mut_column_major_zero() { let mut grid: Grid = Grid::from_vec_with_order(vec![], 0, Order::ColumnMajor); let _ = grid.iter_col_mut(0); @@ -2374,31 +2568,31 @@ mod test { #[test] fn fmt_empty() { let grid: Grid = grid![]; - assert_eq!(format!("{:?}", grid), "[]"); + assert_eq!(format!("{grid:?}"), "[]"); } #[test] fn fmt_row() { let grid: Grid = grid![[1, 2, 3]]; - assert_eq!(format!("{:?}", grid), "[[1, 2, 3]]"); + assert_eq!(format!("{grid:?}"), "[[1, 2, 3]]"); } #[test] fn fmt_grid() { let grid: Grid = grid![[1,2,3][4,5,6][7,8,9]]; - assert_eq!(format!("{:?}", grid), "[[1, 2, 3][4, 5, 6][7, 8, 9]]"); + assert_eq!(format!("{grid:?}"), "[[1, 2, 3][4, 5, 6][7, 8, 9]]"); } #[test] fn fmt_column_major() { let grid = Grid::from_vec_with_order(vec![1, 4, 2, 5, 3, 6], 3, Order::ColumnMajor); - assert_eq!(format!("{:?}", grid), "[[1, 2, 3][4, 5, 6]]"); + assert_eq!(format!("{grid:?}"), "[[1, 2, 3][4, 5, 6]]"); } #[test] fn fmt_pretty_empty() { let grid: Grid = grid![]; - assert_eq!(format!("{:#?}", grid), "[]"); + assert_eq!(format!("{grid:#?}"), "[]"); } #[test] @@ -2409,21 +2603,21 @@ mod test { [7,8,95] ]; - let expected_output = r#"[ + let expected_output = r"[ [ 1, 2, 3] [ 4, 5, 6] [ 7, 8, 95] -]"#; +]"; - assert_eq!(format!("{:#?}", grid), expected_output); + assert_eq!(format!("{grid:#?}"), expected_output); - let expected_output = r#"[ + let expected_output = r"[ [ 1, 2, 3] [ 4, 5, 6] [ 7, 8, 95] -]"#; +]"; - assert_eq!(format!("{:#3?}", grid), expected_output); + assert_eq!(format!("{grid:#3?}"), expected_output); } #[test] @@ -2434,21 +2628,21 @@ mod test { [7.1,8.23444,95.55] ]; - let expected_output = r#"[ + let expected_output = r"[ [ 1.5, 2.6, 3.4] [ 4.8, 5.0, 6.0] [ 7.1, 8.2, 95.6] -]"#; +]"; - assert_eq!(format!("{:#5.1?}", grid), expected_output); + assert_eq!(format!("{grid:#5.1?}"), expected_output); - let expected_output = r#"[ + let expected_output = r"[ [ 1.50000, 2.60000, 3.44000] [ 4.77500, 5.00000, 6.00000] [ 7.10000, 8.23444, 95.55000] -]"#; +]"; - assert_eq!(format!("{:#8.5?}", grid), expected_output); + assert_eq!(format!("{grid:#8.5?}"), expected_output); } #[test] @@ -2458,19 +2652,19 @@ mod test { [(80, 90), (5, 6)] ]; - let expected_output = r#"[ + let expected_output = r"[ [ ( 5, 66), ( 432, 55)] [ ( 80, 90), ( 5, 6)] -]"#; +]"; assert_eq!(format!("{grid:#?}"), expected_output); - let expected_output = r#"[ + let expected_output = r"[ [ ( 5, 66), (432, 55)] [ ( 80, 90), ( 5, 6)] -]"#; +]"; - assert_eq!(format!("{:#3?}", grid), expected_output); + assert_eq!(format!("{grid:#3?}"), expected_output); } #[test] @@ -2500,17 +2694,17 @@ mod test { [ Person { _name: "Sam", _precise_age: 8.99950 }, Person { _name: "John Doe", _precise_age: 40.14000 }] ]"#; - assert_eq!(format!("{:#5.5?}", grid), expected_output); + assert_eq!(format!("{grid:#5.5?}"), expected_output); } #[test] fn fmt_pretty_column_major() { let grid = Grid::from_vec_with_order(vec![1, 4, 2, 5, 3, 6], 3, Order::ColumnMajor); - let expected_output = r#"[ + let expected_output = r"[ [ 1, 2, 3] [ 4, 5, 6] -]"#; - assert_eq!(format!("{:#?}", grid), expected_output); +]"; + assert_eq!(format!("{grid:#?}"), expected_output); } #[test] @@ -2602,6 +2796,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn init_panics() { Grid::init(usize::MAX, 2, 3); } @@ -2629,6 +2824,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn init_with_order_panics() { Grid::init_with_order(usize::MAX, 2, Order::ColumnMajor, 3); } @@ -2650,6 +2846,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn new_panics() { let _: Grid = Grid::new(usize::MAX, 2); } @@ -2671,6 +2868,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn new_with_order_panics() { let _: Grid = Grid::new_with_order(usize::MAX, 2, Order::ColumnMajor); } @@ -2687,7 +2885,7 @@ mod test { #[test] fn get() { let grid = Grid::from_vec_with_order(vec![1, 2], 2, Order::RowMajor); - assert_eq!(grid.get(0, 1), Some(&2)); + assert_eq!(grid.get(0_i64, 1_i32), Some(&2)); } #[test] @@ -2711,7 +2909,7 @@ mod test { #[test] fn get_mut() { let mut grid = Grid::from_vec_with_order(vec![1, 2], 2, Order::RowMajor); - assert_eq!(grid.get_mut(0, 1), Some(&mut 2)); + assert_eq!(grid.get_mut(0_i64, 1_i32), Some(&mut 2)); } #[test] @@ -2743,6 +2941,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn idx_tup_panic_1() { let grid = Grid::init(1, 2, 3); let _ = grid[(20, 0)]; @@ -2750,6 +2949,7 @@ mod test { #[test] #[should_panic] + #[allow(clippy::should_panic_without_expect)] fn idx_tup_panic_2() { let grid = Grid::init(1, 2, 3); let _ = grid[(0, 20)]; @@ -2790,6 +2990,7 @@ mod test { } #[test] + #[allow(clippy::redundant_closure_for_method_calls)] fn iter_rows() { let grid: Grid = grid![[1,2,3][4,5,6]]; let max_by_row: Vec = grid @@ -2804,6 +3005,7 @@ mod test { } #[test] + #[allow(clippy::redundant_closure_for_method_calls)] fn iter_cols() { let grid: Grid = grid![[1,2,3][4,5,6]]; let max_by_col: Vec = grid @@ -2814,7 +3016,7 @@ mod test { assert_eq!(max_by_col, vec![4, 5, 6]); - let sum_by_col: Vec = grid.iter_cols().map(|col| col.sum()).collect(); + let sum_by_col: Vec = grid.iter_cols().map(|row| row.sum()).collect(); assert_eq!(sum_by_col, vec![1 + 4, 2 + 5, 3 + 6]); }