use std::{ fmt::{Debug, Write}, ops::Deref, }; #[derive(Clone, PartialEq, Eq)] pub struct Array2D { width: usize, height: usize, /// column data: Box<[T]>, } impl Debug for Array2D> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("Arr[\n")?; for y in (0..self.height).rev() { for x in 0..self.width { let t = &self[x][y]; if let Some(t) = t { t.fmt(f)?; } else { f.write_char('_')?; } f.write_str(", ")?; } f.write_char('\n')?; } f.write_char(']')?; Ok(()) } } impl Array2D { pub fn new(fill: T, width: usize, height: usize) -> Array2D { Array2D { width, height, data: vec![fill; width * height].into_boxed_slice(), } } } impl Deref for Array2D { type Target = Box<[T]>; /// a sin it is fn deref(&self) -> &Self::Target { &self.data } } impl std::ops::Index for Array2D { type Output = [T]; fn index(&self, x: usize) -> &Self::Output { &self.data[self.height * x..self.height * (x + 1)] } } impl std::ops::IndexMut for Array2D { fn index_mut(&mut self, x: usize) -> &mut Self::Output { &mut self.data[self.height * x..self.height * (x + 1)] } }