Skip to content

Commit cf64e3f

Browse files
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.
1 parent 4a30d9e commit cf64e3f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lightning/src/onion_message/packet.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,16 @@ pub(crate) enum ControlTlvs {
274274
}
275275

276276
impl Readable for ControlTlvs {
277-
fn read<R: Read>(mut r: &mut R) -> Result<Self, DecodeError> {
278-
let mut _padding: Option<Padding> = None;
279-
let mut _short_channel_id: Option<u64> = None;
280-
let mut next_node_id: Option<PublicKey> = None;
281-
let mut path_id: Option<[u8; 32]> = None;
282-
let mut next_blinding_override: Option<PublicKey> = None;
283-
decode_tlv_stream!(&mut r, {
277+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
278+
_init_and_read_tlv_stream!(r, {
284279
(1, _padding, option),
285280
(2, _short_channel_id, option),
286281
(4, next_node_id, option),
287282
(6, path_id, option),
288283
(8, next_blinding_override, option),
289284
});
285+
let _padding: Option<Padding> = _padding;
286+
let _short_channel_id: Option<u64> = _short_channel_id;
290287

291288
let valid_fwd_fmt = next_node_id.is_some() && path_id.is_none();
292289
let valid_recv_fmt = next_node_id.is_none() && next_blinding_override.is_none();

lightning/src/util/ser_macros.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,19 @@ macro_rules! _init_and_read_len_prefixed_tlv_fields {
806806
}
807807
}
808808

809+
/// Equivalent to running [`_init_tlv_field_var`] then [`decode_tlv_stream`].
810+
macro_rules! _init_and_read_tlv_stream {
811+
($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
812+
$(
813+
$crate::_init_tlv_field_var!($field, $fieldty);
814+
)*
815+
816+
$crate::decode_tlv_stream!($reader, {
817+
$(($type, $field, $fieldty)),*
818+
});
819+
}
820+
}
821+
809822
/// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
810823
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
811824
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.

0 commit comments

Comments
 (0)