Function to_bytes

Source
pub fn to_bytes<E>(
    value: &impl for<'a> Serialize<HighSerializer<AlignedVec, ArenaHandle<'a>, E>>,
) -> Result<AlignedVec, E>
where E: Source,
Available on crate feature alloc only.
Expand description

Serialize a value to bytes.

Returns the serialized bytes in an AlignedVec.

This is part of the high-level API.

ยงExample

use rkyv::{
    from_bytes, rancor::Error, to_bytes, Archive, Deserialize, Serialize,
};

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

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

let bytes = to_bytes::<Error>(&value).unwrap();
let deserialized = from_bytes::<Example, Error>(&bytes).unwrap();

assert_eq!(deserialized, value);