From 2dc418d9d1ea7586040aeec4b01ef1e65631f399 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Sun, 21 Jan 2024 09:57:09 +0100 Subject: [PATCH] Remove generics --- webln-js/src/lib.rs | 7 +++---- webln/src/lib.rs | 44 ++++++++++---------------------------------- 2 files changed, 13 insertions(+), 38 deletions(-) diff --git a/webln-js/src/lib.rs b/webln-js/src/lib.rs index 329938b..87c7560 100644 --- a/webln-js/src/lib.rs +++ b/webln-js/src/lib.rs @@ -8,7 +8,6 @@ extern crate alloc; -use alloc::string::String; use core::ops::Deref; use wasm_bindgen::prelude::*; @@ -107,7 +106,7 @@ impl JsWebLN { /// Request that the user sends a payment for an invoice. #[wasm_bindgen(js_name = sendPayment)] - pub async fn send_payment(&self, invoice: String) -> Result { + pub async fn send_payment(&self, invoice: &str) -> Result { Ok(self .inner .send_payment(invoice) @@ -121,7 +120,7 @@ impl JsWebLN { /// This is useful when paying HOLD Invoices. There is no guarantee that the payment will be successfully sent to the receiver. /// It's up to the receiver to check whether or not the invoice has been paid. #[wasm_bindgen(js_name = sendPaymentAsync)] - pub async fn send_payment_async(&self, invoice: String) -> Result<()> { + pub async fn send_payment_async(&self, invoice: &str) -> Result<()> { self.inner .send_payment_async(invoice) .await @@ -130,7 +129,7 @@ impl JsWebLN { /// Request that the user signs an arbitrary string message. #[wasm_bindgen(js_name = signMessage)] - pub async fn sign_message(&self, message: String) -> Result { + pub async fn sign_message(&self, message: &str) -> Result { Ok(self .inner .sign_message(message) diff --git a/webln/src/lib.rs b/webln/src/lib.rs index 0333a0d..bc1fef5 100644 --- a/webln/src/lib.rs +++ b/webln/src/lib.rs @@ -122,12 +122,9 @@ pub enum GetInfoMethod { Other(String), } -impl From for GetInfoMethod -where - S: AsRef, -{ - fn from(method: S) -> Self { - match method.as_ref() { +impl From<&str> for GetInfoMethod { + fn from(method: &str) -> Self { + match method { IS_ENABLED => Self::IsEnabled, ENABLE => Self::Enable, GET_INFO => Self::GetInfo, @@ -246,11 +243,8 @@ impl RequestInvoiceArgs { } /// Set default memo - pub fn default_memo(mut self, default_memo: S) -> Self - where - S: Into, - { - self.default_memo = Some(default_memo.into()); + pub fn default_memo(mut self, default_memo: String) -> Self { + self.default_memo = Some(default_memo); self } } @@ -349,11 +343,7 @@ impl WebLN { Ok(Self { webln_obj }) } - fn get_func(&self, obj: &Object, name: S) -> Result - where - S: AsRef, - { - let name: &str = name.as_ref(); + fn get_func(&self, obj: &Object, name: &str) -> Result { let val: JsValue = Reflect::get(obj, &JsValue::from_str(name)) .map_err(|_| Error::NamespaceNotFound(name.to_string()))?; val.dyn_into() @@ -407,7 +397,7 @@ impl WebLN { let methods: Vec = methods_array .into_iter() .filter_map(|m| m.as_string()) - .map(GetInfoMethod::from) + .map(|m| GetInfoMethod::from(m.as_str())) .collect(); Ok(GetInfoResponse { @@ -474,12 +464,7 @@ impl WebLN { } /// Request that the user sends a payment for an invoice. - pub async fn send_payment(&self, invoice: S) -> Result - where - S: AsRef, - { - let invoice: &str = invoice.as_ref(); - + pub async fn send_payment(&self, invoice: &str) -> Result { // `lightning-invoice` increase too much the WASM binary size // For now just check if invoice is not empty if invoice.is_empty() { @@ -502,12 +487,7 @@ impl WebLN { /// The payment will only be initiated and will not wait for a preimage to be returned. /// This is useful when paying HOLD Invoices. There is no guarantee that the payment will be successfully sent to the receiver. /// It's up to the receiver to check whether or not the invoice has been paid. - pub async fn send_payment_async(&self, invoice: S) -> Result<(), Error> - where - S: AsRef, - { - let invoice: &str = invoice.as_ref(); - + pub async fn send_payment_async(&self, invoice: &str) -> Result<(), Error> { // `lightning-invoice` increase too much the WASM binary size // For now just check if invoice is not empty if invoice.is_empty() { @@ -526,11 +506,7 @@ impl WebLN { } /// Request that the user signs an arbitrary string message. - pub async fn sign_message(&self, message: S) -> Result - where - S: AsRef, - { - let message: &str = message.as_ref(); + pub async fn sign_message(&self, message: &str) -> Result { let func: Function = self.get_func(&self.webln_obj, SIGN_MESSAGE)?; let promise: Promise = Promise::resolve(&func.call1(&self.webln_obj, &message.into())?); let result: JsValue = JsFuture::from(promise).await?;