Skip to content

Commit 834ab78

Browse files
refactor(iroh-base): introduce an Arc into RelayUrl (#3065)
## Description This makes the `clone`ing of `RelayUrl` much cheaper. It turned out the whole code base was already using the `RelayUrl` as an immutable type already, so no changes otherwise necessary. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent a4d4e7d commit 834ab78

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

iroh-base/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ derive_more = { version = "1.0.0", features = ["display"], optional = true }
2222
url = { version = "2.5", features = ["serde"], optional = true }
2323
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"], optional = true }
2424
rand_core = { version = "0.6.4", optional = true }
25-
serde = { version = "1", features = ["derive"] }
25+
serde = { version = "1", features = ["derive", "rc"] }
2626
thiserror = { version = "2", optional = true }
2727

2828
# wasm

iroh-base/src/relay_url.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use std::{fmt, ops::Deref, str::FromStr};
1+
use std::{fmt, ops::Deref, str::FromStr, sync::Arc};
22

33
use serde::{Deserialize, Serialize};
44
use url::Url;
5+
56
/// A URL identifying a relay server.
67
///
7-
/// This is but a wrapper around [`Url`], with a few custom tweaks:
8+
/// It is cheaply clonable, as the underlying type is wrapped into an `Arc`.
9+
/// The main type under the hood though is [`Url`], with a few custom tweaks:
810
///
911
/// - A relay URL is never a relative URL, so an implicit `.` is added at the end of the
1012
/// domain name if missing.
@@ -16,7 +18,7 @@ use url::Url;
1618
#[derive(
1719
Clone, derive_more::Display, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
1820
)]
19-
pub struct RelayUrl(Url);
21+
pub struct RelayUrl(Arc<Url>);
2022

2123
impl From<Url> for RelayUrl {
2224
fn from(mut url: Url) -> Self {
@@ -31,7 +33,7 @@ impl From<Url> for RelayUrl {
3133
url.set_host(Some(&domain)).ok();
3234
}
3335
}
34-
Self(url)
36+
Self(Arc::new(url))
3537
}
3638
}
3739

@@ -55,7 +57,7 @@ impl FromStr for RelayUrl {
5557

5658
impl From<RelayUrl> for Url {
5759
fn from(value: RelayUrl) -> Self {
58-
value.0
60+
Arc::unwrap_or_clone(value.0)
5961
}
6062
}
6163

iroh-net-report/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ impl Actor {
611611
self.handle_run_check(relay_map, opts, response_tx);
612612
}
613613
Message::ReportReady { report } => {
614-
self.handle_report_ready(report);
614+
self.handle_report_ready(*report);
615615
}
616616
Message::ReportAborted { err } => {
617617
self.handle_report_aborted(err);
@@ -694,8 +694,8 @@ impl Actor {
694694
});
695695
}
696696

697-
fn handle_report_ready(&mut self, report: Box<Report>) {
698-
let report = self.finish_and_store_report(*report);
697+
fn handle_report_ready(&mut self, report: Report) {
698+
let report = self.finish_and_store_report(report);
699699
self.in_flight_stun_requests.clear();
700700
if let Some(ReportRun { report_tx, .. }) = self.current_report_run.take() {
701701
report_tx.send(Ok(report)).ok();

0 commit comments

Comments
 (0)