Module fastnbt::borrow

source ·
Expand description

This module contains types enabling ‘zero-copy’ capture of the array NBT types. These types retain a reference to the input data when deserializing, meaning the input has to live as long as the deserialized object. This can be hard to manage, but offers potential performance improvements. Measure! Usually the dominating factor in deserialization is decompressing the NBT data.

The ByteArray, IntArray, and LongArray types are the types to use in your own data structures. They all implement an iter() method to allow you to iterate over the data they contain.

For versions that own their data, see fasnbt::{ByteArray, IntArray, LongArray}.

The iter() methods return an iterator to the values read on demand from an internal reference to the input data.

Example

use fastnbt::borrow::LongArray;
use serde::Deserialize;

#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Section<'a> {
    #[serde(borrow)]
    pub block_states: Option<LongArray<'a>>,
    pub y: i8,
}

    let buf: &[u8] = unimplemented!("get a buffer from somewhere");
    let section: Section = fastnbt::from_bytes(buf).unwrap();
    let states = section.block_states.unwrap();

    for long in states.iter() {
        // do something
    }

Structs

  • ByteArray can be used to deserialize the NBT data of the same name. This borrows from the original input data when deserializing. The carving masks in a chunk use this type, for example.
  • IntArray can be used to deserialize the NBT data of the same name. This borrows from the original input data when deserializing. Biomes in the chunk format are an example of this data type.
  • LongArray can be used to deserialize the NBT data of the same name. This borrows from the original input data when deserializing. Block states (storage of all the blocks in a chunk) are an exmple of when this is used.

Functions