Function access_pos_mut
pub fn access_pos_mut<T, E>(
bytes: &mut [u8],
pos: usize,
) -> Result<Seal<'_, T>, E> ⓘwhere
T: Portable + for<'a> CheckBytes<Strategy<Validator<ArchiveValidator<'a>, ()>, E>>,
E: Source,
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 low-level API.
§Example
use core::mem::MaybeUninit;
use rkyv::{
api::{root_position, low::{to_bytes_in_with_alloc, access_pos_mut}},
rancor::Failure,
ser::{allocator::SubAllocator, writer::Buffer},
util::Align,
with::InlineAsBox,
Archive, Serialize,
munge::munge,
};
let mut output = Align([MaybeUninit::<u8>::uninit(); 256]);
let mut alloc = [MaybeUninit::<u8>::uninit(); 256];
#[derive(Archive, Serialize)]
struct Example {
inner: i32,
}
let value = Example { inner: 42 };
let mut bytes = to_bytes_in_with_alloc::<_, _, Failure>(
&value,
Buffer::from(&mut *output),
SubAllocator::new(&mut alloc),
)
.unwrap();
let root_pos = root_position::<ArchivedExample>(bytes.len());
let mut archived = access_pos_mut::<ArchivedExample, Failure>(
&mut *bytes,
root_pos,
).unwrap();
// Because the access is mutable, we can mutate the archived data
munge!(let ArchivedExample { mut inner, .. } = archived);
assert_eq!(*inner, 42);
*inner = 12345.into();
assert_eq!(*inner, 12345);