]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add support for including `ignorable` types in enum de/ser macros
authorMatt Corallo <git@bluematt.me>
Tue, 22 Nov 2022 22:41:13 +0000 (22:41 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 15 Jan 2023 20:28:42 +0000 (20:28 +0000)
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)`.

lightning/src/util/ser_macros.rs

index 8d74a83a37365fc490ccf089449aad42bcb955ea..afd7fcb2e0fc93b3f536a052c15cec841f8b43b0 100644 (file)
@@ -39,6 +39,9 @@ macro_rules! _encode_tlv {
                        field.write($stream)?;
                }
        };
+       ($stream: expr, $type: expr, $field: expr, ignorable) => {
+               $crate::_encode_tlv!($stream, $type, $field, required);
+       };
        ($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
                $crate::_encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option);
        };
@@ -155,6 +158,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
                        $len.0 += field_len;
                }
        };
+       ($len: expr, $type: expr, $field: expr, ignorable) => {
+               $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
+       };
 }
 
 /// See the documentation of [`write_tlv_fields`].
@@ -581,6 +587,9 @@ macro_rules! _init_tlv_based_struct_field {
        ($field: ident, option) => {
                $field
        };
+       ($field: ident, ignorable) => {
+               if $field.is_none() { return Ok(None); } else { $field.unwrap() }
+       };
        ($field: ident, required) => {
                $field.0.unwrap()
        };
@@ -610,6 +619,9 @@ macro_rules! _init_tlv_field_var {
        ($field: ident, option) => {
                let mut $field = None;
        };
+       ($field: ident, ignorable) => {
+               let mut $field = None;
+       };
 }
 
 /// Equivalent to running [`_init_tlv_field_var`] then [`read_tlv_fields`].