Skip to content

Commit 25c92df

Browse files
Reduce some clones in predicate impls (#151)
* reduce some clones in predicate types * remove unnecesary clone restrictions * better naming * remove more unnecesary from impls * make diff look smaller
1 parent caf8782 commit 25c92df

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/kbucket.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,42 +104,42 @@ impl<TNodeId, TVal> AsRef<Key<TNodeId>> for ClosestValue<TNodeId, TVal> {
104104

105105
/// A key that can be returned from the `closest_keys` function, which indicates if the key matches the
106106
/// predicate or not.
107-
pub struct PredicateKey<TNodeId: Clone> {
107+
pub struct PredicateKey<TNodeId> {
108108
pub key: Key<TNodeId>,
109109
pub predicate_match: bool,
110110
}
111111

112-
impl<TNodeId: Clone> From<PredicateKey<TNodeId>> for Key<TNodeId> {
112+
impl<TNodeId> From<PredicateKey<TNodeId>> for Key<TNodeId> {
113113
fn from(key: PredicateKey<TNodeId>) -> Self {
114114
key.key
115115
}
116116
}
117117

118-
impl<TNodeId: Clone, TVal> From<PredicateValue<TNodeId, TVal>> for PredicateKey<TNodeId> {
119-
fn from(value: PredicateValue<TNodeId, TVal>) -> Self {
120-
PredicateKey {
121-
key: value.key,
122-
predicate_match: value.predicate_match,
123-
}
124-
}
125-
}
126-
127118
/// A value being returned from a predicate closest iterator.
128-
pub struct PredicateValue<TNodeId: Clone, TVal> {
119+
pub struct PredicateValue<TNodeId, TVal> {
129120
pub key: Key<TNodeId>,
130121
pub predicate_match: bool,
131122
pub value: TVal,
132123
}
133124

134-
impl<TNodeId: Clone, TVal> AsRef<Key<TNodeId>> for PredicateValue<TNodeId, TVal> {
125+
impl<TNodeId, TVal> AsRef<Key<TNodeId>> for PredicateValue<TNodeId, TVal> {
135126
fn as_ref(&self) -> &Key<TNodeId> {
136127
&self.key
137128
}
138129
}
139130

140-
impl<TNodeId: Clone, TVal> From<PredicateValue<TNodeId, TVal>> for Key<TNodeId> {
141-
fn from(key: PredicateValue<TNodeId, TVal>) -> Self {
142-
key.key
131+
impl<TNodeId, TVal> PredicateValue<TNodeId, TVal> {
132+
pub fn to_key_value(self) -> (PredicateKey<TNodeId>, TVal) {
133+
let PredicateValue {
134+
key,
135+
predicate_match,
136+
value,
137+
} = self;
138+
let key = PredicateKey {
139+
key,
140+
predicate_match,
141+
};
142+
(key, value)
143143
}
144144
}
145145

src/service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,11 @@ impl Service {
492492
{
493493
let mut kbuckets = self.kbuckets.write();
494494
for closest in kbuckets.closest_values_predicate(&target_key, &kbucket_predicate) {
495+
let (node_id_predicate, enr) = closest.to_key_value();
495496
// Add the known ENR's to the untrusted list
496-
target.untrusted_enrs.push(closest.value.clone());
497+
target.untrusted_enrs.push(enr);
497498
// Add the key to the list for the query
498-
known_closest_peers.push(closest.into());
499+
known_closest_peers.push(node_id_predicate);
499500
}
500501
};
501502

0 commit comments

Comments
 (0)