Macro impl_type_with_repr

Source
macro_rules! impl_type_with_repr {
    ($($ty:ident)::+ $(<$typaram:ident $(: $($tbound:ident)::+)?>)? => $repr:ty {
        $test_mod:ident $(<$($typaram_sample:ident = $typaram_sample_value:ty),*>)? {
            $(signature = $signature:literal,)?
            samples = $samples:expr,
            repr($sample_ident:ident) = $into_repr:expr,
        }
    }) => { ... };
}
Expand description

Implements the Type trait by delegating the signature to a simpler type (usually a tuple). Tests that ensure that the two types are serialize-compatible are auto-generated.

Example:

impl_type_with_repr! {
   // Duration is serialized as a (u64, u32) pair.
   Duration => (u64, u32) {
       // The macro auto-generates tests for us,
       // so we need to provide a test name.
       duration {
           // Sample values used to test serialize compatibility.
           samples = [Duration::ZERO, Duration::MAX],
           // Converts our type into the simpler "repr" type.
           repr(d) = (d.as_secs(), d.subsec_nanos()),
       }
   }
}