From 8e41db05351fcf6338d7042280a6aead55da3281 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Fri, 20 Jan 2023 15:21:49 -0600 Subject: [PATCH] Expose `impl_writeable_msg` --- lightning/src/util/ser_macros.rs | 49 ++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index afd7fcb2e0f..a53345220f0 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -457,20 +457,59 @@ macro_rules! _decode_tlv_stream_range { } } } +/// Implements [`Readable`]/[`Writeable`] for a message struct that may include non-TLV and +/// TLV-encoded parts. +/// +/// This is useful to implement a [`CustomMessageReader`]. +/// +/// If `$fieldty` is `required`, then `$tlvfield` is a required field that is not an [`Option`] nor a [`Vec`]. +/// If `$fieldty` is `(default_value, $default)`, then `$tlvfield` will be set to `$default` if not present. +/// If `$fieldty` is `option`, then `$tlvfield` is optional field. +/// If `$fieldty` is `vec_type`, then `$tlvfield` is a [`Vec`], which needs to have its individual elements serialized. +/// +/// For example, +/// ``` +/// # use lightning::impl_writeable_msg; +/// struct MyCustomMessage { +/// pub field_1: u32, +/// pub field_2: bool, +/// pub field_3: String, +/// pub tlv_integer: u32, +/// pub tlv_default_integer: u32, +/// pub tlv_optional_integer: Option, +/// pub tlv_vec_type_integer: Vec, +/// } +/// +/// impl_writeable_msg!(MyCustomMessage, { +/// field_1, +/// field_2, +/// field_3 +/// }, { +/// (0, tlv_integer, required), +/// (1, tlv_default_integer, (default_value, 7)), +/// (2, tlv_optional_integer, option), +/// (3, tlv_vec_type_integer, vec_type), +/// }); +/// ``` +/// +/// [`Readable`]: crate::util::ser::Readable +/// [`Writeable`]: crate::util::ser::Writeable +/// [`CustomMessageReader`]: crate::ln::wire::CustomMessageReader +#[macro_export] macro_rules! impl_writeable_msg { ($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => { impl $crate::util::ser::Writeable for $st { fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { $( self.$field.write(w)?; )* - encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*}); + $crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*}); Ok(()) } } impl $crate::util::ser::Readable for $st { fn read(r: &mut R) -> Result { $(let $field = $crate::util::ser::Readable::read(r)?;)* - $(_init_tlv_field_var!($tlvfield, $fieldty);)* - decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*}); + $($crate::_init_tlv_field_var!($tlvfield, $fieldty);)* + $crate::decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*}); Ok(Self { $($field),*, $($tlvfield),* @@ -642,10 +681,10 @@ macro_rules! _init_and_read_tlv_fields { } /// 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 `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. /// If `$fieldty` is `option`, then `$field` is optional field. -/// If `$fieldty` is `vec_type`, then `$field` is a Vec, which needs to have its individual elements serialized. +/// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized. /// /// For example, /// ```