use epserde::{deser::DeserType, prelude::*, ser::SerType};
#[derive(Epserde, Debug, Clone, Copy)]
enum Data<T = Vec<i32>> {
A, B { a: usize, b: usize }, C(T), }
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Serializable type: {}", core::any::type_name::<Data>());
println!(
"Associated serialization type: {}",
core::any::type_name::<SerType<Data>>()
);
println!();
let data: Data = Data::A;
let mut cursor = <AlignedCursor<Aligned16>>::new();
#[cfg(feature = "schema")]
{
let schema = unsafe { data.serialize_with_schema(&mut cursor)? };
println!("{}", schema.debug(cursor.as_bytes()));
println!();
}
#[cfg(not(feature = "schema"))]
let _bytes_written = unsafe { data.serialize(&mut cursor)? };
cursor.set_position(0);
let full = unsafe { <Data>::deserialize_full(&mut cursor)? };
println!(
"Full-copy deserialization: returns the deserializable type {}",
core::any::type_name::<Data>(),
);
println!("Value: {:x?}", full);
println!();
let eps = unsafe { <Data>::deserialize_eps(cursor.as_bytes())? };
println!(
"ε-copy deserialization: returns the associated deserialization type {}",
core::any::type_name::<DeserType<'_, Data>>(),
);
println!("Value: {:x?}", eps);
let data: Data<Vec<usize>> = Data::A;
let mut cursor = <AlignedCursor<Aligned16>>::new();
#[cfg(feature = "schema")]
{
let schema = unsafe { data.serialize_with_schema(&mut cursor)? };
println!("{}", schema.debug(cursor.as_bytes()));
println!();
}
#[cfg(not(feature = "schema"))]
let _bytes_written = unsafe { data.serialize(&mut cursor)? };
println!();
println!("Deserializing with a different parameter type (will generate a type-hash error)...");
println!();
cursor.set_position(0);
println!("Error in full-copy deserialization: {}", unsafe {
<Data>::deserialize_full(&mut cursor).err().unwrap()
});
#[cfg(not(feature = "schema"))]
println!("\nPlease compile with the \"schema\" feature to see the schema output");
Ok(())
}