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

add NbtTag::read_optional

This commit is contained in:
mat 2023-12-28 17:46:24 -06:00
parent 00c6ad1b41
commit 922701104a
4 changed files with 22 additions and 1 deletions

View file

@ -23,6 +23,8 @@ fastnbt = "2.4.4"
azalea-nbt = { git = "https://github.com/azalea-rs/azalea", rev = "84e036ce3752ecf57904b0f5aff1f33d43e95a32" }
hematite-nbt = { version = "0.5.2", default-features = false }
tikv-jemallocator = "0.5"
[features]
default = ["derive"]
derive = ["dep:simdnbt-derive"]

View file

@ -46,8 +46,11 @@ fn bench_file(filename: &str, c: &mut Criterion) {
group.finish();
}
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
fn bench(c: &mut Criterion) {
// bench_file("bigtest.nbt", c);
bench_file("bigtest.nbt", c);
// bench_file("simple_player.dat", c);
bench_file("complex_player.dat", c);
// bench_file("level.dat", c);

View file

@ -170,6 +170,14 @@ impl<'a> NbtTag<'a> {
Self::read_with_type(data, tag_type, 0)
}
pub fn read_optional(data: &mut Cursor<&'a [u8]>) -> Result<Option<Self>, Error> {
let tag_type = data.read_u8().map_err(|_| Error::UnexpectedEof)?;
if tag_type == END_ID {
return Ok(None);
}
Ok(Some(Self::read_with_type(data, tag_type, 0)?))
}
pub fn byte(&self) -> Option<i8> {
match self {
NbtTag::Byte(byte) => Some(*byte),

View file

@ -264,6 +264,14 @@ impl NbtTag {
Self::read_with_type(data, tag_type, 0)
}
pub fn read_optional(data: &mut Cursor<&[u8]>) -> Result<Option<Self>, Error> {
let tag_type = data.read_u8().map_err(|_| Error::UnexpectedEof)?;
if tag_type == END_ID {
return Ok(None);
}
Ok(Some(Self::read_with_type(data, tag_type, 0)?))
}
/// Write to the data without checking that there's enough space in it.
///
/// # Safety