]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Make `offers::Amount` `Copy` and export it in bindings
authorMatt Corallo <git@bluematt.me>
Sat, 11 May 2024 18:51:48 +0000 (18:51 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 13 May 2024 18:55:22 +0000 (18:55 +0000)
`Amount` is less than two pointers long, so there's no reason it
shouldn't be `Copy`. Further, because its an enum, bindings can't
map a reference to it in an `Option`. Thus, here, we simply make it
`Copy` and return it in `Option`s rather than a reference to it.

lightning/src/offers/invoice.rs
lightning/src/offers/invoice_request.rs
lightning/src/offers/offer.rs

index 6f907d785c7747f6c1124274e01871f175bbf50d..d99e647e9dd495422866b298f8bfe0e0cb9db250 100644 (file)
@@ -697,7 +697,7 @@ macro_rules! invoice_accessors { ($self: ident, $contents: expr) => {
        ///
        /// [`Offer`]: crate::offers::offer::Offer
        /// [`Offer::amount`]: crate::offers::offer::Offer::amount
-       pub fn amount(&$self) -> Option<&Amount> {
+       pub fn amount(&$self) -> Option<Amount> {
                $contents.amount()
        }
 
@@ -944,7 +944,7 @@ impl InvoiceContents {
                }
        }
 
-       fn amount(&self) -> Option<&Amount> {
+       fn amount(&self) -> Option<Amount> {
                match self {
                        InvoiceContents::ForOffer { invoice_request, .. } =>
                                invoice_request.inner.offer.amount(),
@@ -1545,7 +1545,7 @@ mod tests {
                assert_eq!(unsigned_invoice.payer_metadata(), &[1; 32]);
                assert_eq!(unsigned_invoice.offer_chains(), Some(vec![ChainHash::using_genesis_block(Network::Bitcoin)]));
                assert_eq!(unsigned_invoice.metadata(), None);
-               assert_eq!(unsigned_invoice.amount(), Some(&Amount::Bitcoin { amount_msats: 1000 }));
+               assert_eq!(unsigned_invoice.amount(), Some(Amount::Bitcoin { amount_msats: 1000 }));
                assert_eq!(unsigned_invoice.description(), Some(PrintableString("")));
                assert_eq!(unsigned_invoice.offer_features(), Some(&OfferFeatures::empty()));
                assert_eq!(unsigned_invoice.absolute_expiry(), None);
@@ -1589,7 +1589,7 @@ mod tests {
                assert_eq!(invoice.payer_metadata(), &[1; 32]);
                assert_eq!(invoice.offer_chains(), Some(vec![ChainHash::using_genesis_block(Network::Bitcoin)]));
                assert_eq!(invoice.metadata(), None);
-               assert_eq!(invoice.amount(), Some(&Amount::Bitcoin { amount_msats: 1000 }));
+               assert_eq!(invoice.amount(), Some(Amount::Bitcoin { amount_msats: 1000 }));
                assert_eq!(invoice.description(), Some(PrintableString("")));
                assert_eq!(invoice.offer_features(), Some(&OfferFeatures::empty()));
                assert_eq!(invoice.absolute_expiry(), None);
index faeef26c82f2332b31f6c689e18cd49889cae2af..8b06ba9a5ed52f724413510a95f96c5291c95001 100644 (file)
@@ -1247,7 +1247,7 @@ mod tests {
                assert_eq!(unsigned_invoice_request.payer_metadata(), &[1; 32]);
                assert_eq!(unsigned_invoice_request.chains(), vec![ChainHash::using_genesis_block(Network::Bitcoin)]);
                assert_eq!(unsigned_invoice_request.metadata(), None);
-               assert_eq!(unsigned_invoice_request.amount(), Some(&Amount::Bitcoin { amount_msats: 1000 }));
+               assert_eq!(unsigned_invoice_request.amount(), Some(Amount::Bitcoin { amount_msats: 1000 }));
                assert_eq!(unsigned_invoice_request.description(), Some(PrintableString("")));
                assert_eq!(unsigned_invoice_request.offer_features(), &OfferFeatures::empty());
                assert_eq!(unsigned_invoice_request.absolute_expiry(), None);
@@ -1279,7 +1279,7 @@ mod tests {
                assert_eq!(invoice_request.payer_metadata(), &[1; 32]);
                assert_eq!(invoice_request.chains(), vec![ChainHash::using_genesis_block(Network::Bitcoin)]);
                assert_eq!(invoice_request.metadata(), None);
-               assert_eq!(invoice_request.amount(), Some(&Amount::Bitcoin { amount_msats: 1000 }));
+               assert_eq!(invoice_request.amount(), Some(Amount::Bitcoin { amount_msats: 1000 }));
                assert_eq!(invoice_request.description(), Some(PrintableString("")));
                assert_eq!(invoice_request.offer_features(), &OfferFeatures::empty());
                assert_eq!(invoice_request.absolute_expiry(), None);
index 6df9b30a4efa4d9d351ca06d1fa0a10e3a5bc47e..b37775ac0e2e42273d0dee24fd5c4ad5b914b526 100644 (file)
@@ -582,7 +582,7 @@ macro_rules! offer_accessors { ($self: ident, $contents: expr) => {
        }
 
        /// The minimum amount required for a successful payment of a single item.
-       pub fn amount(&$self) -> Option<&$crate::offers::offer::Amount> {
+       pub fn amount(&$self) -> Option<$crate::offers::offer::Amount> {
                $contents.amount()
        }
 
@@ -808,8 +808,8 @@ impl OfferContents {
                self.metadata.as_ref().and_then(|metadata| metadata.as_bytes())
        }
 
-       pub fn amount(&self) -> Option<&Amount> {
-               self.amount.as_ref()
+       pub fn amount(&self) -> Option<Amount> {
+               self.amount
        }
 
        pub fn description(&self) -> Option<PrintableString> {
@@ -982,7 +982,7 @@ impl Writeable for OfferContents {
 
 /// The minimum amount required for an item in an [`Offer`], denominated in either bitcoin or
 /// another currency.
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Copy, Debug, PartialEq)]
 pub enum Amount {
        /// An amount of bitcoin.
        Bitcoin {
@@ -1381,7 +1381,7 @@ mod tests {
                        .build()
                        .unwrap();
                let tlv_stream = offer.as_tlv_stream();
-               assert_eq!(offer.amount(), Some(&bitcoin_amount));
+               assert_eq!(offer.amount(), Some(bitcoin_amount));
                assert_eq!(tlv_stream.amount, Some(1000));
                assert_eq!(tlv_stream.currency, None);