Skip to content

Commit d325fa7

Browse files
committed
Fix docs and export impl_tlv_based_enum variants
1 parent d78d06c commit d325fa7

File tree

1 file changed

+60
-54
lines changed

1 file changed

+60
-54
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,13 @@ macro_rules! impl_writeable {
501501
}
502502

503503
/// Write out two bytes to indicate the version of an object.
504+
///
504505
/// $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+
///
508509
/// $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`].
511511
///
512512
/// Updates to either `$this_version` or `$min_version_that_can_read_this` should be included in
513513
/// release notes.
@@ -567,6 +567,7 @@ macro_rules! read_tlv_fields {
567567
}
568568

569569
/// Initializes the struct fields.
570+
///
570571
/// This is exported for use by other exported macros, do not use directly.
571572
#[doc(hidden)]
572573
#[macro_export]
@@ -589,6 +590,7 @@ macro_rules! _init_tlv_based_struct_field {
589590
}
590591

591592
/// Initializes the variable we are going to read the TLV into.
593+
///
592594
/// This is exported for use by other exported macros, do not use directly.
593595
#[doc(hidden)]
594596
#[macro_export]
@@ -611,6 +613,7 @@ macro_rules! _init_tlv_field_var {
611613
}
612614

613615
/// Equivalent to running [`_init_tlv_field_var`] then [`read_tlv_fields`].
616+
///
614617
/// This is exported for use by other exported macros, do not use directly.
615618
#[doc(hidden)]
616619
#[macro_export]
@@ -795,30 +798,34 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
795798
}
796799
}
797800

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`].
805813
///
806-
/// [`MaybeReadable`]: crate::util::ser::MaybeReadable
814+
/// [`Readable`]: crate::util::ser::Readable
807815
/// [`Writeable`]: crate::util::ser::Writeable
808816
/// [`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 {
811819
($st: ident, $(($variant_id: expr, $variant_name: ident) =>
812820
{$(($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)),* $(,)*) => {
816823
_impl_writeable_tlv_based_enum_common!($st,
817824
$(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
818-
$($(($tuple_variant_id, $tuple_variant_name)),*)*);
825+
$(($tuple_variant_id, $tuple_variant_name)),*);
819826

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> {
822829
let id: u8 = $crate::util::ser::Readable::read(reader)?;
823830
match id {
824831
$($variant_id => {
@@ -828,51 +835,51 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
828835
_init_and_read_tlv_fields!(reader, {
829836
$(($type, $field, $fieldty)),*
830837
});
831-
Ok(Some($st::$variant_name {
838+
Ok($st::$variant_name {
832839
$(
833840
$field: _init_tlv_based_struct_field!($field, $fieldty)
834841
),*
835-
}))
842+
})
836843
};
837844
f()
838845
}),*
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+
},
844852
}
845853
}
846854
}
847-
848855
}
849856
}
850857

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.
861860
///
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
863867
/// [`Writeable`]: crate::util::ser::Writeable
864868
/// [`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 {
866872
($st: ident, $(($variant_id: expr, $variant_name: ident) =>
867873
{$(($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)),* $(,)*)*) => {
870877
_impl_writeable_tlv_based_enum_common!($st,
871878
$(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
872-
$(($tuple_variant_id, $tuple_variant_name)),*);
879+
$($(($tuple_variant_id, $tuple_variant_name)),*)*);
873880

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> {
876883
let id: u8 = $crate::util::ser::Readable::read(reader)?;
877884
match id {
878885
$($variant_id => {
@@ -882,20 +889,19 @@ macro_rules! impl_writeable_tlv_based_enum {
882889
_init_and_read_tlv_fields!(reader, {
883890
$(($type, $field, $fieldty)),*
884891
});
885-
Ok($st::$variant_name {
892+
Ok(Some($st::$variant_name {
886893
$(
887894
$field: _init_tlv_based_struct_field!($field, $fieldty)
888895
),*
889-
})
896+
}))
890897
};
891898
f()
892899
}),*
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),
899905
}
900906
}
901907
}

0 commit comments

Comments
 (0)