#![cfg(test)] use core::marker::PhantomData; use epserde::prelude::*; use epserde::TypeInfo; use maligned::A16; #[test] /// Test that we can serialize and desertialize a PhantomData /// This should be a NOOP fn test_phantom() { // Create a new value to serialize let obj = >::default(); let mut cursor = >::new(); // Serialize let _bytes_written = obj.serialize(&mut cursor).unwrap(); // Do a full-copy deserialization cursor.set_position(0); let full = >::deserialize_full(&mut cursor).unwrap(); assert_eq!(obj, full); println!(); // Do an ε-copy deserialization let eps = >::deserialize_eps(cursor.as_bytes()).unwrap(); assert_eq!(obj, eps); } #[derive(Epserde, Debug, PartialEq, Eq, Clone, Default)] struct DataFull { a: usize, b: PhantomData, } #[derive(Debug, PartialEq, Eq, Clone, Default, TypeInfo)] struct NotSerializableType; /// Test that we can serialize a PhantomData of a non-serializable type /// in a full-copy type. /// This should be a no-op. #[test] fn test_not_serializable_in_phantom() { // Full copy with a non-serializable type let obj = >::default(); let mut cursor = >::new(); // Serialize let _bytes_written = obj.serialize(&mut cursor).unwrap(); // Do a full-copy deserialization cursor.set_position(0); let full = >::deserialize_full(&mut cursor).unwrap(); assert_eq!(obj, full); println!(); // Do an ε-copy deserialization cursor.set_position(0); let eps = >::deserialize_eps(cursor.as_bytes()).unwrap(); assert_eq!(obj.a, eps.a); } #[derive(Epserde, Copy, Debug, PartialEq, Eq, Clone, Default)] #[repr(C)] #[zero_copy] struct DataZero { a: usize, b: PhantomData, } #[derive(Epserde, Debug, Copy, PartialEq, Eq, Clone, Default)] #[repr(C)] #[zero_copy] struct ZeroCopyType; /// Test that we can serialize a PhantomData in a zero-copy /// type if the argument of the PhantomData is zero-copy. /// This should be a no-op. #[test] fn test_phantom_zero_copy() { // Zero copy needs a zero-copy type, even if inside a PhantomData let obj = >::default(); let mut cursor = >::new(); // Serialize let _bytes_written = obj.serialize(&mut cursor).unwrap(); // Do a full-copy deserialization cursor.set_position(0); let zero = >::deserialize_full(&mut cursor).unwrap(); assert_eq!(obj, zero); println!(); // Do an ε-copy deserialization cursor.set_position(0); let eps = >::deserialize_eps(cursor.as_bytes()).unwrap(); assert_eq!(obj.a, eps.a); } #[derive(Epserde, Copy, Debug, PartialEq, Eq, Clone, Default)] #[repr(C)] #[zero_copy] struct OnlyPhantom { a: PhantomData, b: PhantomData<(A, A)>, } /// Test that we can serialize a zero-copy type containing a single /// PhantomData. /// This should be a no-op. #[test] fn test_only_phantom() { // Zero copy needs a zero-copy type, even if inside a PhantomData let obj = >::default(); let mut cursor = >::new(); // Serialize let _bytes_written = obj.serialize(&mut cursor).unwrap(); // Do a full-copy deserialization cursor.set_position(0); let zero = >::deserialize_full(&mut cursor).unwrap(); assert_eq!(obj, zero); println!(); // Do an ε-copy deserialization cursor.set_position(0); let eps = >::deserialize_eps(cursor.as_bytes()).unwrap(); assert_eq!(obj.a, eps.a); }