diff --git a/simdnbt/src/borrow/mod.rs b/simdnbt/src/borrow/mod.rs index a49d1bc..950b1c2 100644 --- a/simdnbt/src/borrow/mod.rs +++ b/simdnbt/src/borrow/mod.rs @@ -53,12 +53,17 @@ pub fn read<'a>(data: &mut Cursor<&'a [u8]>) -> Result, Error> { stack.push(ParsingStackElement::Compound { index_of_compound_element: 0, })?; - tapes.main.push(TapeElement::new_with_approx_len_and_offset( - TapeTagKind::Compound, - // these get overwritten later - 0, - 0, - )); + unsafe { + // SAFETY: we just created the MainTape so there's definitely space for an item + tapes + .main + .push_unchecked(TapeElement::new_with_approx_len_and_offset( + TapeTagKind::Compound, + // these get overwritten later + 0, + 0, + )) + }; read_with_stack(&mut data, &mut tapes, &mut stack)?; @@ -91,12 +96,17 @@ pub fn read_compound<'a>(data: &mut Cursor<&'a [u8]>) -> Result MainTape { self.end = unsafe { self.cur.add(extending_by) }; } + unsafe { self.push_unchecked(element) }; + } + + #[inline] + pub unsafe fn push_unchecked(&mut self, element: TapeElement) { unsafe { self.cur.write(element) }; self.cur = unsafe { self.cur.add(1) }; }