-
Notifications
You must be signed in to change notification settings - Fork 313
ref(iroh): Relay map extend #3577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,11 +112,34 @@ impl RelayMap { | |
| self.relays.write().expect("poisoned").remove(url) | ||
| } | ||
|
|
||
| /// Joins this `RelayMap` with another one into a new one | ||
| pub fn join(self, other: RelayMap) -> RelayMap { | ||
| { | ||
| let mut a = self.relays.write().expect("poisoned"); | ||
| let b = other.relays.read().expect("poisoned"); | ||
| a.extend(b.iter().map(|(a, b)| (a.clone(), b.clone()))); | ||
| } | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Extend<(RelayUrl, Arc<RelayConfig>)> for RelayMap { | ||
| /// Extends this `RelayMap` with another one. | ||
| pub fn extend(&self, other: &RelayMap) { | ||
| /// | ||
| /// You can use this like this: | ||
| /// | ||
| /// ```rust | ||
| /// # let relay_map_a: RelayMap = { unimplemented!() }; | ||
| /// # let relay_map_b: RelayMap = { unimplemented!() }; | ||
| /// | ||
| /// relay_map_a.extend(relay_map_b.relays::<Vec<_>>()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is less efficient, and seems rather pointless to support, if you have to collect & allocate into a vector first before using the trait
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inner lock seems to stop you from doing anything nice using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that's the issue yes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this would be a That also would mean that not the whole map must be locked when adding/removing. But it introduces other things that are dangerous, such as holding dashmap locks over
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you lint against that? I didn't know that was possible.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But clippy can do that (by now) via the |
||
| /// ``` | ||
| fn extend<I>(&mut self, iter: I) | ||
| where | ||
| I: IntoIterator<Item = (RelayUrl, Arc<RelayConfig>)>, | ||
| { | ||
| let mut a = self.relays.write().expect("poisoned"); | ||
| let b = other.relays.read().expect("poisoned"); | ||
| a.extend(b.iter().map(|(a, b)| (a.clone(), b.clone()))); | ||
| a.extend(iter); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this called
chainusually? I think this is very similar to theIterator::chainsignature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is. I opted for
joinbecause aMapis not something that can (semantically) be put into a ⛓️ chain! 😉 I think ofchain()something that can be applied to a sequence of something (henceIterator::chainbut notBTreeMap::chainfor example).I have no strong feelings about the actual method name tho, so if you want me to change it, I'll happily do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not convinced passing owned versions makes sense for a fully clonable thing. This API really doesn't communicate the fact that this doesn't actually create a new version, but rather modifies the underlying, likely shared instances