From 47dc3795a3c57a0c26dad824caba679e71046e83 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 4 Mar 2023 16:53:53 +0100 Subject: [PATCH] world: introduce SectionIterItem struct --- src/world/chunk.rs | 26 +++++++++++++++----------- src/world/layer.rs | 8 ++++++-- src/world/section.rs | 4 +++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/world/chunk.rs b/src/world/chunk.rs index 3f37210..72c5f9a 100644 --- a/src/world/chunk.rs +++ b/src/world/chunk.rs @@ -182,16 +182,19 @@ impl<'a> Chunk<'a> { } } +#[derive(Debug, Clone, Copy)] +pub struct SectionIterItem<'a> { + pub y: SectionY, + pub section: &'a dyn Section, +} + trait SectionIterTrait<'a>: - Iterator - + DoubleEndedIterator - + ExactSizeIterator - + FusedIterator + Iterator> + DoubleEndedIterator + ExactSizeIterator + FusedIterator { } impl<'a, T> SectionIterTrait<'a> for T where - T: Iterator + T: Iterator> + DoubleEndedIterator + ExactSizeIterator + FusedIterator @@ -204,14 +207,15 @@ impl<'a> SectionIter<'a> { F: FnOnce(&mut dyn SectionIterTrait<'a>) -> T, { match &mut self.inner { - SectionIterInner::V1_18 { iter } => f( - &mut iter.map(|(y, section)| -> (SectionY, &'a dyn Section) { (*y, §ion.0) }) - ), + SectionIterInner::V1_18 { iter } => f(&mut iter.map(|(&y, section)| SectionIterItem { + y, + section: §ion.0, + })), SectionIterInner::V1_13 { iter } => { - f(&mut iter.map(|(y, section)| -> (SectionY, &'a dyn Section) { (*y, section) })) + f(&mut iter.map(|(&y, section)| SectionIterItem { y, section })) } SectionIterInner::V0 { iter } => { - f(&mut iter.map(|(y, section)| -> (SectionY, &'a dyn Section) { (*y, section) })) + f(&mut iter.map(|(&y, section)| SectionIterItem { y, section })) } SectionIterInner::Empty => f(&mut iter::empty()), } @@ -219,7 +223,7 @@ impl<'a> SectionIter<'a> { } impl<'a> Iterator for SectionIter<'a> { - type Item = (SectionY, &'a dyn Section); + type Item = SectionIterItem<'a>; fn next(&mut self) -> Option { self.with_iter(|iter| iter.next()) diff --git a/src/world/layer.rs b/src/world/layer.rs index 2f9382c..5541e34 100644 --- a/src/world/layer.rs +++ b/src/world/layer.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; -use super::chunk::Chunk; +use super::chunk::{Chunk, SectionIterItem}; use crate::{ resource::{BlockFlag, BlockType}, types::*, @@ -97,7 +97,11 @@ pub fn top_layer(chunk: &Chunk) -> Result> { let mut done = 0; let mut ret = Box::::default(); - for (section_y, section) in chunk.sections().rev() { + for SectionIterItem { + y: section_y, + section, + } in chunk.sections().rev() + { for y in BlockY::iter().rev() { for xz in BlockInfoArray::keys() { let entry = &mut ret[xz]; diff --git a/src/world/section.rs b/src/world/section.rs index 819bd3f..8a7a8d7 100644 --- a/src/world/section.rs +++ b/src/world/section.rs @@ -1,3 +1,5 @@ +use std::fmt::Debug; + use anyhow::{bail, Context, Result}; use num_integer::div_rem; @@ -26,7 +28,7 @@ fn palette_bits(len: usize, min: u8, max: u8) -> Option { } /// Trait for common functions of [SectionV1_13] and [SectionV0] -pub trait Section { +pub trait Section: Debug { fn block_at(&self, coords: SectionBlockCoords) -> Result>; }