Skip to content

Commit 3bd395f

Browse files
authored
Merge pull request #1976 from tnull/2023-01-expose-impl-writeable-msg
Expose `impl_writeable_msg`
2 parents bc90d00 + 99fe52e commit 3bd395f

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

lightning/src/util/ser_macros.rs

+35-5
Original file line numberDiff line numberDiff line change
@@ -460,20 +460,50 @@ macro_rules! _decode_tlv_stream_range {
460460
} }
461461
}
462462

463+
/// Implements [`Readable`]/[`Writeable`] for a message struct that may include non-TLV and
464+
/// TLV-encoded parts.
465+
///
466+
/// This is useful to implement a [`CustomMessageReader`].
467+
///
468+
/// Currently `$fieldty` may only be `option`, i.e., `$tlvfield` is optional field.
469+
///
470+
/// For example,
471+
/// ```
472+
/// # use lightning::impl_writeable_msg;
473+
/// struct MyCustomMessage {
474+
/// pub field_1: u32,
475+
/// pub field_2: bool,
476+
/// pub field_3: String,
477+
/// pub tlv_optional_integer: Option<u32>,
478+
/// }
479+
///
480+
/// impl_writeable_msg!(MyCustomMessage, {
481+
/// field_1,
482+
/// field_2,
483+
/// field_3
484+
/// }, {
485+
/// (1, tlv_optional_integer, option),
486+
/// });
487+
/// ```
488+
///
489+
/// [`Readable`]: crate::util::ser::Readable
490+
/// [`Writeable`]: crate::util::ser::Writeable
491+
/// [`CustomMessageReader`]: crate::ln::wire::CustomMessageReader
492+
#[macro_export]
463493
macro_rules! impl_writeable_msg {
464494
($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => {
465495
impl $crate::util::ser::Writeable for $st {
466496
fn write<W: $crate::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
467497
$( self.$field.write(w)?; )*
468-
encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
498+
$crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
469499
Ok(())
470500
}
471501
}
472502
impl $crate::util::ser::Readable for $st {
473503
fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
474504
$(let $field = $crate::util::ser::Readable::read(r)?;)*
475-
$(_init_tlv_field_var!($tlvfield, $fieldty);)*
476-
decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
505+
$($crate::_init_tlv_field_var!($tlvfield, $fieldty);)*
506+
$crate::decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
477507
Ok(Self {
478508
$($field),*,
479509
$($tlvfield),*
@@ -645,10 +675,10 @@ macro_rules! _init_and_read_tlv_fields {
645675
}
646676

647677
/// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
648-
/// If `$fieldty` is `required`, then `$field` is a required field that is not an Option nor a Vec.
678+
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
649679
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
650680
/// If `$fieldty` is `option`, then `$field` is optional field.
651-
/// If `$fieldty` is `vec_type`, then `$field` is a Vec, which needs to have its individual elements serialized.
681+
/// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
652682
///
653683
/// For example,
654684
/// ```

0 commit comments

Comments
 (0)