types: introduce LayerBlockArray

A LayerBlockArray is a generic array of data stored for each block of a
chunk layer. It provides the same methods as a ChunkArray.
This commit is contained in:
Matthias Schiffer 2023-02-15 00:38:12 +01:00
parent 789a4002c7
commit 3ef48783b7
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C

View file

@ -52,6 +52,40 @@ impl Debug for LayerBlockCoords {
}
}
/// Generic array for data stored per block of a chunk layer
///
/// Includes various convenient iteration functions.
#[derive(Debug, Clone, Copy, Default)]
pub struct LayerBlockArray<T>(pub [[T; BLOCKS_PER_CHUNK]; BLOCKS_PER_CHUNK]);
impl<T> LayerBlockArray<T> {
pub fn keys() -> impl Iterator<Item = LayerBlockCoords> + Clone + Debug {
iproduct!(BlockZ::iter(), BlockX::iter()).map(|(z, x)| LayerBlockCoords { x, z })
}
pub fn values(&self) -> impl Iterator<Item = &T> + Clone + Debug {
Self::keys().map(|k| &self[k])
}
pub fn iter(&self) -> impl Iterator<Item = (LayerBlockCoords, &T)> + Clone + Debug {
Self::keys().map(|k| (k, &self[k]))
}
}
impl<T> Index<LayerBlockCoords> for LayerBlockArray<T> {
type Output = T;
fn index(&self, index: LayerBlockCoords) -> &Self::Output {
&self.0[index.z.0 as usize][index.x.0 as usize]
}
}
impl<T> IndexMut<LayerBlockCoords> for LayerBlockArray<T> {
fn index_mut(&mut self, index: LayerBlockCoords) -> &mut Self::Output {
&mut self.0[index.z.0 as usize][index.x.0 as usize]
}
}
/// X, Y and Z coordinates of a block in a chunk section
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct SectionBlockCoords {