| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
/// Take a `VarULE` type and serialize it both in human and machine readable contexts, |
| 6 |
/// and ensure it roundtrips correctly |
| 7 |
/// |
| 8 |
/// Note that the concrete type may need to be explicitly specified to prevent issues with |
| 9 |
/// <https://github.com/rust-lang/rust/issues/130180> |
| 10 |
#[cfg(feature = "serde")] |
| 11 |
pub(crate) fn assert_serde_roundtrips<T>(var: &T) |
| 12 |
where |
| 13 |
T: crate::ule::VarULE + ?Sized + serde::Serialize, |
| 14 |
for<'a> Box<T>: serde::Deserialize<'a>, |
| 15 |
for<'a> &'a T: serde::Deserialize<'a>, |
| 16 |
T: core::fmt::Debug + PartialEq, |
| 17 |
{ |
| 18 |
let bincode = bincode::serialize(var).unwrap(); |
| 19 |
let deserialized: &T = bincode::deserialize(&bincode).unwrap(); |
| 20 |
let deserialized_box: Box<T> = bincode::deserialize(&bincode).unwrap(); |
| 21 |
assert_eq!(var, deserialized, "Single element roundtrips with bincode"); |
| 22 |
assert_eq!( |
| 23 |
var, &*deserialized_box, |
| 24 |
"Single element roundtrips with bincode" |
| 25 |
); |
| 26 |
|
| 27 |
let json = serde_json::to_string(var).unwrap(); |
| 28 |
let deserialized: Box<T> = serde_json::from_str(&json).unwrap(); |
| 29 |
assert_eq!(var, &*deserialized, "Single element roundtrips with serde"); |
| 30 |
} |