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

View file

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

View file

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