Merge pull request #1790 from tnull/2022-10-inbound-user-channel-id-randomization
[rust-lightning] / lightning / src / util / ser_macros.rs
index 3ec0848680ffe62a2532e03d47d1fc5b62d7f4b8..9a11dac7a4922bfb5e6ddf2911d9974f2a1e9470 100644 (file)
@@ -412,6 +412,18 @@ macro_rules! init_tlv_field_var {
        };
 }
 
+macro_rules! init_and_read_tlv_fields {
+       ($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
+               $(
+                       init_tlv_field_var!($field, $fieldty);
+               )*
+
+               read_tlv_fields!($reader, {
+                       $(($type, $field, $fieldty)),*
+               });
+       }
+}
+
 /// Implements Readable/Writeable for a struct storing it as a set of TLVs
 /// If $fieldty is `required`, then $field is a required field that is not an Option nor a Vec.
 /// If $fieldty is `option`, then $field is optional field.
@@ -446,10 +458,7 @@ macro_rules! impl_writeable_tlv_based {
 
                impl $crate::util::ser::Readable for $st {
                        fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
-                               $(
-                                       init_tlv_field_var!($field, $fieldty);
-                               )*
-                               read_tlv_fields!(reader, {
+                               init_and_read_tlv_fields!(reader, {
                                        $(($type, $field, $fieldty)),*
                                });
                                Ok(Self {
@@ -583,10 +592,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
                                                // Because read_tlv_fields creates a labeled loop, we cannot call it twice
                                                // in the same function body. Instead, we define a closure and call it.
                                                let f = || {
-                                                       $(
-                                                               init_tlv_field_var!($field, $fieldty);
-                                                       )*
-                                                       read_tlv_fields!(reader, {
+                                                       init_and_read_tlv_fields!(reader, {
                                                                $(($type, $field, $fieldty)),*
                                                        });
                                                        Ok(Some($st::$variant_name {
@@ -636,10 +642,7 @@ macro_rules! impl_writeable_tlv_based_enum {
                                                // Because read_tlv_fields creates a labeled loop, we cannot call it twice
                                                // in the same function body. Instead, we define a closure and call it.
                                                let f = || {
-                                                       $(
-                                                               init_tlv_field_var!($field, $fieldty);
-                                                       )*
-                                                       read_tlv_fields!(reader, {
+                                                       init_and_read_tlv_fields!(reader, {
                                                                $(($type, $field, $fieldty)),*
                                                        });
                                                        Ok($st::$variant_name {