Skip to content

Commit 622bf1c

Browse files
committed
nostr: avoid unnecessary allocation in RelayInformationDocument::get method
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 1ee82f9 commit 622bf1c

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

crates/nostr/src/nips/nip11.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use alloc::vec::Vec;
1212
use core::fmt;
1313
use std::net::SocketAddr;
1414

15+
use reqwest::Client;
1516
#[cfg(not(target_arch = "wasm32"))]
1617
use reqwest::Proxy;
1718

@@ -188,9 +189,7 @@ impl RelayInformationDocument {
188189
/// Get Relay Information Document
189190
///
190191
/// **Proxy is ignored for WASM targets!**
191-
pub async fn get(url: Url, _proxy: Option<SocketAddr>) -> Result<Self, Error> {
192-
use reqwest::Client;
193-
192+
pub async fn get(mut url: Url, _proxy: Option<SocketAddr>) -> Result<Self, Error> {
194193
#[cfg(not(target_arch = "wasm32"))]
195194
let client: Client = {
196195
let mut builder = Client::builder();
@@ -204,10 +203,8 @@ impl RelayInformationDocument {
204203
#[cfg(target_arch = "wasm32")]
205204
let client: Client = Client::new();
206205

207-
let url = Self::with_http_scheme(url)?;
208-
let req = client
209-
.get(url.to_string())
210-
.header("Accept", "application/nostr+json");
206+
let url: &str = Self::with_http_scheme(&mut url)?;
207+
let req = client.get(url).header("Accept", "application/nostr+json");
211208
match req.send().await {
212209
Ok(response) => {
213210
let json: String = response.text().await?;
@@ -222,14 +219,13 @@ impl RelayInformationDocument {
222219

223220
/// Returns new URL with scheme substituted to HTTP(S) if WS(S) was provided,
224221
/// other schemes leaves untouched.
225-
fn with_http_scheme(url: Url) -> Result<Url, Error> {
226-
let mut url = url;
222+
fn with_http_scheme(url: &mut Url) -> Result<&str, Error> {
227223
match url.scheme() {
228224
"wss" => url.set_scheme("https").map_err(|_| Error::InvalidScheme)?,
229225
"ws" => url.set_scheme("http").map_err(|_| Error::InvalidScheme)?,
230226
_ => {}
231227
}
232-
Ok(url)
228+
Ok(url.as_str())
233229
}
234230
}
235231

0 commit comments

Comments
 (0)