Fix #13 support init zero sized grids
This commit is contained in:
parent
4877def932
commit
164fbeac5c
1 changed files with 29 additions and 9 deletions
38
src/lib.rs
38
src/lib.rs
|
@ -127,17 +127,24 @@ pub struct Grid<T> {
|
||||||
impl<T> Grid<T> {
|
impl<T> Grid<T> {
|
||||||
/// Init a grid of size rows x columns with default values of the given type.
|
/// 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:
|
/// For example this will generate a 2x3 grid of zeros:
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use grid::Grid;
|
/// use grid::Grid;
|
||||||
/// let grid : Grid<u8> = Grid::new(2,2);
|
/// let grid : Grid<u8> = Grid::new(2,2);
|
||||||
/// assert_eq!(grid[0][0], 0);
|
/// assert_eq!(grid[0][0], 0);
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// If `rows == 0` or `cols == 0` the grid will be empty with no cols and rows.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if `rows * cols > usize`.
|
||||||
pub fn new(rows: usize, cols: usize) -> Grid<T>
|
pub fn new(rows: usize, cols: usize) -> Grid<T>
|
||||||
where
|
where
|
||||||
T: Default,
|
T: Default,
|
||||||
{
|
{
|
||||||
if rows < 1 || cols < 1 {
|
if rows == 0 || cols == 0 {
|
||||||
panic!("Grid size of rows and columns must be greater than zero.");
|
return Grid { data: Vec::new(), rows:0, cols: 0 }
|
||||||
}
|
}
|
||||||
let mut data = Vec::new();
|
let mut data = Vec::new();
|
||||||
data.resize_with(rows * cols, T::default);
|
data.resize_with(rows * cols, T::default);
|
||||||
|
@ -145,13 +152,16 @@ impl<T> Grid<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Init a grid of size rows x columns with the given data element.
|
/// Init a grid of size rows x columns with the given data element.
|
||||||
|
///
|
||||||
|
/// If `rows == 0` or `cols == 0` the grid will be empty with no cols and rows.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if `rows * cols > usize`.
|
||||||
pub fn init(rows: usize, cols: usize, data: T) -> Grid<T>
|
pub fn init(rows: usize, cols: usize, data: T) -> Grid<T>
|
||||||
where
|
where
|
||||||
T: Clone,
|
T: Clone,
|
||||||
{
|
{
|
||||||
if rows < 1 || cols < 1 {
|
|
||||||
panic!("Grid size of rows and columns must be greater than zero.");
|
|
||||||
}
|
|
||||||
Grid {
|
Grid {
|
||||||
data: vec![data; rows * cols],
|
data: vec![data; rows * cols],
|
||||||
cols,
|
cols,
|
||||||
|
@ -1060,6 +1070,15 @@ mod test {
|
||||||
assert!(g.is_empty());
|
assert!(g.is_empty());
|
||||||
g = Grid::from_vec(vec![], 0);
|
g = Grid::from_vec(vec![], 0);
|
||||||
assert!(g.is_empty());
|
assert!(g.is_empty());
|
||||||
|
g = Grid::new(0, 0);
|
||||||
|
assert!(g.is_empty());
|
||||||
|
g = Grid::new(0, 1);
|
||||||
|
assert!(g.is_empty());
|
||||||
|
g = Grid::new(1,0);
|
||||||
|
assert!(g.is_empty());
|
||||||
|
g = Grid::init(0, 0,10);
|
||||||
|
assert!(g.is_empty());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1159,16 +1178,17 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn init_panics() {
|
fn new_panics() {
|
||||||
Grid::init(0, 2, 3);
|
let _ : Grid<u8>= Grid::new(usize::MAX, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn ctr_panics_2() {
|
fn init_panics() {
|
||||||
Grid::init(1, 0, 3);
|
Grid::init(usize::MAX, 2, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get() {
|
fn get() {
|
||||||
let grid = Grid::init(1, 2, 3);
|
let grid = Grid::init(1, 2, 3);
|
||||||
|
|
Loading…
Reference in a new issue