devela::_dep::rkyv::api

Function access_pos_unchecked

pub unsafe fn access_pos_unchecked<T>(bytes: &[u8], pos: usize) -> &T
where T: Portable,
Available on crate feature dep_rkyv only.
Expand description

Access a byte slice with a given root position.

Most of the time, the root position should be calculated using the root type and size of the buffer. Prefer access_unchecked whenever possible.

While the root of the archived data is located at the given position, the reachable data may be located throughout the byte slice.

This function does not check that the bytes are valid to access. Use access_pos to safely access the buffer using validation.

§Safety

The byte slice must represent a valid archived type when accessed with the given root position. See the module docs for more information.

§Example

use rkyv::{
    api::{access_pos_unchecked, root_position},
    rancor::Error,
    to_bytes, Archive, Deserialize, Serialize,
};

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

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

let bytes = to_bytes::<Error>(&value).unwrap();

let archived = unsafe {
    access_pos_unchecked::<ArchivedExample>(
        &*bytes,
        root_position::<ArchivedExample>(bytes.len()),
    )
};
assert_eq!(archived.name, "pi");
assert_eq!(archived.value, 31415926);