Add debug
This commit is contained in:
parent
ae329ef765
commit
9d77a6d4fd
1 changed files with 32 additions and 0 deletions
32
src/lib.rs
32
src/lib.rs
|
@ -1,3 +1,4 @@
|
|||
use std::fmt;
|
||||
use std::ops::Index;
|
||||
use std::ops::IndexMut;
|
||||
|
||||
|
@ -202,10 +203,41 @@ impl<T: Clone> IndexMut<usize> for Grid<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for Grid<T> {
|
||||
#[allow(unused_must_use)]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "[");
|
||||
if self.cols > 0 {
|
||||
for (i, _) in self.data.iter().enumerate().step_by(self.cols) {
|
||||
write!(f, "{:?}", &self.data[i..(i + self.cols)]);
|
||||
}
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn fmt_empty() {
|
||||
let grid: Grid<u8> = grid![];
|
||||
assert_eq!(format!("{:?}", grid), "[]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fmt_row() {
|
||||
let grid: Grid<u8> = grid![[1, 2, 3]];
|
||||
assert_eq!(format!("{:?}", grid), "[[1, 2, 3]]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fmt_grid() {
|
||||
let grid: Grid<u8> = 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 clone() {
|
||||
let grid = grid![[1, 2, 3][4, 5, 6]];
|
||||
|
|
Loading…
Reference in a new issue