From: Valentine Wallace Date: Fri, 23 Jun 2023 18:55:43 +0000 (-0400) Subject: Add new _init_and_read_tlv_stream ser macro X-Git-Tag: v0.0.117-alpha1~45^2~5 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=cf64e3fba52d7b7f6abce352948d78b97bcb9700;p=rust-lightning Add new _init_and_read_tlv_stream ser macro 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. --- diff --git a/lightning/src/onion_message/packet.rs b/lightning/src/onion_message/packet.rs index 8b677e7bb..8896941c8 100644 --- a/lightning/src/onion_message/packet.rs +++ b/lightning/src/onion_message/packet.rs @@ -274,19 +274,16 @@ pub(crate) enum ControlTlvs { } impl Readable for ControlTlvs { - fn read(mut r: &mut R) -> Result { - let mut _padding: Option = None; - let mut _short_channel_id: Option = None; - let mut next_node_id: Option = None; - let mut path_id: Option<[u8; 32]> = None; - let mut next_blinding_override: Option = None; - decode_tlv_stream!(&mut r, { + fn read(r: &mut R) -> Result { + _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; + let _short_channel_id: Option = _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(); diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 50451f777..4f1fada95 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -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.