From d427ea3f554da97ef1ec568dc2beba6bc738c603 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 2 Dec 2023 13:54:12 -0600 Subject: [PATCH] Release 0.3.0 simdnbt@0.3.0 simdnbt-derive@0.3.0 Generated by cargo-workspaces --- simdnbt-derive/Cargo.toml | 2 +- simdnbt/Cargo.toml | 4 +-- simdnbt/src/borrow/compound.rs | 49 ++++---------------------------- simdnbt/src/borrow/list.rs | 2 +- simdnbt/src/borrow/mod.rs | 52 ++++++++++++++++++++++++++++++---- simdnbt/src/raw_list.rs | 2 +- 6 files changed, 57 insertions(+), 54 deletions(-) diff --git a/simdnbt-derive/Cargo.toml b/simdnbt-derive/Cargo.toml index 6b8db65..6650909 100644 --- a/simdnbt-derive/Cargo.toml +++ b/simdnbt-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "simdnbt-derive" -version = "0.2.1" +version = "0.3.0" edition = "2021" description = "Derive macros for simdnbt" license = "MIT" diff --git a/simdnbt/Cargo.toml b/simdnbt/Cargo.toml index 75d96c1..4404da9 100644 --- a/simdnbt/Cargo.toml +++ b/simdnbt/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "simdnbt" -version = "0.2.1" +version = "0.3.0" edition = "2021" description = "an unnecessarily fast nbt decoder" license = "MIT" @@ -12,7 +12,7 @@ repository = "https://github.com/mat-1/simdnbt" byteorder = "1.5.0" flate2 = "^1.0.28" residua-mutf8 = "2.0.0" -simdnbt-derive = { version = "0.2.0", path = "../simdnbt-derive", optional = true } +simdnbt-derive = { version = "0.3.0", path = "../simdnbt-derive", optional = true } thiserror = "1.0.50" [dev-dependencies] diff --git a/simdnbt/src/borrow/compound.rs b/simdnbt/src/borrow/compound.rs index 43d9440..997c96d 100644 --- a/simdnbt/src/borrow/compound.rs +++ b/simdnbt/src/borrow/compound.rs @@ -1,13 +1,11 @@ use std::io::Cursor; -use byteorder::{ReadBytesExt, BE}; +use byteorder::ReadBytesExt; use crate::{ common::{ - read_int_array, read_long_array, read_string, read_with_u32_length, unchecked_extend, - unchecked_push, unchecked_write_string, write_string, BYTE_ARRAY_ID, BYTE_ID, COMPOUND_ID, - DOUBLE_ID, END_ID, FLOAT_ID, INT_ARRAY_ID, INT_ID, LIST_ID, LONG_ARRAY_ID, LONG_ID, - MAX_DEPTH, SHORT_ID, STRING_ID, + read_string, unchecked_extend, unchecked_push, unchecked_write_string, write_string, + END_ID, MAX_DEPTH, }, Error, Mutf8Str, }; @@ -15,7 +13,7 @@ use crate::{ use super::{list::NbtList, NbtTag}; /// A list of named tags. The order of the tags is preserved. -#[derive(Debug, Default, PartialEq)] +#[derive(Debug, Default, PartialEq, Clone)] pub struct NbtCompound<'a> { values: Vec<(&'a Mutf8Str, NbtTag<'a>)>, } @@ -41,44 +39,7 @@ impl<'a> NbtCompound<'a> { } let tag_name = read_string(data)?; - match tag_type { - BYTE_ID => values.push(( - tag_name, - NbtTag::Byte(data.read_i8().map_err(|_| Error::UnexpectedEof)?), - )), - SHORT_ID => values.push(( - tag_name, - NbtTag::Short(data.read_i16::().map_err(|_| Error::UnexpectedEof)?), - )), - INT_ID => values.push(( - tag_name, - NbtTag::Int(data.read_i32::().map_err(|_| Error::UnexpectedEof)?), - )), - LONG_ID => values.push(( - tag_name, - NbtTag::Long(data.read_i64::().map_err(|_| Error::UnexpectedEof)?), - )), - FLOAT_ID => values.push(( - tag_name, - NbtTag::Float(data.read_f32::().map_err(|_| Error::UnexpectedEof)?), - )), - DOUBLE_ID => values.push(( - tag_name, - NbtTag::Double(data.read_f64::().map_err(|_| Error::UnexpectedEof)?), - )), - BYTE_ARRAY_ID => { - values.push((tag_name, NbtTag::ByteArray(read_with_u32_length(data, 1)?))) - } - STRING_ID => values.push((tag_name, NbtTag::String(read_string(data)?))), - LIST_ID => values.push((tag_name, NbtTag::List(NbtList::read(data, depth + 1)?))), - COMPOUND_ID => values.push(( - tag_name, - NbtTag::Compound(NbtCompound::read_with_depth(data, depth + 1)?), - )), - INT_ARRAY_ID => values.push((tag_name, NbtTag::IntArray(read_int_array(data)?))), - LONG_ARRAY_ID => values.push((tag_name, NbtTag::LongArray(read_long_array(data)?))), - _ => return Err(Error::UnknownTagId(tag_type)), - } + values.push((tag_name, NbtTag::read_with_type(data, tag_type, depth)?)); } Ok(Self { values }) } diff --git a/simdnbt/src/borrow/list.rs b/simdnbt/src/borrow/list.rs index a1f29aa..647932c 100644 --- a/simdnbt/src/borrow/list.rs +++ b/simdnbt/src/borrow/list.rs @@ -17,7 +17,7 @@ use super::{read_u32, NbtCompound, MAX_DEPTH}; /// A list of NBT tags of a single type. #[repr(u8)] -#[derive(Debug, Default, PartialEq)] +#[derive(Debug, Default, PartialEq, Clone)] pub enum NbtList<'a> { #[default] Empty = END_ID, diff --git a/simdnbt/src/borrow/mod.rs b/simdnbt/src/borrow/mod.rs index c2093e9..dbed7ca 100644 --- a/simdnbt/src/borrow/mod.rs +++ b/simdnbt/src/borrow/mod.rs @@ -5,13 +5,13 @@ mod list; use std::{io::Cursor, ops::Deref}; -use byteorder::ReadBytesExt; +use byteorder::{ReadBytesExt, BE}; use crate::{ common::{ - read_string, read_u32, write_string, BYTE_ARRAY_ID, BYTE_ID, COMPOUND_ID, DOUBLE_ID, - END_ID, FLOAT_ID, INT_ARRAY_ID, INT_ID, LIST_ID, LONG_ARRAY_ID, LONG_ID, MAX_DEPTH, - SHORT_ID, STRING_ID, + read_int_array, read_long_array, read_string, read_u32, read_with_u32_length, write_string, + BYTE_ARRAY_ID, BYTE_ID, COMPOUND_ID, DOUBLE_ID, END_ID, FLOAT_ID, INT_ARRAY_ID, INT_ID, + LIST_ID, LONG_ARRAY_ID, LONG_ID, MAX_DEPTH, SHORT_ID, STRING_ID, }, raw_list::RawList, Error, Mutf8Str, @@ -102,7 +102,7 @@ impl<'a> BaseNbt<'a> { /// A single NBT tag. #[repr(u8)] -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum NbtTag<'a> { Byte(i8) = BYTE_ID, Short(i16) = SHORT_ID, @@ -128,6 +128,48 @@ impl<'a> NbtTag<'a> { unsafe { *<*const _>::from(self).cast::() } } + fn read_with_type( + data: &mut Cursor<&'a [u8]>, + tag_type: u8, + depth: usize, + ) -> Result { + match tag_type { + BYTE_ID => Ok(NbtTag::Byte( + data.read_i8().map_err(|_| Error::UnexpectedEof)?, + )), + SHORT_ID => Ok(NbtTag::Short( + data.read_i16::().map_err(|_| Error::UnexpectedEof)?, + )), + INT_ID => Ok(NbtTag::Int( + data.read_i32::().map_err(|_| Error::UnexpectedEof)?, + )), + LONG_ID => Ok(NbtTag::Long( + data.read_i64::().map_err(|_| Error::UnexpectedEof)?, + )), + FLOAT_ID => Ok(NbtTag::Float( + data.read_f32::().map_err(|_| Error::UnexpectedEof)?, + )), + DOUBLE_ID => Ok(NbtTag::Double( + data.read_f64::().map_err(|_| Error::UnexpectedEof)?, + )), + BYTE_ARRAY_ID => Ok(NbtTag::ByteArray(read_with_u32_length(data, 1)?)), + STRING_ID => Ok(NbtTag::String(read_string(data)?)), + LIST_ID => Ok(NbtTag::List(NbtList::read(data, depth + 1)?)), + COMPOUND_ID => Ok(NbtTag::Compound(NbtCompound::read_with_depth( + data, + depth + 1, + )?)), + INT_ARRAY_ID => Ok(NbtTag::IntArray(read_int_array(data)?)), + LONG_ARRAY_ID => Ok(NbtTag::LongArray(read_long_array(data)?)), + _ => Err(Error::UnknownTagId(tag_type)), + } + } + + pub fn read(data: &mut Cursor<&'a [u8]>) -> Result { + let tag_type = data.read_u8().map_err(|_| Error::UnexpectedEof)?; + Self::read_with_type(data, tag_type, 0) + } + pub fn byte(&self) -> Option { match self { NbtTag::Byte(byte) => Some(*byte), diff --git a/simdnbt/src/raw_list.rs b/simdnbt/src/raw_list.rs index ea22f3d..94c6dfd 100644 --- a/simdnbt/src/raw_list.rs +++ b/simdnbt/src/raw_list.rs @@ -4,7 +4,7 @@ use crate::swap_endianness::{swap_endianness, swap_endianness_as_u8, SwappableNu /// A list of numbers that's kept as big-endian in memory. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub struct RawList<'a, T> { data: &'a [u8], _marker: PhantomData,