1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 07:26:04 +00:00

Release 0.3.0

simdnbt@0.3.0
simdnbt-derive@0.3.0

Generated by cargo-workspaces
This commit is contained in:
mat 2023-12-02 13:54:12 -06:00
parent 049b604498
commit d427ea3f55
6 changed files with 57 additions and 54 deletions

View file

@ -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"

View file

@ -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]

View file

@ -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::<BE>().map_err(|_| Error::UnexpectedEof)?),
)),
INT_ID => values.push((
tag_name,
NbtTag::Int(data.read_i32::<BE>().map_err(|_| Error::UnexpectedEof)?),
)),
LONG_ID => values.push((
tag_name,
NbtTag::Long(data.read_i64::<BE>().map_err(|_| Error::UnexpectedEof)?),
)),
FLOAT_ID => values.push((
tag_name,
NbtTag::Float(data.read_f32::<BE>().map_err(|_| Error::UnexpectedEof)?),
)),
DOUBLE_ID => values.push((
tag_name,
NbtTag::Double(data.read_f64::<BE>().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 })
}

View file

@ -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,

View file

@ -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::<u8>() }
}
fn read_with_type(
data: &mut Cursor<&'a [u8]>,
tag_type: u8,
depth: usize,
) -> Result<Self, Error> {
match tag_type {
BYTE_ID => Ok(NbtTag::Byte(
data.read_i8().map_err(|_| Error::UnexpectedEof)?,
)),
SHORT_ID => Ok(NbtTag::Short(
data.read_i16::<BE>().map_err(|_| Error::UnexpectedEof)?,
)),
INT_ID => Ok(NbtTag::Int(
data.read_i32::<BE>().map_err(|_| Error::UnexpectedEof)?,
)),
LONG_ID => Ok(NbtTag::Long(
data.read_i64::<BE>().map_err(|_| Error::UnexpectedEof)?,
)),
FLOAT_ID => Ok(NbtTag::Float(
data.read_f32::<BE>().map_err(|_| Error::UnexpectedEof)?,
)),
DOUBLE_ID => Ok(NbtTag::Double(
data.read_f64::<BE>().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<Self, Error> {
let tag_type = data.read_u8().map_err(|_| Error::UnexpectedEof)?;
Self::read_with_type(data, tag_type, 0)
}
pub fn byte(&self) -> Option<i8> {
match self {
NbtTag::Byte(byte) => Some(*byte),

View file

@ -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<T>,