devela::_dep::rkyv

Function access_unchecked

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

Access a byte slice.

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

§Safety

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

§Example

use rkyv::{
    access_unchecked, 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_unchecked::<ArchivedExample>(&*bytes) };
assert_eq!(archived.name, "pi");
assert_eq!(archived.value, 31415926);