@@ -457,20 +457,50 @@ macro_rules! _decode_tlv_stream_range {
457
457
} }
458
458
}
459
459
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]
460
490
macro_rules! impl_writeable_msg {
461
491
( $st: ident, { $( $field: ident) ,* $( , ) * } , { $( ( $type: expr, $tlvfield: ident, $fieldty: tt) ) ,* $( , ) * } ) => {
462
492
impl $crate:: util:: ser:: Writeable for $st {
463
493
fn write<W : $crate:: util:: ser:: Writer >( & self , w: & mut W ) -> Result <( ) , $crate:: io:: Error > {
464
494
$( self . $field. write( w) ?; ) *
465
- encode_tlv_stream!( w, { $( ( $type, self . $tlvfield, $fieldty) ) ,* } ) ;
495
+ $crate :: encode_tlv_stream!( w, { $( ( $type, self . $tlvfield, $fieldty) ) ,* } ) ;
466
496
Ok ( ( ) )
467
497
}
468
498
}
469
499
impl $crate:: util:: ser:: Readable for $st {
470
500
fn read<R : $crate:: io:: Read >( r: & mut R ) -> Result <Self , $crate:: ln:: msgs:: DecodeError > {
471
501
$( 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) ) ,* } ) ;
474
504
Ok ( Self {
475
505
$( $field) ,* ,
476
506
$( $tlvfield) ,*
@@ -642,10 +672,10 @@ macro_rules! _init_and_read_tlv_fields {
642
672
}
643
673
644
674
/// 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`] .
646
676
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
647
677
/// 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.
649
679
///
650
680
/// For example,
651
681
/// ```
0 commit comments