Function to_bytes_in_with_alloc
pub fn to_bytes_in_with_alloc<W, A, E>(
value: &impl Serialize<Strategy<Serializer<W, A, ()>, E>>,
writer: W,
alloc: A,
) -> Result<W, E> ⓘ
Available on crate feature
dep_rkyv
only.Expand description
Serialize a value using the given allocator and write the bytes to the given writer.
This is part of the low-level API.
§Example
use core::mem::MaybeUninit;
use rkyv::{
access_unchecked,
api::low::to_bytes_in_with_alloc,
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 = unsafe { access_unchecked::<ArchivedExample<'_>>(&*bytes) };
assert_eq!(*archived.inner, 42);