use epserde::{deser::DeserType, prelude::*, ser::SerType};
#[derive(Epserde, Copy, Debug, PartialEq, Eq, Default, Clone)]
#[repr(C)]
#[epserde(zero_copy)]
struct USize(usize);
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Serializable type: {}", core::any::type_name::<USize>());
println!(
"Associated serialization type: {}",
core::any::type_name::<SerType<USize>>()
);
println!();
let data = USize(0);
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 { <USize>::deserialize_full(&mut cursor)? };
println!(
"Full-copy deserialization: returns the deserializable type {}",
core::any::type_name::<USize>(),
);
println!("Value: {:x?}", full);
assert_eq!(data, full);
println!();
let eps = unsafe { <USize>::deserialize_eps(cursor.as_bytes())? };
println!(
"ε-copy deserialization: returns the associated deserialization type {}",
core::any::type_name::<DeserType<'_, USize>>(),
);
println!("Value: {:x?}", eps);
assert_eq!(data, *eps);
#[cfg(not(feature = "schema"))]
println!("\nPlease compile with the \"schema\" feature to see the schema output");
Ok(())
}