]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add new _init_and_read_tlv_stream ser macro
authorValentine Wallace <vwallace@protonmail.com>
Fri, 23 Jun 2023 18:55:43 +0000 (14:55 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Wed, 23 Aug 2023 15:24:54 +0000 (11:24 -0400)
Useful for when you want to use _init_and_read_len_prefixed_tlv_fields but there is no
length byte at the start of the TLV stream.

lightning/src/onion_message/packet.rs
lightning/src/util/ser_macros.rs

index 8b677e7bb611ea307067493bc2252685e7632636..8896941c8753789518944a260e15d3fa3112e6cd 100644 (file)
@@ -274,19 +274,16 @@ pub(crate) enum ControlTlvs {
 }
 
 impl Readable for ControlTlvs {
-       fn read<R: Read>(mut r: &mut R) -> Result<Self, DecodeError> {
-               let mut _padding: Option<Padding> = None;
-               let mut _short_channel_id: Option<u64> = None;
-               let mut next_node_id: Option<PublicKey> = None;
-               let mut path_id: Option<[u8; 32]> = None;
-               let mut next_blinding_override: Option<PublicKey> = None;
-               decode_tlv_stream!(&mut r, {
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               _init_and_read_tlv_stream!(r, {
                        (1, _padding, option),
                        (2, _short_channel_id, option),
                        (4, next_node_id, option),
                        (6, path_id, option),
                        (8, next_blinding_override, option),
                });
+               let _padding: Option<Padding> = _padding;
+               let _short_channel_id: Option<u64> = _short_channel_id;
 
                let valid_fwd_fmt  = next_node_id.is_some() && path_id.is_none();
                let valid_recv_fmt = next_node_id.is_none() && next_blinding_override.is_none();
index 50451f7775c5bbc340e9492631098e58190398a2..4f1fada952a49af110bd0abc67e38edb5b8ba293 100644 (file)
@@ -806,6 +806,19 @@ macro_rules! _init_and_read_len_prefixed_tlv_fields {
        }
 }
 
+/// Equivalent to running [`_init_tlv_field_var`] then [`decode_tlv_stream`].
+macro_rules! _init_and_read_tlv_stream {
+       ($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
+               $(
+                       $crate::_init_tlv_field_var!($field, $fieldty);
+               )*
+
+               $crate::decode_tlv_stream!($reader, {
+                       $(($type, $field, $fieldty)),*
+               });
+       }
+}
+
 /// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
 /// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
 /// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.