mod.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. pub(crate) mod deserialize_props;
  2. pub(crate) mod serialize_props;
  3. #[test]
  4. fn serialized_and_deserializes() {
  5. use postcard::to_allocvec;
  6. #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
  7. struct Data {
  8. a: u32,
  9. b: String,
  10. bytes: Vec<u8>,
  11. nested: Nested,
  12. }
  13. #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
  14. struct Nested {
  15. a: u32,
  16. b: u16,
  17. c: u8,
  18. }
  19. for x in 0..10usize {
  20. for y in 0..10 {
  21. let mut as_string = String::new();
  22. let data = vec![
  23. Data {
  24. a: x as u32,
  25. b: "hello".to_string(),
  26. bytes: vec![0; x],
  27. nested: Nested {
  28. a: 1,
  29. b: x as u16,
  30. c: 3
  31. },
  32. };
  33. y
  34. ];
  35. serialize_props::serde_to_writable(&data, &mut as_string).unwrap();
  36. println!("{}", as_string);
  37. println!(
  38. "original size: {}",
  39. std::mem::size_of::<Data>() * data.len()
  40. );
  41. println!("serialized size: {}", to_allocvec(&data).unwrap().len());
  42. println!("compressed size: {}", as_string.len());
  43. let decoded: Vec<Data> = deserialize_props::serde_from_string(&as_string).unwrap();
  44. assert_eq!(data, decoded);
  45. }
  46. }
  47. }