From 56573640fd8873712c4a15a505a9e45908e1db87 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 4 Mar 2023 00:27:26 +0100 Subject: [PATCH] world/section: prefer slice references to Vec and fastnbt types Makes the code a bit nicer and saves repeated deref calls into fastnbt. --- src/world/chunk.rs | 4 ++-- src/world/section.rs | 22 +++++++++------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/world/chunk.rs b/src/world/chunk.rs index 45b3476..3f37210 100644 --- a/src/world/chunk.rs +++ b/src/world/chunk.rs @@ -92,12 +92,12 @@ impl<'a> Chunk<'a> { ( SectionV1_13::new( data_version, - section.block_states.data.as_ref(), + section.block_states.data.as_deref(), §ion.block_states.palette, block_types, ) .with_context(|| format!("Failed to load section at Y={}", section.y))?, - BiomesV18::new(section.biomes.data.as_ref(), §ion.biomes.palette) + BiomesV18::new(section.biomes.data.as_deref(), §ion.biomes.palette) .with_context(|| { format!("Failed to load section biomes at Y={}", section.y) })?, diff --git a/src/world/section.rs b/src/world/section.rs index 8c98a68..819bd3f 100644 --- a/src/world/section.rs +++ b/src/world/section.rs @@ -37,14 +37,14 @@ pub trait Section { /// v1.13+ block data. #[derive(Debug)] pub struct BiomesV18<'a> { - _biomes: Option<&'a fastnbt::LongArray>, - _palette: &'a Vec, + _biomes: Option<&'a [i64]>, + _palette: &'a [String], _bits: u8, } impl<'a> BiomesV18<'a> { /// Constructs a new [BiomesV18] from deserialized data structures - pub fn new(biomes: Option<&'a fastnbt::LongArray>, palette: &'a Vec) -> Result { + pub fn new(biomes: Option<&'a [i64]>, palette: &'a [String]) -> Result { let bits = palette_bits(palette.len(), 1, 6).context("Unsupported block palette size")?; if let Some(biomes) = biomes { @@ -66,7 +66,7 @@ impl<'a> BiomesV18<'a> { /// Minecraft v1.13+ section block data #[derive(Debug)] pub struct SectionV1_13<'a> { - block_states: Option<&'a fastnbt::LongArray>, + block_states: Option<&'a [i64]>, palette: Vec>, bits: u8, aligned_blocks: bool, @@ -76,8 +76,8 @@ impl<'a> SectionV1_13<'a> { /// Constructs a new [SectionV1_13] from deserialized data structures pub fn new( data_version: u32, - block_states: Option<&'a fastnbt::LongArray>, - palette: &'a Vec, + block_states: Option<&'a [i64]>, + palette: &'a [de::BlockStatePaletteEntry], block_types: &'a BlockTypes, ) -> Result { let aligned_blocks = data_version >= 2529; @@ -158,18 +158,14 @@ impl<'a> Section for SectionV1_13<'a> { /// Pre-1.13 section block data #[derive(Debug)] pub struct SectionV0<'a> { - blocks: &'a fastnbt::ByteArray, - data: &'a fastnbt::ByteArray, + blocks: &'a [i8], + data: &'a [i8], block_types: &'a BlockTypes, } impl<'a> SectionV0<'a> { /// Constructs a new [SectionV0] from deserialized data structures - pub fn new( - blocks: &'a fastnbt::ByteArray, - data: &'a fastnbt::ByteArray, - block_types: &'a BlockTypes, - ) -> Result { + pub fn new(blocks: &'a [i8], data: &'a [i8], block_types: &'a BlockTypes) -> Result { use BLOCKS_PER_CHUNK as N; if blocks.len() != N * N * N {