Skip to content

Commit 99fe52e

Browse files
committed
Expose impl_writeable_msg
1 parent 153b048 commit 99fe52e

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,20 +457,50 @@ macro_rules! _decode_tlv_stream_range {
457457
} }
458458
}
459459

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

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

0 commit comments

Comments
 (0)