/* * SPDX-FileCopyrightText: 2023 Inria * * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later */ #![cfg(test)] use epserde::prelude::*; use maligned::A16; use std::marker::PhantomData; #[derive(Epserde, Debug, PartialEq, Eq, Clone)] struct Data { a: A, b: [i32; Q], } #[test] fn test_inner_param_full() { // Create a new value to serialize let person = Data { a: vec![0x89; 6], b: [0xbadf00d; 2], }; let mut cursor = >::new(); // Serialize let _bytes_written = person.serialize(&mut cursor).unwrap(); // Do a full-copy deserialization cursor.set_position(0); let full = , 2>>::deserialize_full(&mut cursor).unwrap(); assert_eq!(person, full); println!(); // Do an ε-copy deserialization let eps = , 2>>::deserialize_eps(cursor.as_bytes()).unwrap(); assert_eq!(person.a, eps.a); assert_eq!(person.b, eps.b); } #[derive(Epserde, Debug, PartialEq, Eq, Clone)] struct Data2 { a: B, _marker: std::marker::PhantomData, } #[test] fn test_inner_param_eps() { // Create a new value to serialize let data = Data2::> { a: vec![0x89; 6], _marker: PhantomData, }; let mut cursor = >::new(); // Serialize let _bytes_written = data.serialize(&mut cursor).unwrap(); // Do a full-copy deserialization cursor.set_position(0); let full = >>::deserialize_full(&mut cursor).unwrap(); assert_eq!(data, full); // Do an ε-copy deserialization let eps = >>::deserialize_eps(cursor.as_bytes()).unwrap(); assert_eq!(data.a, eps.a); } #[derive(Epserde, Debug, PartialEq, Eq, Clone, Copy)] #[zero_copy] #[repr(C)] struct Data3; #[test] fn test_consts() { // Create a new value to serialize let data = Data3::<11> {}; let mut cursor = >::new(); // Serialize let _bytes_written = data.serialize(&mut cursor).unwrap(); cursor.set_position(0); // with a different const the deserialization should fail let eps = >::deserialize_full(&mut cursor); assert!(eps.is_err()); // Do a full-copy deserialization cursor.set_position(0); let full = >::deserialize_full(&mut cursor).unwrap(); assert_eq!(data, full); // Do an ε-copy deserialization let eps = >::deserialize_eps(cursor.as_bytes()).unwrap(); assert_eq!(&data, eps); // with a different const the deserialization should fail let eps = >::deserialize_eps(cursor.as_bytes()); assert!(eps.is_err()); }