]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Avoid a redundant allocation in `InvoiceError` handling in one case
authorMatt Corallo <git@bluematt.me>
Fri, 20 Oct 2023 17:38:19 +0000 (17:38 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 21 Oct 2023 14:30:13 +0000 (14:30 +0000)
... by passing an owned `String`, rather than taking an `&str` and
`to_owned()`ing it.

lightning/src/ln/channelmanager.rs
lightning/src/offers/invoice_error.rs

index 4f3caef03e37efb8ca21e2f4d5f1d374c824e887..20dba4cf8e673eef4674489dd32eb46fc2030f51 100644 (file)
@@ -8967,10 +8967,10 @@ where
                                                                match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
                                                                        Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
                                                                        Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
-                                                                                       InvoiceError::from_str("Failed signing invoice")
+                                                                                       InvoiceError::from_string("Failed signing invoice".to_string())
                                                                        )),
                                                                        Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
-                                                                                       InvoiceError::from_str("Failed invoice signature verification")
+                                                                                       InvoiceError::from_string("Failed invoice signature verification".to_string())
                                                                        )),
                                                                });
                                                match response {
@@ -8986,7 +8986,7 @@ where
                        OffersMessage::Invoice(invoice) => {
                                match invoice.verify(expanded_key, secp_ctx) {
                                        Err(()) => {
-                                               Some(OffersMessage::InvoiceError(InvoiceError::from_str("Unrecognized invoice")))
+                                               Some(OffersMessage::InvoiceError(InvoiceError::from_string("Unrecognized invoice".to_owned())))
                                        },
                                        Ok(_) if invoice.invoice_features().requires_unknown_bits_from(&self.bolt12_invoice_features()) => {
                                                Some(OffersMessage::InvoiceError(Bolt12SemanticError::UnknownRequiredFeatures.into()))
@@ -8994,7 +8994,7 @@ where
                                        Ok(payment_id) => {
                                                if let Err(e) = self.send_payment_for_bolt12_invoice(&invoice, payment_id) {
                                                        log_trace!(self.logger, "Failed paying invoice: {:?}", e);
-                                                       Some(OffersMessage::InvoiceError(InvoiceError::from_str(&format!("{:?}", e))))
+                                                       Some(OffersMessage::InvoiceError(InvoiceError::from_string(format!("{:?}", e))))
                                                } else {
                                                        None
                                                }
index 441dae265cbb4fb9c9ed8d527e6b9b65c1e61ad1..5476ad551b7b627d5e1abac8d19fc836b673e54c 100644 (file)
@@ -50,10 +50,10 @@ pub struct ErroneousField {
 
 impl InvoiceError {
        /// Creates an [`InvoiceError`] with the given message.
-       pub fn from_str(s: &str) -> Self {
+       pub fn from_string(s: String) -> Self {
                Self {
                        erroneous_field: None,
-                       message: UntrustedString(s.to_string()),
+                       message: UntrustedString(s),
                }
        }
 }