Add optional lifetime to tlv_stream macro

Using the tlv_stream macro without a type needing a reference results in
a compilation error because of an unused lifetime parameter. To avoid
this, add an optional lifetime parameter to the macro. This allows for
experimental TLVs, which will be empty initially, and TLVs of entirely
primitive types.
This commit is contained in:
Jeffrey Czyz 2024-08-02 11:54:42 -05:00
parent bbdd8738a8
commit a77300129c
No known key found for this signature in database
GPG key ID: 912EF12EA67705F5
6 changed files with 8 additions and 8 deletions

View file

@ -1237,7 +1237,7 @@ impl TryFrom<Vec<u8>> for Bolt12Invoice {
/// Valid type range for invoice TLV records. /// Valid type range for invoice TLV records.
pub(super) const INVOICE_TYPES: core::ops::Range<u64> = 160..240; pub(super) const INVOICE_TYPES: core::ops::Range<u64> = 160..240;
tlv_stream!(InvoiceTlvStream, InvoiceTlvStreamRef, INVOICE_TYPES, { tlv_stream!(InvoiceTlvStream, InvoiceTlvStreamRef<'a>, INVOICE_TYPES, {
(160, paths: (Vec<BlindedPath>, WithoutLength, Iterable<'a, BlindedPathIter<'a>, BlindedPath>)), (160, paths: (Vec<BlindedPath>, WithoutLength, Iterable<'a, BlindedPathIter<'a>, BlindedPath>)),
(162, blindedpay: (Vec<BlindedPayInfo>, WithoutLength, Iterable<'a, BlindedPayInfoIter<'a>, BlindedPayInfo>)), (162, blindedpay: (Vec<BlindedPayInfo>, WithoutLength, Iterable<'a, BlindedPayInfoIter<'a>, BlindedPayInfo>)),
(164, created_at: (u64, HighZeroBytesDroppedBigSize)), (164, created_at: (u64, HighZeroBytesDroppedBigSize)),

View file

@ -1061,7 +1061,7 @@ pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;
// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for // This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below. // InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, { tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef<'a>, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash), (80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)), (82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)), (84, features: (InvoiceRequestFeatures, WithoutLength)),

View file

@ -21,7 +21,7 @@ use crate::prelude::*;
/// Valid type range for signature TLV records. /// Valid type range for signature TLV records.
const SIGNATURE_TYPES: core::ops::RangeInclusive<u64> = 240..=1000; const SIGNATURE_TYPES: core::ops::RangeInclusive<u64> = 240..=1000;
tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, { tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef<'a>, SIGNATURE_TYPES, {
(240, signature: Signature), (240, signature: Signature),
}); });

View file

@ -1077,7 +1077,7 @@ const OFFER_METADATA_TYPE: u64 = 4;
/// TLV record type for [`Offer::issuer_signing_pubkey`]. /// TLV record type for [`Offer::issuer_signing_pubkey`].
const OFFER_ISSUER_ID_TYPE: u64 = 22; const OFFER_ISSUER_ID_TYPE: u64 = 22;
tlv_stream!(OfferTlvStream, OfferTlvStreamRef, OFFER_TYPES, { tlv_stream!(OfferTlvStream, OfferTlvStreamRef<'a>, OFFER_TYPES, {
(2, chains: (Vec<ChainHash>, WithoutLength)), (2, chains: (Vec<ChainHash>, WithoutLength)),
(OFFER_METADATA_TYPE, metadata: (Vec<u8>, WithoutLength)), (OFFER_METADATA_TYPE, metadata: (Vec<u8>, WithoutLength)),
(6, currency: CurrencyCode), (6, currency: CurrencyCode),

View file

@ -30,6 +30,6 @@ pub(super) struct PayerContents(pub Metadata);
/// [`Refund::payer_metadata`]: crate::offers::refund::Refund::payer_metadata /// [`Refund::payer_metadata`]: crate::offers::refund::Refund::payer_metadata
pub(super) const PAYER_METADATA_TYPE: u64 = 0; pub(super) const PAYER_METADATA_TYPE: u64 = 0;
tlv_stream!(PayerTlvStream, PayerTlvStreamRef, 0..1, { tlv_stream!(PayerTlvStream, PayerTlvStreamRef<'a>, 0..1, {
(PAYER_METADATA_TYPE, metadata: (Vec<u8>, WithoutLength)), (PAYER_METADATA_TYPE, metadata: (Vec<u8>, WithoutLength)),
}); });

View file

@ -952,7 +952,7 @@ macro_rules! impl_writeable_tlv_based {
/// [`Readable`]: crate::util::ser::Readable /// [`Readable`]: crate::util::ser::Readable
/// [`Writeable`]: crate::util::ser::Writeable /// [`Writeable`]: crate::util::ser::Writeable
macro_rules! tlv_stream { macro_rules! tlv_stream {
($name:ident, $nameref:ident, $range:expr, { ($name:ident, $nameref:ident $(<$lifetime:lifetime>)?, $range:expr, {
$(($type:expr, $field:ident : $fieldty:tt)),* $(,)* $(($type:expr, $field:ident : $fieldty:tt)),* $(,)*
}) => { }) => {
#[derive(Debug)] #[derive(Debug)]
@ -964,13 +964,13 @@ macro_rules! tlv_stream {
#[cfg_attr(test, derive(PartialEq))] #[cfg_attr(test, derive(PartialEq))]
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct $nameref<'a> { pub(crate) struct $nameref<$($lifetime)*> {
$( $(
pub(super) $field: Option<tlv_record_ref_type!($fieldty)>, pub(super) $field: Option<tlv_record_ref_type!($fieldty)>,
)* )*
} }
impl<'a> $crate::util::ser::Writeable for $nameref<'a> { impl<$($lifetime)*> $crate::util::ser::Writeable for $nameref<$($lifetime)*> {
fn write<W: $crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> { fn write<W: $crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
encode_tlv_stream!(writer, { encode_tlv_stream!(writer, {
$(($type, self.$field, (option, encoding: $fieldty))),* $(($type, self.$field, (option, encoding: $fieldty))),*