Skip to content

Commit 8e41db0

Browse files
committed
Expose impl_writeable_msg
1 parent 153b048 commit 8e41db0

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,20 +457,59 @@ 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+
/// If `$fieldty` is `required`, then `$tlvfield` is a required field that is not an [`Option`] nor a [`Vec`].
466+
/// If `$fieldty` is `(default_value, $default)`, then `$tlvfield` will be set to `$default` if not present.
467+
/// If `$fieldty` is `option`, then `$tlvfield` is optional field.
468+
/// If `$fieldty` is `vec_type`, then `$tlvfield` is a [`Vec`], which needs to have its individual elements serialized.
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_integer: u32,
478+
/// pub tlv_default_integer: u32,
479+
/// pub tlv_optional_integer: Option<u32>,
480+
/// pub tlv_vec_type_integer: Vec<u32>,
481+
/// }
482+
///
483+
/// impl_writeable_msg!(MyCustomMessage, {
484+
/// field_1,
485+
/// field_2,
486+
/// field_3
487+
/// }, {
488+
/// (0, tlv_integer, required),
489+
/// (1, tlv_default_integer, (default_value, 7)),
490+
/// (2, tlv_optional_integer, option),
491+
/// (3, tlv_vec_type_integer, vec_type),
492+
/// });
493+
/// ```
494+
///
495+
/// [`Readable`]: crate::util::ser::Readable
496+
/// [`Writeable`]: crate::util::ser::Writeable
497+
/// [`CustomMessageReader`]: crate::ln::wire::CustomMessageReader
498+
#[macro_export]
460499
macro_rules! impl_writeable_msg {
461500
($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => {
462501
impl $crate::util::ser::Writeable for $st {
463502
fn write<W: $crate::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
464503
$( self.$field.write(w)?; )*
465-
encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
504+
$crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
466505
Ok(())
467506
}
468507
}
469508
impl $crate::util::ser::Readable for $st {
470509
fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
471510
$(let $field = $crate::util::ser::Readable::read(r)?;)*
472-
$(_init_tlv_field_var!($tlvfield, $fieldty);)*
473-
decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
511+
$($crate::_init_tlv_field_var!($tlvfield, $fieldty);)*
512+
$crate::decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
474513
Ok(Self {
475514
$($field),*,
476515
$($tlvfield),*
@@ -642,10 +681,10 @@ macro_rules! _init_and_read_tlv_fields {
642681
}
643682

644683
/// 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.
684+
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
646685
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
647686
/// 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.
687+
/// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
649688
///
650689
/// For example,
651690
/// ```

0 commit comments

Comments
 (0)