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

Function access_pos_mut

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

Mutably access a byte slice with a given root position.

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

§Example

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

#[derive(Archive, Serialize)]
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 =
    access_pos_mut::<ArchivedExample, Error>(&mut bytes, root_pos).unwrap();

// 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);