Skip to content

Commit 980e7ce

Browse files
committed
Introduce init_and_read_tlv_fields macro
We introduce a new macro that inits and reads tlv fields and DRY up `impl_writeable_tlv_based`.
1 parent dbd5213 commit 980e7ce

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lightning/src/util/ser_macros.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,18 @@ macro_rules! init_tlv_field_var {
391391
};
392392
}
393393

394+
macro_rules! init_and_read_tlv_fields {
395+
($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
396+
$(
397+
init_tlv_field_var!($field, $fieldty);
398+
)*
399+
400+
read_tlv_fields!($reader, {
401+
$(($type, $field, $fieldty)),*
402+
});
403+
}
404+
}
405+
394406
/// Implements Readable/Writeable for a struct storing it as a set of TLVs
395407
/// If $fieldty is `required`, then $field is a required field that is not an Option nor a Vec.
396408
/// If $fieldty is `option`, then $field is optional field.
@@ -425,12 +437,10 @@ macro_rules! impl_writeable_tlv_based {
425437

426438
impl $crate::util::ser::Readable for $st {
427439
fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
428-
$(
429-
init_tlv_field_var!($field, $fieldty);
430-
)*
431-
read_tlv_fields!(reader, {
440+
init_and_read_tlv_fields!(reader, {
432441
$(($type, $field, $fieldty)),*
433442
});
443+
434444
Ok(Self {
435445
$(
436446
$field: init_tlv_based_struct_field!($field, $fieldty)

0 commit comments

Comments
 (0)