@@ -501,13 +501,13 @@ macro_rules! impl_writeable {
501
501
}
502
502
503
503
/// Write out two bytes to indicate the version of an object.
504
+ ///
504
505
/// $this_version represents a unique version of a type. Incremented whenever the type's
505
- /// serialization format has changed or has a new interpretation. Used by a type's
506
- /// reader to determine how to interpret fields or if it can understand a serialized
507
- /// object.
506
+ /// serialization format has changed or has a new interpretation. Used by a type's reader to
507
+ /// determine how to interpret fields or if it can understand a serialized object.
508
+ ///
508
509
/// $min_version_that_can_read_this is the minimum reader version which can understand this
509
- /// serialized object. Previous versions will simply err with a
510
- /// [`DecodeError::UnknownVersion`].
510
+ /// serialized object. Previous versions will simply err with a [`DecodeError::UnknownVersion`].
511
511
///
512
512
/// Updates to either `$this_version` or `$min_version_that_can_read_this` should be included in
513
513
/// release notes.
@@ -567,6 +567,7 @@ macro_rules! read_tlv_fields {
567
567
}
568
568
569
569
/// Initializes the struct fields.
570
+ ///
570
571
/// This is exported for use by other exported macros, do not use directly.
571
572
#[ doc( hidden) ]
572
573
#[ macro_export]
@@ -589,6 +590,7 @@ macro_rules! _init_tlv_based_struct_field {
589
590
}
590
591
591
592
/// Initializes the variable we are going to read the TLV into.
593
+ ///
592
594
/// This is exported for use by other exported macros, do not use directly.
593
595
#[ doc( hidden) ]
594
596
#[ macro_export]
@@ -611,6 +613,7 @@ macro_rules! _init_tlv_field_var {
611
613
}
612
614
613
615
/// Equivalent to running [`_init_tlv_field_var`] then [`read_tlv_fields`].
616
+ ///
614
617
/// This is exported for use by other exported macros, do not use directly.
615
618
#[ doc( hidden) ]
616
619
#[ macro_export]
@@ -795,30 +798,34 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
795
798
}
796
799
}
797
800
798
- /// Implement [`MaybeReadable`] and [`Writeable`] for an enum, with struct variants stored as TLVs and
799
- /// tuple variants stored directly.
800
- ///
801
- /// This is largely identical to [`impl_writeable_tlv_based_enum`], except that odd variants will
802
- /// return `Ok(None)` instead of `Err(`[`DecodeError::UnknownRequiredFeature`]`)`. It should generally be preferred
803
- /// when [`MaybeReadable`] is practical instead of just [`Readable`] as it provides an upgrade path for
804
- /// new variants to be added which are simply ignored by existing clients.
801
+ /// Implement [`Readable`] and [`Writeable`] for an enum, with struct variants stored as TLVs and tuple
802
+ /// variants stored directly.
803
+ /// The format is, for example
804
+ /// ```ignore
805
+ /// impl_writeable_tlv_based_enum!(EnumName,
806
+ /// (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)},
807
+ /// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type)};
808
+ /// (2, TupleVariantA), (3, TupleVariantB),
809
+ /// );
810
+ /// ```
811
+ /// The type is written as a single byte, followed by any variant data.
812
+ /// Attempts to read an unknown type byte result in [`DecodeError::UnknownRequiredFeature`].
805
813
///
806
- /// [`MaybeReadable `]: crate::util::ser::MaybeReadable
814
+ /// [`Readable `]: crate::util::ser::Readable
807
815
/// [`Writeable`]: crate::util::ser::Writeable
808
816
/// [`DecodeError::UnknownRequiredFeature`]: crate::ln::msgs::DecodeError::UnknownRequiredFeature
809
- /// [`Readable`]: crate::util::ser::Readable
810
- macro_rules! impl_writeable_tlv_based_enum_upgradable {
817
+ # [ macro_export ]
818
+ macro_rules! impl_writeable_tlv_based_enum {
811
819
( $st: ident, $( ( $variant_id: expr, $variant_name: ident) =>
812
820
{ $( ( $type: expr, $field: ident, $fieldty: tt) ) ,* $( , ) * }
813
- ) ,* $( , ) *
814
- $( ;
815
- $( ( $tuple_variant_id: expr, $tuple_variant_name: ident) ) ,* $( , ) * ) * ) => {
821
+ ) ,* $( , ) * ;
822
+ $( ( $tuple_variant_id: expr, $tuple_variant_name: ident) ) ,* $( , ) * ) => {
816
823
_impl_writeable_tlv_based_enum_common!( $st,
817
824
$( ( $variant_id, $variant_name) => { $( ( $type, $field, $fieldty) ) ,* } ) ,* ;
818
- $( $ ( ( $tuple_variant_id, $tuple_variant_name) ) ,* ) * ) ;
825
+ $( ( $tuple_variant_id, $tuple_variant_name) ) ,* ) ;
819
826
820
- impl $crate:: util:: ser:: MaybeReadable for $st {
821
- fn read<R : $crate:: io:: Read >( reader: & mut R ) -> Result <Option < Self > , $crate:: ln:: msgs:: DecodeError > {
827
+ impl $crate:: util:: ser:: Readable for $st {
828
+ fn read<R : $crate:: io:: Read >( reader: & mut R ) -> Result <Self , $crate:: ln:: msgs:: DecodeError > {
822
829
let id: u8 = $crate:: util:: ser:: Readable :: read( reader) ?;
823
830
match id {
824
831
$( $variant_id => {
@@ -828,51 +835,51 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
828
835
_init_and_read_tlv_fields!( reader, {
829
836
$( ( $type, $field, $fieldty) ) ,*
830
837
} ) ;
831
- Ok ( Some ( $st:: $variant_name {
838
+ Ok ( $st:: $variant_name {
832
839
$(
833
840
$field: _init_tlv_based_struct_field!( $field, $fieldty)
834
841
) ,*
835
- } ) )
842
+ } )
836
843
} ;
837
844
f( )
838
845
} ) ,*
839
- $( $( $tuple_variant_id => {
840
- Ok ( Some ( $st:: $tuple_variant_name( Readable :: read( reader) ?) ) )
841
- } ) ,* ) *
842
- _ if id % 2 == 1 => Ok ( None ) ,
843
- _ => Err ( DecodeError :: UnknownRequiredFeature ) ,
846
+ $( $tuple_variant_id => {
847
+ Ok ( $st:: $tuple_variant_name( Readable :: read( reader) ?) )
848
+ } ) ,*
849
+ _ => {
850
+ Err ( $crate:: ln:: msgs:: DecodeError :: UnknownRequiredFeature )
851
+ } ,
844
852
}
845
853
}
846
854
}
847
-
848
855
}
849
856
}
850
857
851
- /// Implement [`Readable`] and [`Writeable`] for an enum, with struct variants stored as TLVs and tuple
852
- /// variants stored directly.
853
- /// The format is, for example
854
- /// impl_writeable_tlv_based_enum!(EnumName,
855
- /// (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)},
856
- /// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type)};
857
- /// (2, TupleVariantA), (3, TupleVariantB),
858
- /// );
859
- /// The type is written as a single byte, followed by any variant data.
860
- /// Attempts to read an unknown type byte result in [`DecodeError::UnknownRequiredFeature`].
858
+ /// Implement [`MaybeReadable`] and [`Writeable`] for an enum, with struct variants stored as TLVs and
859
+ /// tuple variants stored directly.
861
860
///
862
- /// [`Readable`]: crate::util::ser::Readable
861
+ /// This is largely identical to [`impl_writeable_tlv_based_enum`], except that odd variants will
862
+ /// return `Ok(None)` instead of `Err(`[`DecodeError::UnknownRequiredFeature`]`)`. It should generally be preferred
863
+ /// when [`MaybeReadable`] is practical instead of just [`Readable`] as it provides an upgrade path for
864
+ /// new variants to be added which are simply ignored by existing clients.
865
+ ///
866
+ /// [`MaybeReadable`]: crate::util::ser::MaybeReadable
863
867
/// [`Writeable`]: crate::util::ser::Writeable
864
868
/// [`DecodeError::UnknownRequiredFeature`]: crate::ln::msgs::DecodeError::UnknownRequiredFeature
865
- macro_rules! impl_writeable_tlv_based_enum {
869
+ /// [`Readable`]: crate::util::ser::Readable
870
+ #[ macro_export]
871
+ macro_rules! impl_writeable_tlv_based_enum_upgradable {
866
872
( $st: ident, $( ( $variant_id: expr, $variant_name: ident) =>
867
873
{ $( ( $type: expr, $field: ident, $fieldty: tt) ) ,* $( , ) * }
868
- ) ,* $( , ) * ;
869
- $( ( $tuple_variant_id: expr, $tuple_variant_name: ident) ) ,* $( , ) * ) => {
874
+ ) ,* $( , ) *
875
+ $( ;
876
+ $( ( $tuple_variant_id: expr, $tuple_variant_name: ident) ) ,* $( , ) * ) * ) => {
870
877
_impl_writeable_tlv_based_enum_common!( $st,
871
878
$( ( $variant_id, $variant_name) => { $( ( $type, $field, $fieldty) ) ,* } ) ,* ;
872
- $( ( $tuple_variant_id, $tuple_variant_name) ) ,* ) ;
879
+ $( $ ( ( $tuple_variant_id, $tuple_variant_name) ) ,* ) * ) ;
873
880
874
- impl $crate:: util:: ser:: Readable for $st {
875
- fn read<R : $crate:: io:: Read >( reader: & mut R ) -> Result <Self , $crate:: ln:: msgs:: DecodeError > {
881
+ impl $crate:: util:: ser:: MaybeReadable for $st {
882
+ fn read<R : $crate:: io:: Read >( reader: & mut R ) -> Result <Option < Self > , $crate:: ln:: msgs:: DecodeError > {
876
883
let id: u8 = $crate:: util:: ser:: Readable :: read( reader) ?;
877
884
match id {
878
885
$( $variant_id => {
@@ -882,20 +889,19 @@ macro_rules! impl_writeable_tlv_based_enum {
882
889
_init_and_read_tlv_fields!( reader, {
883
890
$( ( $type, $field, $fieldty) ) ,*
884
891
} ) ;
885
- Ok ( $st:: $variant_name {
892
+ Ok ( Some ( $st:: $variant_name {
886
893
$(
887
894
$field: _init_tlv_based_struct_field!( $field, $fieldty)
888
895
) ,*
889
- } )
896
+ } ) )
890
897
} ;
891
898
f( )
892
899
} ) ,*
893
- $( $tuple_variant_id => {
894
- Ok ( $st:: $tuple_variant_name( Readable :: read( reader) ?) )
895
- } ) ,*
896
- _ => {
897
- Err ( $crate:: ln:: msgs:: DecodeError :: UnknownRequiredFeature )
898
- } ,
900
+ $( $( $tuple_variant_id => {
901
+ Ok ( Some ( $st:: $tuple_variant_name( Readable :: read( reader) ?) ) )
902
+ } ) ,* ) *
903
+ _ if id % 2 == 1 => Ok ( None ) ,
904
+ _ => Err ( DecodeError :: UnknownRequiredFeature ) ,
899
905
}
900
906
}
901
907
}
0 commit comments