devela::_dep::rkyv::api::high

Function access_pos

pub fn access_pos<T, E>(bytes: &[u8], pos: usize) -> Result<&T, E> 
Available on crate feature dep_rkyv only.
Expand description

Access a byte slice with a given root position.

This is a safe alternative to access_pos_unchecked and is part of the high-level API.

§Example

use rkyv::{
    api::{high::access_pos, root_position},
    bytecheck::CheckBytes,
    rancor::Error,
    to_bytes, Archive, Archived, Serialize,
};

#[derive(Archive, Serialize)]
struct Example {
    name: String,
    value: i32,
}

let value = Example {
    name: "pi".to_string(),
    value: 31415926,
};

let bytes = to_bytes::<Error>(&value).unwrap();
let archived = access_pos::<ArchivedExample, Error>(
    &bytes,
    root_position::<ArchivedExample>(bytes.len()),
)
.unwrap();

assert_eq!(archived.name, "pi");
assert_eq!(archived.value, 31415926);