Skip to content

Commit 66b8611

Browse files
committed
Avoid allocating during LSPSMethod serialization
1 parent be3cc40 commit 66b8611

File tree

1 file changed

+17
-18
lines changed
  • lightning-liquidity/src/lsps0

1 file changed

+17
-18
lines changed

lightning-liquidity/src/lsps0/ser.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ use crate::lsps1::msgs::{
1414
use crate::lsps2::msgs::{
1515
LSPS2Message, LSPS2Request, LSPS2Response, LSPS2_BUY_METHOD_NAME, LSPS2_GET_INFO_METHOD_NAME,
1616
};
17-
use crate::prelude::{HashMap, String, ToString};
17+
use crate::prelude::{HashMap, String};
1818

1919
use lightning::ln::msgs::LightningError;
2020
use lightning::ln::wire;
2121
use lightning::util::ser::WithoutLength;
2222

2323
use bitcoin::secp256k1::PublicKey;
2424

25-
use core::fmt::{self, Display};
25+
use core::fmt;
2626
use core::str::FromStr;
2727

2828
use serde::de::{self, MapAccess, Visitor};
@@ -53,6 +53,19 @@ pub(crate) enum LSPSMethod {
5353
LSPS2Buy,
5454
}
5555

56+
impl LSPSMethod {
57+
fn as_static_str(&self) -> &'static str {
58+
match self {
59+
Self::LSPS0ListProtocols => LSPS0_LISTPROTOCOLS_METHOD_NAME,
60+
Self::LSPS1GetInfo => LSPS1_GET_INFO_METHOD_NAME,
61+
Self::LSPS1CreateOrder => LSPS1_CREATE_ORDER_METHOD_NAME,
62+
Self::LSPS1GetOrder => LSPS1_GET_ORDER_METHOD_NAME,
63+
Self::LSPS2GetInfo => LSPS2_GET_INFO_METHOD_NAME,
64+
Self::LSPS2Buy => LSPS2_BUY_METHOD_NAME,
65+
}
66+
}
67+
}
68+
5669
impl FromStr for LSPSMethod {
5770
type Err = &'static str;
5871
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -68,20 +81,6 @@ impl FromStr for LSPSMethod {
6881
}
6982
}
7083

71-
impl Display for LSPSMethod {
72-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73-
let s = match self {
74-
Self::LSPS0ListProtocols => LSPS0_LISTPROTOCOLS_METHOD_NAME,
75-
Self::LSPS1GetInfo => LSPS1_GET_INFO_METHOD_NAME,
76-
Self::LSPS1CreateOrder => LSPS1_CREATE_ORDER_METHOD_NAME,
77-
Self::LSPS1GetOrder => LSPS1_GET_ORDER_METHOD_NAME,
78-
Self::LSPS2GetInfo => LSPS2_GET_INFO_METHOD_NAME,
79-
Self::LSPS2Buy => LSPS2_BUY_METHOD_NAME,
80-
};
81-
write!(f, "{}", s)
82-
}
83-
}
84-
8584
impl From<&LSPS0Request> for LSPSMethod {
8685
fn from(value: &LSPS0Request) -> Self {
8786
match value {
@@ -124,7 +123,7 @@ impl Serialize for LSPSMethod {
124123
where
125124
S: serde::Serializer,
126125
{
127-
serializer.serialize_str(&self.to_string())
126+
serializer.serialize_str(&self.as_static_str())
128127
}
129128
}
130129

@@ -407,7 +406,7 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
407406
if let Some(method) = method {
408407
return Err(de::Error::custom(format!(
409408
"Received unknown notification: {}",
410-
method
409+
method.as_static_str()
411410
)));
412411
} else {
413412
if let Some(error) = error {

0 commit comments

Comments
 (0)