Skip to content

Commit d8adf89

Browse files
committed
Serialization macro for TLV streams
BOLT 12's offer message is encoded as a TLV stream (i.e., a sequence of TLV records). impl_writeable_tlv_based can't be used because it writes the overall length of the struct, whereas TLV streams only include the length of each TLV record.
1 parent 0472c2f commit d8adf89

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,37 @@ macro_rules! impl_writeable_tlv_based {
427427
}
428428
}
429429

430+
/// Implements [`Readable`]/[`Writeable`] for TLV streams (i.e., a sequence of TLV records).
431+
///
432+
///
433+
/// This is like [`impl_writeable_tlv_based`] only without a length prefix for the entire TLV
434+
/// stream.
435+
macro_rules! impl_writeable_tlv_stream {
436+
($st: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
437+
impl ::util::ser::Writeable for $st {
438+
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
439+
encode_tlv_stream!(writer, { $(($type, self.$field, $fieldty)),* });
440+
Ok(())
441+
}
442+
}
443+
444+
impl ::util::ser::Readable for $st {
445+
fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
446+
$(
447+
init_tlv_field_var!($field, $fieldty);
448+
)*
449+
decode_tlv_stream!(reader, {$(($type, $field, $fieldty)),*});
450+
451+
Ok(Self {
452+
$(
453+
$field: init_tlv_based_struct_field!($field, $fieldty)
454+
),*
455+
})
456+
}
457+
}
458+
}
459+
}
460+
430461
macro_rules! _impl_writeable_tlv_based_enum_common {
431462
($st: ident, $(($variant_id: expr, $variant_name: ident) =>
432463
{$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}

0 commit comments

Comments
 (0)