Skip to content

Commit aa10f64

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` and other macros.
1 parent 505102d commit aa10f64

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 15 additions & 12 deletions
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,10 +437,7 @@ 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
});
434443
Ok(Self {
@@ -493,10 +502,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
493502
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
494503
// in the same function body. Instead, we define a closure and call it.
495504
let f = || {
496-
$(
497-
init_tlv_field_var!($field, $fieldty);
498-
)*
499-
read_tlv_fields!(reader, {
505+
init_and_read_tlv_fields!(reader, {
500506
$(($type, $field, $fieldty)),*
501507
});
502508
Ok(Some($st::$variant_name {
@@ -546,10 +552,7 @@ macro_rules! impl_writeable_tlv_based_enum {
546552
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
547553
// in the same function body. Instead, we define a closure and call it.
548554
let f = || {
549-
$(
550-
init_tlv_field_var!($field, $fieldty);
551-
)*
552-
read_tlv_fields!(reader, {
555+
init_and_read_tlv_fields!(reader, {
553556
$(($type, $field, $fieldty)),*
554557
});
555558
Ok($st::$variant_name {

0 commit comments

Comments
 (0)