]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Impl `PartialEq`/`Eq` for `Offer`/`Refund`
authorElias Rohrer <dev@tnull.de>
Fri, 8 Mar 2024 09:24:50 +0000 (10:24 +0100)
committerElias Rohrer <dev@tnull.de>
Fri, 8 Mar 2024 16:35:20 +0000 (17:35 +0100)
We add custom implementations based on comparing the `bytes` for
`Offer`/`Refund` themselves as this is sufficient and should be faster
than comapring all fields in the worst case.

lightning/src/offers/offer.rs
lightning/src/offers/refund.rs

index 802813fd180f70d9f6006cdc126a69f5c5f8f6e3..d2be5b05677d32068db9ef1a658acb575ac5dec0 100644 (file)
@@ -363,7 +363,6 @@ impl<'a, M: MetadataStrategy, T: secp256k1::Signing> OfferBuilder<'a, M, T> {
 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
 #[derive(Clone, Debug)]
-#[cfg_attr(test, derive(PartialEq))]
 pub struct Offer {
        // The serialized offer. Needed when creating an `InvoiceRequest` if the offer contains unknown
        // fields.
@@ -584,6 +583,14 @@ impl AsRef<[u8]> for Offer {
        }
 }
 
+impl PartialEq for Offer {
+       fn eq(&self, other: &Self) -> bool {
+               self.bytes.eq(&other.bytes)
+       }
+}
+
+impl Eq for Offer {}
+
 impl OfferContents {
        pub fn chains(&self) -> Vec<ChainHash> {
                self.chains.as_ref().cloned().unwrap_or_else(|| vec![self.implied_chain()])
index ba3ab1d1ef3bad508bc14ab4e7b933c00269d7e3..db42c7457edfdab702eabd248cb1d719ce0e96c1 100644 (file)
@@ -317,7 +317,6 @@ impl<'a, T: secp256k1::Signing> RefundBuilder<'a, T> {
 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
 /// [`Offer`]: crate::offers::offer::Offer
 #[derive(Clone, Debug)]
-#[cfg_attr(test, derive(PartialEq))]
 pub struct Refund {
        pub(super) bytes: Vec<u8>,
        pub(super) contents: RefundContents,
@@ -539,6 +538,14 @@ impl AsRef<[u8]> for Refund {
        }
 }
 
+impl PartialEq for Refund {
+       fn eq(&self, other: &Self) -> bool {
+               self.bytes.eq(&other.bytes)
+       }
+}
+
+impl Eq for Refund {}
+
 impl RefundContents {
        pub fn description(&self) -> PrintableString {
                PrintableString(&self.description)