world: introduce SectionIterItem struct

This commit is contained in:
Matthias Schiffer 2023-03-04 16:53:53 +01:00
parent ed422be451
commit 47dc3795a3
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 24 additions and 14 deletions

View file

@ -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>: trait SectionIterTrait<'a>:
Iterator<Item = (SectionY, &'a dyn Section)> Iterator<Item = SectionIterItem<'a>> + DoubleEndedIterator + ExactSizeIterator + FusedIterator
+ DoubleEndedIterator
+ ExactSizeIterator
+ FusedIterator
{ {
} }
impl<'a, T> SectionIterTrait<'a> for T where impl<'a, T> SectionIterTrait<'a> for T where
T: Iterator<Item = (SectionY, &'a dyn Section)> T: Iterator<Item = SectionIterItem<'a>>
+ DoubleEndedIterator + DoubleEndedIterator
+ ExactSizeIterator + ExactSizeIterator
+ FusedIterator + FusedIterator
@ -204,14 +207,15 @@ impl<'a> SectionIter<'a> {
F: FnOnce(&mut dyn SectionIterTrait<'a>) -> T, F: FnOnce(&mut dyn SectionIterTrait<'a>) -> T,
{ {
match &mut self.inner { match &mut self.inner {
SectionIterInner::V1_18 { iter } => f( SectionIterInner::V1_18 { iter } => f(&mut iter.map(|(&y, section)| SectionIterItem {
&mut iter.map(|(y, section)| -> (SectionY, &'a dyn Section) { (*y, &section.0) }) y,
), section: &section.0,
})),
SectionIterInner::V1_13 { iter } => { 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 } => { 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()), SectionIterInner::Empty => f(&mut iter::empty()),
} }
@ -219,7 +223,7 @@ impl<'a> SectionIter<'a> {
} }
impl<'a> Iterator for SectionIter<'a> { impl<'a> Iterator for SectionIter<'a> {
type Item = (SectionY, &'a dyn Section); type Item = SectionIterItem<'a>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.with_iter(|iter| iter.next()) self.with_iter(|iter| iter.next())

View file

@ -1,7 +1,7 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::chunk::Chunk; use super::chunk::{Chunk, SectionIterItem};
use crate::{ use crate::{
resource::{BlockFlag, BlockType}, resource::{BlockFlag, BlockType},
types::*, types::*,
@ -97,7 +97,11 @@ pub fn top_layer(chunk: &Chunk) -> Result<Box<BlockInfoArray>> {
let mut done = 0; let mut done = 0;
let mut ret = Box::<BlockInfoArray>::default(); let mut ret = Box::<BlockInfoArray>::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 y in BlockY::iter().rev() {
for xz in BlockInfoArray::keys() { for xz in BlockInfoArray::keys() {
let entry = &mut ret[xz]; let entry = &mut ret[xz];

View file

@ -1,3 +1,5 @@
use std::fmt::Debug;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use num_integer::div_rem; use num_integer::div_rem;
@ -26,7 +28,7 @@ fn palette_bits(len: usize, min: u8, max: u8) -> Option<u8> {
} }
/// Trait for common functions of [SectionV1_13] and [SectionV0] /// Trait for common functions of [SectionV1_13] and [SectionV0]
pub trait Section { pub trait Section: Debug {
fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>>; fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>>;
} }