devela::_dep::rkyv::api

Function access_unchecked_mut

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

Mutably access a byte slice.

This function does not check that the bytes are valid to access. Use access_mut 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::{
    to_bytes, access_unchecked_mut, util::Align, Archive,
    munge::munge, Serialize, Deserialize, rancor::Error,
};

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

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

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

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

// Because the access is mutable, we can mutate the archived data
munge!(let ArchivedExample { mut value, .. } = archived);
assert_eq!(*value, 31415926);
*value = 12345.into();
assert_eq!(*value, 12345);