diff --git a/src/impls.rs b/src/impls.rs index c496618..4b6e6bd 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -1,5 +1,6 @@ mod add; mod index; mod intersection; -mod sub; +mod sub_iterable; +mod sub_self; mod union; diff --git a/src/impls/sub_iterable.rs b/src/impls/sub_iterable.rs new file mode 100644 index 0000000..15e5f81 --- /dev/null +++ b/src/impls/sub_iterable.rs @@ -0,0 +1,57 @@ +use crate::Counter; + +use num_traits::{One, Zero}; + +use std::hash::Hash; +use std::ops::{Sub, SubAssign}; + +impl Sub for Counter +where + I: IntoIterator, + T: Hash + Eq, + N: PartialOrd + SubAssign + Zero + One, +{ + type Output = Self; + /// Consume `self` producing a `Counter` like `self` with the counts of the + /// elements of `I` subtracted, keeping only positive values. + /// + /// ```rust + /// # use counter::Counter; + /// # use std::collections::HashMap; + /// let c = "aaab".chars().collect::>(); + /// let e = c - "abb".chars(); + /// + /// let expect = [('a', 2)].iter().cloned().collect::>(); + /// assert_eq!(e.into_map(), expect); + /// ``` + fn sub(mut self, rhs: I) -> Self::Output { + self.subtract(rhs); + self + } +} + +impl SubAssign for Counter +where + I: IntoIterator, + T: Hash + Eq, + N: PartialOrd + SubAssign + Zero + One, +{ + /// Directly subtract the counts of the elements of `I` from `self`, + /// keeping only items with a value greater than [`N::zero()`]. + /// + /// [`N::zero()`]: + /// https://docs.rs/num-traits/latest/num_traits/identities/trait.Zero.html#tymethod.zero + /// + /// ```rust + /// # use counter::Counter; + /// # use std::collections::HashMap; + /// let mut c = "aaab".chars().collect::>(); + /// c -= "abb".chars(); + /// + /// let expect = [('a', 2)].iter().cloned().collect::>(); + /// assert_eq!(c.into_map(), expect); + /// ``` + fn sub_assign(&mut self, rhs: I) { + self.subtract(rhs); + } +} diff --git a/src/impls/sub.rs b/src/impls/sub_self.rs similarity index 59% rename from src/impls/sub.rs rename to src/impls/sub_self.rs index 9413dcf..a213527 100644 --- a/src/impls/sub.rs +++ b/src/impls/sub_self.rs @@ -1,6 +1,6 @@ use crate::Counter; -use num_traits::{One, Zero}; +use num_traits::Zero; use std::hash::Hash; use std::ops::{Sub, SubAssign}; @@ -80,54 +80,3 @@ where } } } - -impl Sub for Counter -where - I: IntoIterator, - T: Hash + Eq, - N: PartialOrd + SubAssign + Zero + One, -{ - type Output = Self; - /// Consume `self` producing a `Counter` like `self` with the counts of the - /// elements of `I` subtracted, keeping only positive values. - /// - /// ```rust - /// # use counter::Counter; - /// # use std::collections::HashMap; - /// let c = "aaab".chars().collect::>(); - /// let e = c - "abb".chars(); - /// - /// let expect = [('a', 2)].iter().cloned().collect::>(); - /// assert_eq!(e.into_map(), expect); - /// ``` - fn sub(mut self, rhs: I) -> Self::Output { - self.subtract(rhs); - self - } -} - -impl SubAssign for Counter -where - I: IntoIterator, - T: Hash + Eq, - N: PartialOrd + SubAssign + Zero + One, -{ - /// Directly subtract the counts of the elements of `I` from `self`, - /// keeping only items with a value greater than [`N::zero()`]. - /// - /// [`N::zero()`]: - /// https://docs.rs/num-traits/latest/num_traits/identities/trait.Zero.html#tymethod.zero - /// - /// ```rust - /// # use counter::Counter; - /// # use std::collections::HashMap; - /// let mut c = "aaab".chars().collect::>(); - /// c -= "abb".chars(); - /// - /// let expect = [('a', 2)].iter().cloned().collect::>(); - /// assert_eq!(c.into_map(), expect); - /// ``` - fn sub_assign(&mut self, rhs: I) { - self.subtract(rhs); - } -}