@@ -457,20 +457,59 @@ 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
+ /// 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]
460
499
macro_rules! impl_writeable_msg {
461
500
( $st: ident, { $( $field: ident) ,* $( , ) * } , { $( ( $type: expr, $tlvfield: ident, $fieldty: tt) ) ,* $( , ) * } ) => {
462
501
impl $crate:: util:: ser:: Writeable for $st {
463
502
fn write<W : $crate:: util:: ser:: Writer >( & self , w: & mut W ) -> Result <( ) , $crate:: io:: Error > {
464
503
$( self . $field. write( w) ?; ) *
465
- encode_tlv_stream!( w, { $( ( $type, self . $tlvfield, $fieldty) ) ,* } ) ;
504
+ $crate :: encode_tlv_stream!( w, { $( ( $type, self . $tlvfield, $fieldty) ) ,* } ) ;
466
505
Ok ( ( ) )
467
506
}
468
507
}
469
508
impl $crate:: util:: ser:: Readable for $st {
470
509
fn read<R : $crate:: io:: Read >( r: & mut R ) -> Result <Self , $crate:: ln:: msgs:: DecodeError > {
471
510
$( 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) ) ,* } ) ;
474
513
Ok ( Self {
475
514
$( $field) ,* ,
476
515
$( $tlvfield) ,*
@@ -642,10 +681,10 @@ macro_rules! _init_and_read_tlv_fields {
642
681
}
643
682
644
683
/// 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`] .
646
685
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
647
686
/// 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.
649
688
///
650
689
/// For example,
651
690
/// ```
0 commit comments