[][src]Struct fastnbt::de::Deserializer

pub struct Deserializer<'de> { /* fields omitted */ }

Deserializer for getting a T from some NBT data. Quite often you will need to rename fields using serde, as most Minecraft NBT data has inconsistent naming. The examples below show this with the rename_all attribute. See serdes other attributes for more.

You can take advantage of the lifetime of the input data to save allocations for things like strings. You can also deserialize any Array or List of primitive type as &'a [u8] to avoid allocating this data. See example below.

When deserializing integral types, the values are range checked to prevent overflow from occurring. If an overflow does occur you will get a Error::IntegralOutOfRange error.

Example of deserializing player.dat

use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct PlayerDat {
    data_version: i32,
    inventory: Vec<InventorySlot>,
    ender_items: Vec<InventorySlot>,
}

#[derive(Deserialize, Debug)]
struct InventorySlot {
    id: String,
}

Examples of avoiding allocation

We can easily avoid allocations of Strings using &'a str where 'a is the lifetime of the input data.

use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct InventorySlot<'a> {
    id: &'a str, // we avoid allocating a string here.
}

Here we're avoiding allocating memory for the various heightmaps found in chunk data. The PackedBits type is used as a wrapper for the way Minecraft's Anvil format packs various lists of numbers.

use fastnbt::anvil::PackedBits;
use serde::Deserialize;


#[derive(Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct Heightmaps<'a> {
    #[serde(borrow)]
    pub motion_blocking: Option<PackedBits<'a>>,
    pub motion_blocking_no_leaves: Option<PackedBits<'a>>,
    pub ocean_floor: Option<PackedBits<'a>>,
    pub world_surface: Option<PackedBits<'a>>,

    #[serde(skip)]
    unpacked_motion_blocking: Option<Vec<u16>>,
}

Example from region file

use fastnbt::anvil::{Chunk, Region};
use fastnbt::de::from_bytes;

fn main() {
    let args: Vec<_> = std::env::args().skip(1).collect();
    let file = std::fs::File::open(args[0].clone()).unwrap();

    let mut region = Region::new(file);
    let data = region.load_chunk(0, 0).unwrap();

    let chunk: Chunk = from_bytes(data.as_slice()).unwrap();

    println!("{:?}", chunk);
}

Implementations

impl<'de> Deserializer<'de>[src]

pub fn from_bytes(input: &'de [u8]) -> Self[src]

Trait Implementations

impl<'de, 'a> Deserializer<'de> for &'a mut Deserializer<'de>[src]

type Error = Error

The error type that can be returned if some error occurs during deserialization. Read more

Auto Trait Implementations

impl<'de> RefUnwindSafe for Deserializer<'de>

impl<'de> Send for Deserializer<'de>

impl<'de> Sync for Deserializer<'de>

impl<'de> Unpin for Deserializer<'de>

impl<'de> UnwindSafe for Deserializer<'de>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.