1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 15:36:03 +00:00

convert ParsingStackElement from an enum to a struct

This commit is contained in:
mat 2025-01-18 20:44:14 +00:00
parent 1a4fa2d7ee
commit dd45102d75
3 changed files with 51 additions and 45 deletions

View file

@ -1,4 +1,4 @@
use std::{hint::unreachable_unchecked, mem::MaybeUninit}; use std::mem::MaybeUninit;
use crate::{ use crate::{
common::{ common::{
@ -34,9 +34,9 @@ impl<'a: 'tape, 'tape> NbtCompound<'a, 'tape> {
) -> Result<(), NonRootError> { ) -> Result<(), NonRootError> {
let index_of_compound_element = tapes.main.len(); let index_of_compound_element = tapes.main.len();
stack.push(ParsingStackElement::Compound { stack.push(ParsingStackElement::compound(
index_of_compound_element: index_of_compound_element as u32, index_of_compound_element as u32,
})?; ))?;
tapes.main.push(TapeElement::new_with_approx_len_and_offset( tapes.main.push(TapeElement::new_with_approx_len_and_offset(
TapeTagKind::Compound, TapeTagKind::Compound,
// these get overwritten later // these get overwritten later
@ -233,10 +233,36 @@ impl<'a: 'tape, 'tape> Iterator for NbtCompoundIter<'a, 'tape> {
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub(crate) enum ParsingStackElement { pub(crate) struct ParsingStackElement {
Compound { index_of_compound_element: u32 }, pub kind: ParsingStackElementKind,
ListOfCompounds { index_of_list_element: u32 }, pub index: u32,
ListOfLists { index_of_list_element: u32 }, }
#[derive(Debug, Copy, Clone)]
#[repr(u32)]
pub(crate) enum ParsingStackElementKind {
Compound,
ListOfCompounds,
ListOfLists,
}
impl ParsingStackElement {
pub fn compound(index_of_compound_element: u32) -> Self {
Self {
kind: ParsingStackElementKind::Compound,
index: index_of_compound_element,
}
}
pub fn list_of_compounds(index_of_list_element: u32) -> Self {
Self {
kind: ParsingStackElementKind::ListOfCompounds,
index: index_of_list_element,
}
}
pub fn list_of_lists(index_of_list_element: u32) -> Self {
Self {
kind: ParsingStackElementKind::ListOfLists,
index: index_of_list_element,
}
}
} }
pub struct ParsingStack { pub struct ParsingStack {
@ -411,12 +437,7 @@ pub(crate) fn read_tag_in_compound<'a>(
#[inline(always)] #[inline(always)]
fn handle_compound_end(tapes: &mut Tapes, stack: &mut ParsingStack) { fn handle_compound_end(tapes: &mut Tapes, stack: &mut ParsingStack) {
let ParsingStackElement::Compound { let index_of_compound_element = stack.pop().index;
index_of_compound_element,
} = stack.pop()
else {
unsafe { unreachable_unchecked() };
};
let index_after_end_element = tapes.main.len(); let index_after_end_element = tapes.main.len();
unsafe { unsafe {

View file

@ -1,4 +1,4 @@
use std::{hint::unreachable_unchecked, marker::PhantomData, mem}; use std::{marker::PhantomData, mem};
use crate::{ use crate::{
common::{ common::{
@ -99,9 +99,9 @@ impl<'a, 'tape> NbtList<'a, 'tape> {
// length estimate + tape index offset to the end of the list // length estimate + tape index offset to the end of the list
let index_of_list_element = tapes.main.len(); let index_of_list_element = tapes.main.len();
stack.push(ParsingStackElement::ListOfLists { stack.push(ParsingStackElement::list_of_lists(
index_of_list_element: index_of_list_element as u32, index_of_list_element as u32,
})?; ))?;
stack.set_list_length(length); stack.set_list_length(length);
TapeElement::new_with_approx_len_and_offset( TapeElement::new_with_approx_len_and_offset(
TapeTagKind::ListList, TapeTagKind::ListList,
@ -115,9 +115,9 @@ impl<'a, 'tape> NbtList<'a, 'tape> {
// length estimate + tape index offset to the end of the compound // length estimate + tape index offset to the end of the compound
let index_of_list_element = tapes.main.len(); let index_of_list_element = tapes.main.len();
stack.push(ParsingStackElement::ListOfCompounds { stack.push(ParsingStackElement::list_of_compounds(
index_of_list_element: index_of_list_element as u32, index_of_list_element as u32,
})?; ))?;
stack.set_list_length(length); stack.set_list_length(length);
TapeElement::new_with_approx_len_and_offset( TapeElement::new_with_approx_len_and_offset(
TapeTagKind::CompoundList, TapeTagKind::CompoundList,
@ -831,12 +831,7 @@ pub fn read_list_in_list<'a>(
tapes: &mut Tapes<'a>, tapes: &mut Tapes<'a>,
stack: &mut ParsingStack, stack: &mut ParsingStack,
) -> Result<(), NonRootError> { ) -> Result<(), NonRootError> {
let ParsingStackElement::ListOfLists { let index_of_list_element = stack.peek().index;
index_of_list_element,
} = stack.peek()
else {
unsafe { unreachable_unchecked() };
};
let remaining = stack.remaining_elements_in_list(); let remaining = stack.remaining_elements_in_list();
@ -864,12 +859,7 @@ pub(crate) fn read_compound_in_list<'a>(
tapes: &mut Tapes<'a>, tapes: &mut Tapes<'a>,
stack: &mut ParsingStack, stack: &mut ParsingStack,
) -> Result<(), NonRootError> { ) -> Result<(), NonRootError> {
let ParsingStackElement::ListOfCompounds { let index_of_list_element = stack.peek().index;
index_of_list_element,
} = stack.peek()
else {
unsafe { unreachable_unchecked() };
};
let remaining = stack.remaining_elements_in_list(); let remaining = stack.remaining_elements_in_list();

View file

@ -11,6 +11,7 @@ use std::{
}; };
use byteorder::ReadBytesExt; use byteorder::ReadBytesExt;
use compound::ParsingStackElementKind;
use tape::{UnalignedU16, UnalignedU32, UnalignedU64}; use tape::{UnalignedU16, UnalignedU32, UnalignedU64};
use crate::{ use crate::{
@ -56,9 +57,7 @@ pub fn read<'a>(data: &mut Cursor<&'a [u8]>) -> Result<Nbt<'a>, Error> {
)); ));
let mut stack = ParsingStack::new(); let mut stack = ParsingStack::new();
stack.push(ParsingStackElement::Compound { stack.push(ParsingStackElement::compound(0))?;
index_of_compound_element: 0,
})?;
read_with_stack(&mut data, &mut tapes, &mut stack)?; read_with_stack(&mut data, &mut tapes, &mut stack)?;
@ -88,9 +87,7 @@ pub fn read_compound<'a>(data: &mut Cursor<&'a [u8]>) -> Result<BaseNbtCompound<
let mut data = ReaderFromCursor::new(data); let mut data = ReaderFromCursor::new(data);
stack.push(ParsingStackElement::Compound { stack.push(ParsingStackElement::compound(0))?;
index_of_compound_element: 0,
})?;
tapes.main.push(TapeElement::new_with_approx_len_and_offset( tapes.main.push(TapeElement::new_with_approx_len_and_offset(
TapeTagKind::Compound, TapeTagKind::Compound,
@ -146,12 +143,10 @@ fn read_with_stack<'a>(
) -> Result<(), Error> { ) -> Result<(), Error> {
while !stack.is_empty() { while !stack.is_empty() {
let top = stack.peek_mut(); let top = stack.peek_mut();
match top { match top.kind {
ParsingStackElement::Compound { .. } => read_tag_in_compound(data, tapes, stack)?, ParsingStackElementKind::Compound => read_tag_in_compound(data, tapes, stack)?,
ParsingStackElement::ListOfCompounds { .. } => { ParsingStackElementKind::ListOfCompounds => read_compound_in_list(data, tapes, stack)?,
read_compound_in_list(data, tapes, stack)? ParsingStackElementKind::ListOfLists => read_list_in_list(data, tapes, stack)?,
}
ParsingStackElement::ListOfLists { .. } => read_list_in_list(data, tapes, stack)?,
} }
} }