/* * SPDX-FileCopyrightText: 2023 Inria * SPDX-FileCopyrightText: 2023 Sebastiano Vigna * * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later */ /// Example showing that vectors of a zero-copy type are ε-copy /// deserialized to a reference. use epserde::prelude::*; use maligned::A16; #[derive(Epserde, Debug, PartialEq, Eq, Default, Clone)] struct Object { a: A, test: isize, } #[repr(C)] #[derive(Epserde, Debug, PartialEq, Eq, Default, Clone, Copy)] // We want to use zero-copy deserialization on Point, // and thus ε-copy deserialization on Vec, etc. #[zero_copy] struct Point { x: usize, y: usize, } fn main() { let point: Object> = Object { a: vec![Point { x: 2, y: 1 }; 6], test: -0xbadf00d, }; let mut cursor = >::new(); // Serialize let _bytes_written = point.serialize(&mut cursor).unwrap(); // Do a full-copy deserialization cursor.set_position(0); let full = >>::deserialize_full(&mut cursor).unwrap(); println!( "Full-copy deserialization type: {}", std::any::type_name::>>(), ); println!("Value: {:x?}", full); assert_eq!(point, full); println!(); // Do an ε-copy deserialization let eps = >>::deserialize_eps(cursor.as_bytes()).unwrap(); println!( "ε-copy deserialization type: {}", std::any::type_name::<> as DeserializeInner>::DeserType<'_>>(), ); println!("Value: {:x?}", eps); assert_eq!(point.a, eps.a); assert_eq!(point.test, eps.test); }