From: Matt Corallo Date: Tue, 22 Nov 2022 22:41:13 +0000 (+0000) Subject: Add support for including `ignorable` types in enum de/ser macros X-Git-Tag: v0.0.114-beta~53^2~2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a03db3ca309413c3408f5971b5ed5a8020422a81;p=rust-lightning 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)`. --- diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 8d74a83a3..afd7fcb2e 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -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`].