Skip to content

Commit

Permalink
Merge pull request #17 from samueltardieu/no-clone
Browse files Browse the repository at this point in the history
<N: Clone> bound is not required on {Add,Sub}{,Assign}
  • Loading branch information
coriolinus authored Feb 7, 2022
2 parents 618858d + c2d7198 commit ef765ab
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ where
impl<T, N> AddAssign for Counter<T, N>
where
T: Clone + Hash + Eq,
N: Clone + Zero + AddAssign,
N: Zero + AddAssign,
{
/// Add another counter to this counter
///
Expand All @@ -339,17 +339,17 @@ where
/// assert_eq!(c.into_map(), expect);
/// ```
fn add_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map.iter() {
for (key, value) in rhs.map.into_iter() {
let entry = self.map.entry(key.clone()).or_insert_with(N::zero);
*entry += value.clone();
*entry += value;
}
}
}

impl<T, N> Add for Counter<T, N>
where
T: Clone + Hash + Eq,
N: Clone + PartialOrd + PartialEq + AddAssign + Zero,
N: AddAssign + Zero,
{
type Output = Counter<T, N>;

Expand Down Expand Up @@ -377,7 +377,7 @@ where
impl<T, N> SubAssign for Counter<T, N>
where
T: Hash + Eq,
N: Clone + PartialOrd + PartialEq + SubAssign + Zero,
N: PartialOrd + PartialEq + SubAssign + Zero,
{
/// Subtract (keeping only positive values).
///
Expand All @@ -396,11 +396,11 @@ where
/// assert_eq!(c.into_map(), expect);
/// ```
fn sub_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map.iter() {
for (key, value) in rhs.map.into_iter() {
let mut remove = false;
if let Some(entry) = self.map.get_mut(key) {
if *entry >= *value {
*entry -= value.clone();
if let Some(entry) = self.map.get_mut(&key) {
if *entry >= value {
*entry -= value;
} else {
remove = true;
}
Expand All @@ -409,7 +409,7 @@ where
}
}
if remove {
self.map.remove(key);
self.map.remove(&key);
}
}
}
Expand All @@ -418,7 +418,7 @@ where
impl<T, N> Sub for Counter<T, N>
where
T: Hash + Eq,
N: Clone + PartialOrd + PartialEq + SubAssign + Zero,
N: PartialOrd + PartialEq + SubAssign + Zero,
{
type Output = Counter<T, N>;

Expand Down

0 comments on commit ef765ab

Please sign in to comment.