diff --git a/src/lib.rs b/src/lib.rs index 85a9817..c597040 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -412,6 +412,30 @@ impl Grid { } } + /// Initialises an empty Grid with the capacity to store `cols * rows` elements. + /// Similar to `Vec::with_capacity`. + /// + /// # Panics + /// + /// Panics if `rows * cols > usize::MAX` or if `rows * cols * size_of::() > isize::MAX` + pub fn with_capacity(rows: usize, cols: usize) -> Self { + Self::with_capacity_and_order(rows, cols, Order::default()) + } + + /// Same as [`with_capacity`](Self::with_capacity) but with a specified [`Order`] + /// + /// # Panics + /// + /// Panics if `rows * cols > usize::MAX` or if `rows * cols * size_of::() > isize::MAX` + pub fn with_capacity_and_order(rows: usize, cols: usize, order: Order) -> Self { + Self { + data: Vec::with_capacity(rows.checked_mul(cols).unwrap()), + cols: 0, + rows: 0, + order, + } + } + /// Returns a grid from a vector with a given column length. /// The length of `vec` must be a multiple of `cols`. /// @@ -3023,6 +3047,49 @@ mod test { test_grid(&grid, 0, 0, Order::ColumnMajor, &[]); } + #[test] + fn with_capacity() { + // doesn't impl Default + struct Foo(); + + let grid: Grid = Grid::with_capacity(20, 20); + assert!(grid.is_empty()); + assert_eq!(grid.order(), Order::default()); + } + + #[test] + fn with_capacity_and_order() { + // doesn't impl Default + struct Foo(); + + let grid: Grid = Grid::with_capacity_and_order(20, 20, Order::ColumnMajor); + assert!(grid.is_empty()); + assert_eq!(grid.order(), Order::ColumnMajor); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn with_capacity_panics_internal() { + // doesn't impl Default + struct Foo(); + + let _grid: Grid = Grid::with_capacity_and_order(usize::MAX, 2, Order::RowMajor); + } + + #[test] + #[should_panic] + #[allow(clippy::should_panic_without_expect)] + fn with_capacity_panics_vec() { + let rows: usize = isize::MAX.try_into().expect("isize::MAX is positive"); + assert!( + core::mem::size_of::() * rows < usize::MAX, + "shows that panic is from Vec::with_capacity, not internal check" + ); + + let _grid: Grid = Grid::with_capacity_and_order(rows, 2, Order::RowMajor); + } + #[test] fn get() { let grid = Grid::from_vec_with_order(vec![1, 2], 2, Order::RowMajor);