Trait planus::ReadAsRoot

source ·
pub trait ReadAsRoot<'a>: Sized {
    // Required method
    fn read_as_root(slice: &'a [u8]) -> Result<Self>;
}
Expand description

Interface for getting a view into serialized data.

To get an owned variant use TryInto on the Ref type. Note that for nested types with lots of sharing the owned variants can be much larger than the serialized representation.

§Examples

use std::error::Error;
use planus::ReadAsRoot;
use planus_example::monster_generated::my_game::sample::{Monster, MonsterRef};

fn main() -> Result<(), Box<dyn Error>> {
    let buf = std::fs::read("monster.bin")?;
    let monster: MonsterRef<'_> = MonsterRef::read_as_root(&buf)?;
    let monster_health = monster.hp()?;
    let owned_monster: Monster = monster.try_into().expect("invalid monster");
    Ok(())
}

Required Methods§

source

fn read_as_root(slice: &'a [u8]) -> Result<Self>

Takes a slice assumed to be of this type and returns a view into it.

If the data is not valid for this type the field accessors will give errors or invalid values, but will still be memory safe.

Object Safety§

This trait is not object safe.

Implementors§