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

Function access_mut

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

Mutably access a byte slice.

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

§Example

use rkyv::{
    access_mut,
    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 mut archived = access_mut::<ArchivedExample, Error>(&mut bytes)
    .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);