Skip to content

Commit a03db3c

Browse files
committed
Add support for including ignorable types in enum de/ser macros
An enum implements de/serialization via `impl_writeable_tlv_based_enum_upgradable` currently cannot contain an object that only implements `MaybeReadable`. This solves that by implementing the required blocks for `ignorable`, opting to return `Ok(None)` in the top-level read in cases where the inner field read returns `Ok(None)`.
1 parent 7fd9b33 commit a03db3c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ macro_rules! _encode_tlv {
3939
field.write($stream)?;
4040
}
4141
};
42+
($stream: expr, $type: expr, $field: expr, ignorable) => {
43+
$crate::_encode_tlv!($stream, $type, $field, required);
44+
};
4245
($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
4346
$crate::_encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option);
4447
};
@@ -155,6 +158,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
155158
$len.0 += field_len;
156159
}
157160
};
161+
($len: expr, $type: expr, $field: expr, ignorable) => {
162+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
163+
};
158164
}
159165

160166
/// See the documentation of [`write_tlv_fields`].
@@ -581,6 +587,9 @@ macro_rules! _init_tlv_based_struct_field {
581587
($field: ident, option) => {
582588
$field
583589
};
590+
($field: ident, ignorable) => {
591+
if $field.is_none() { return Ok(None); } else { $field.unwrap() }
592+
};
584593
($field: ident, required) => {
585594
$field.0.unwrap()
586595
};
@@ -610,6 +619,9 @@ macro_rules! _init_tlv_field_var {
610619
($field: ident, option) => {
611620
let mut $field = None;
612621
};
622+
($field: ident, ignorable) => {
623+
let mut $field = None;
624+
};
613625
}
614626

615627
/// Equivalent to running [`_init_tlv_field_var`] then [`read_tlv_fields`].

0 commit comments

Comments
 (0)