mz_catalog/durable/upgrade/
wire_compatible.rs1use bytes::Bytes;
11
12pub unsafe trait WireCompatible<T: prost::Message>: prost::Message + Default {
16 fn convert(old: &T) -> Self {
18 let bytes = old.encode_to_vec();
19 let bytes = Bytes::from(bytes);
21 Self::decode(bytes).expect("wire compatible")
22 }
23}
24
25unsafe impl<T: prost::Message + Default + Clone> WireCompatible<T> for T {
27 fn convert(old: &Self) -> Self {
28 old.clone()
29 }
30}
31
32#[macro_export]
43macro_rules! wire_compatible {
44 ($a:ident $(:: $a_sub:ident)* with $b:ident $(:: $b_sub:ident)*) => {
45 ::static_assertions::assert_impl_all!(
46 $a $(::$a_sub)* : ::proptest::arbitrary::Arbitrary, ::prost::Message, Default,
47 );
48 ::static_assertions::assert_impl_all!(
49 $b $(::$b_sub)* : ::proptest::arbitrary::Arbitrary, ::prost::Message, Default,
50 );
51
52 unsafe impl $crate::durable::upgrade::wire_compatible::WireCompatible< $b $(::$b_sub)* > for $a $(::$a_sub)* {}
55 unsafe impl $crate::durable::upgrade::wire_compatible::WireCompatible< $a $(::$a_sub)* > for $b $(::$b_sub)* {}
56
57 ::paste::paste! {
58 ::proptest::proptest! {
59 #[mz_ore::test]
60 #[cfg_attr(miri, ignore)] fn [<proptest_wire_compat_ $a:snake $(_$a_sub:snake)* _to_ $b:snake $(_$b_sub:snake)* >](a: $a $(::$a_sub)* ) {
62 use ::prost::Message;
63 let a_bytes = a.encode_to_vec();
64 let b_decoded = $b $(::$b_sub)*::decode(&a_bytes[..]);
65 ::proptest::prelude::prop_assert!(b_decoded.is_ok());
66
67 let b_decoded = b_decoded.expect("asserted Ok");
69 let b_converted: $b $(::$b_sub)* = $crate::durable::upgrade::wire_compatible::WireCompatible::convert(&a);
70 assert_eq!(b_decoded, b_converted);
71
72 let b_bytes = b_decoded.encode_to_vec();
73 ::proptest::prelude::prop_assert_eq!(a_bytes, b_bytes, "a and b serialize differently");
74 }
75
76 #[mz_ore::test]
77 #[cfg_attr(miri, ignore)] fn [<proptest_wire_compat_ $b:snake $(_$b_sub:snake)* _to_ $a:snake $(_$a_sub:snake)* >](b: $b $(::$b_sub)* ) {
79 use ::prost::Message;
80 let b_bytes = b.encode_to_vec();
81 let a_decoded = $a $(::$a_sub)*::decode(&b_bytes[..]);
82 ::proptest::prelude::prop_assert!(a_decoded.is_ok());
83
84 let a_decoded = a_decoded.expect("asserted Ok");
86 let a_converted: $a $(::$a_sub)* = $crate::durable::upgrade::wire_compatible::WireCompatible::convert(&b);
87 assert_eq!(a_decoded, a_converted);
88
89 let a_bytes = a_decoded.encode_to_vec();
90 ::proptest::prelude::prop_assert_eq!(a_bytes, b_bytes, "a and b serialize differently");
91 }
92 }
93 }
94 };
95}
96pub use wire_compatible;