@@ -460,20 +460,50 @@ macro_rules! _decode_tlv_stream_range {
460
460
} }
461
461
}
462
462
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]
463
493
macro_rules! impl_writeable_msg {
464
494
( $st: ident, { $( $field: ident) ,* $( , ) * } , { $( ( $type: expr, $tlvfield: ident, $fieldty: tt) ) ,* $( , ) * } ) => {
465
495
impl $crate:: util:: ser:: Writeable for $st {
466
496
fn write<W : $crate:: util:: ser:: Writer >( & self , w: & mut W ) -> Result <( ) , $crate:: io:: Error > {
467
497
$( self . $field. write( w) ?; ) *
468
- encode_tlv_stream!( w, { $( ( $type, self . $tlvfield, $fieldty) ) ,* } ) ;
498
+ $crate :: encode_tlv_stream!( w, { $( ( $type, self . $tlvfield, $fieldty) ) ,* } ) ;
469
499
Ok ( ( ) )
470
500
}
471
501
}
472
502
impl $crate:: util:: ser:: Readable for $st {
473
503
fn read<R : $crate:: io:: Read >( r: & mut R ) -> Result <Self , $crate:: ln:: msgs:: DecodeError > {
474
504
$( 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) ) ,* } ) ;
477
507
Ok ( Self {
478
508
$( $field) ,* ,
479
509
$( $tlvfield) ,*
@@ -645,10 +675,10 @@ macro_rules! _init_and_read_tlv_fields {
645
675
}
646
676
647
677
/// 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`] .
649
679
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
650
680
/// 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.
652
682
///
653
683
/// For example,
654
684
/// ```
0 commit comments