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

Function to_bytes_in_with_alloc

pub fn to_bytes_in_with_alloc<W, A, E>(
    value: &impl Serialize<Strategy<Serializer<W, A, Share>, E>>,
    writer: W,
    alloc: A,
) -> Result<W, E> 
where W: Writer<E>, A: Allocator<E>, E: Source,
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 high-level API.

§Example

use rkyv::{
    api::high::to_bytes_in_with_alloc,
    from_bytes,
    rancor::Error,
    util::{with_arena, AlignedVec},
    Archive, Deserialize, Serialize,
};

#[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
struct Example {
    name: String,
    value: i32,
}

let value = Example {
    name: "pi".to_string(),
    value: 31415926,
};

with_arena(|arena| {
    let bytes = to_bytes_in_with_alloc::<_, _, Error>(
        &value,
        AlignedVec::<8>::new(),
        arena.acquire(),
    )
    .expect("failed to serialize vec");

    let deserialized = from_bytes::<Example, Error>(&bytes)
        .expect("failed to deserialize vec");

    assert_eq!(deserialized, value);
});