diff --git a/simdnbt/src/borrow/compound.rs b/simdnbt/src/borrow/compound.rs index 8f42ed9..229d53e 100644 --- a/simdnbt/src/borrow/compound.rs +++ b/simdnbt/src/borrow/compound.rs @@ -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 { diff --git a/simdnbt/src/borrow/list.rs b/simdnbt/src/borrow/list.rs index cab9256..3b9207a 100644 --- a/simdnbt/src/borrow/list.rs +++ b/simdnbt/src/borrow/list.rs @@ -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(); diff --git a/simdnbt/src/borrow/mod.rs b/simdnbt/src/borrow/mod.rs index eb42410..024b0e4 100644 --- a/simdnbt/src/borrow/mod.rs +++ b/simdnbt/src/borrow/mod.rs @@ -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, 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( ) -> 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)?, } }