Skip to main content

to_bytes_with_alloc

Function to_bytes_with_alloc 

Source
pub fn to_bytes_with_alloc<A, E>(
    value: &impl Serialize<HighSerializer<AlignedVec, A, E>>,
    alloc: A,
) -> Result<AlignedVec, E>
where A: Allocator<E>, E: Source,
Expand description

Serialize a value using the given allocator.

This is part of the high-level API.

ยงExample

use rkyv::{
    api::high::to_bytes_with_alloc, from_bytes, rancor::Error,
    util::with_arena, 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_with_alloc::<_, Error>(&value, arena.acquire()).unwrap();
    let deserialized = from_bytes::<Example, Error>(&bytes).unwrap();

    assert_eq!(deserialized, value);
});