Function access_pos
pub fn access_pos<T, E>(bytes: &[u8], pos: usize) -> Result<&T, E> ⓘwhere
T: Portable + for<'a> CheckBytes<Strategy<Validator<ArchiveValidator<'a>, ()>, E>>,
E: Source,
Available on crate feature
dep_rkyv
only.Expand description
Access a byte slice with a given root position.
This is a safe alternative to access_pos_unchecked
and is part of the
low-level API.
§Example
use core::mem::MaybeUninit;
use rkyv::{
api::{
low::{access_pos, to_bytes_in_with_alloc},
root_position,
},
rancor::Failure,
ser::{allocator::SubAllocator, writer::Buffer},
util::Align,
with::InlineAsBox,
Archive, Serialize,
};
let mut output = Align([MaybeUninit::<u8>::uninit(); 256]);
let mut alloc = [MaybeUninit::<u8>::uninit(); 256];
#[derive(Archive, Serialize)]
struct Example<'a> {
#[rkyv(with = InlineAsBox)]
inner: &'a i32,
}
let forty_two = 42;
let value = Example { inner: &forty_two };
let bytes = to_bytes_in_with_alloc::<_, _, Failure>(
&value,
Buffer::from(&mut *output),
SubAllocator::new(&mut alloc),
)
.unwrap();
let archived = access_pos::<ArchivedExample<'_>, Failure>(
&*bytes,
root_position::<ArchivedExample<'_>>(bytes.len()),
)
.unwrap();
assert_eq!(*archived.inner, 42);