devela::_dep::rkyv::api

Function access_pos_unchecked_mut

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

Mutably 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_mut 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_mut to safely access the buffer using validation.

The returned Seal restricts the mutating operations that may be safely performed on the returned reference. See Seal for more information.

§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::{
    to_bytes, api::{root_position, access_pos_unchecked_mut}, util::Align,
    Archive, Serialize, Deserialize, munge::munge, 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 root_pos = root_position::<ArchivedExample>(bytes.len());

let mut archived = unsafe {
    access_pos_unchecked_mut::<ArchivedExample>(&mut *bytes, root_pos)
};
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);