fastnbt crate
Documentation: docs.rs
Fast (or trying to be!) deserializer and parser for Minecraft: Java Edition's NBT format.
Includes
- a serde based deserializer for NBT for deserialization.
- a lower level parser using the
Read
trait.
The derserializer allows you to avoid allocations where possible. Strings can be
deserialized to &'a str
where 'a
is the lifetime of the data being
deserialized. This can also be done for lists of any integral type into
&'a [u8]
. The same applies to the Array types in NBT.
You can then parse the &[u8]
only when you need it. The fastanvil
crate has
a PackedBits
type that can do this for you.
fastnbt::Value
can be used to deserialize any NBT value.
See the examples
directory for more examples.
[]
= "0.12"
Serde derserialize example
This example shows retrieving a players inventory from the palyer.dat file found in the world/playerdata directory.
Some points:
- we are avoiding allocating a new string for every inventory slot by instead having a &str with a lifetime tied to the input data.
- we are deserializing the tag structure which is very dynamic using
Value
. - we rename fields using
serde
. NBT structures tend to mix case a lot.
use Result;
use ;
use GzDecoder;
use Deserialize;
use Read;
// This example show retrieving a players inventory from the palyer.dat file
// found in the world/playerdata directory.
//
// In particular we are avoiding allocating a new string for every inventory
// slot by instead having a &str with a lifetime tied to the input data.
Comparison to other NBT crates
There are other crates for NBT out there, this tries to give an honest comparison to them.
Feature | fastnbt |
Hematite nbt |
note |
---|---|---|---|
Benchmark world render* | 23.8+-0.7s | 31.6+-0.9s | making fastnbt 33% faster in this test |
Deserialization | yes | yes | |
Serialization | no | yes | |
Value -like type |
yes | yes | |
Long Array (MC 1.12+) | yes | yes | |
Minecraft specialized unicode | no** | yes | |
Deserialize from reader | no | yes | |
WASM compatible | yes | unknown |
*see 01-11-2020.md in benchmarks directory
**intended feature