Skip to content

Commit

Permalink
fix(reputation): improve usability of ars and trs
Browse files Browse the repository at this point in the history
impl PartialEq for ActiveReputationSet

Add default hasher `RandomState` for TRS

Fix serialization of TRS

Fix lifetimes in signature of penalize_many
  • Loading branch information
tmpolaczyk committed May 13, 2019
1 parent 5a1b015 commit 2df3708
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 10 additions & 0 deletions reputation/src/ars.rs
Expand Up @@ -107,6 +107,16 @@ where
}
}

impl<K> PartialEq for ActiveReputationSet<K>
where
K: Clone + Eq + Hash,
{
fn eq(&self, other: &Self) -> bool {
// Equality is fully defined by equality of queues
self.capacity == other.capacity && self.queue == other.queue
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
19 changes: 17 additions & 2 deletions reputation/src/trs.rs
Expand Up @@ -33,7 +33,7 @@ use serde::{Deserialize, Serialize};
/// expire later).
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Clone, Debug)]
pub struct TotalReputationSet<K, V, A, S>
pub struct TotalReputationSet<K, V, A, S = RandomState>
where
K: Clone + Eq + Hash,
V: AddAssign + Clone + Default + Ord + SubAssign,
Expand All @@ -43,8 +43,22 @@ where
// A cache of <identity: total_reputation>
// All the identities with reputation are in the cache: identities
// not in the cache must have null reputation
#[cfg_attr(
feature = "serde",
serde(bound(
serialize = "HashMap<K, V, S>: Serialize",
deserialize = "HashMap<K, V, S>: Deserialize<'de>"
))
)]
map: HashMap<K, V, S>,
// The list of reputation packets ordered by expiration
#[cfg_attr(
feature = "serde",
serde(bound(
serialize = " VecDeque<(A, HashMap<K, V, S>)>: Serialize",
deserialize = " VecDeque<(A, HashMap<K, V, S>)>: Deserialize<'de>"
))
)]
queue: VecDeque<(A, HashMap<K, V, S>)>,
}

Expand Down Expand Up @@ -185,10 +199,11 @@ where
}

/// The more efficient version of `penalize`.
pub fn penalize_many<'a, F, I>(&'a mut self, ids_fs: I) -> Result<V, RepError<V>>
pub fn penalize_many<'a, F, I>(&mut self, ids_fs: I) -> Result<V, RepError<V>>
where
F: FnMut(V) -> V,
I: IntoIterator<Item = (&'a K, F)>,
K: 'a,
{
let mut total_subtracted = V::default();
let mut to_subtract = ids_fs
Expand Down

0 comments on commit 2df3708

Please sign in to comment.