Skip to content
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

<N: Clone> bound is not required on {Add,Sub}{,Assign} #17

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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