diffus/
twodvec.rs

1pub(crate) struct TwoDVec<T> {
2    storage: Vec<T>,
3    width: usize,
4}
5
6impl<T: Clone> TwoDVec<T> {
7    pub fn new(initial: T, width: usize, height: usize) -> Self {
8        Self {
9            storage: vec![initial; width * height],
10            width,
11        }
12    }
13}
14
15impl<T> TwoDVec<T> {
16    pub fn height(&self) -> usize {
17        self.storage.len() / self.width
18    }
19    pub fn width(&self) -> usize {
20        self.width
21    }
22}
23
24impl<T> std::ops::Index<usize> for TwoDVec<T> {
25    type Output = [T];
26
27    fn index(&self, index: usize) -> &Self::Output {
28        &self.storage.as_slice()[self.width * index..][..self.width]
29    }
30}
31
32impl<T> std::ops::IndexMut<usize> for TwoDVec<T> {
33    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
34        &mut self.storage.as_mut_slice()[self.width * index..][..self.width]
35    }
36}