self_compare/
ext.rs

1use ::{Comparer, ComparerMut};
2/// Extra methods for slices to compare with self
3pub trait SliceCompareExt<T> {
4    /// Returns a `Comparer` from a reference to a slice
5    fn self_comparer(&self) -> Comparer<T>;
6    /// Returns a `Comparer` from a reference to a mutable slice
7    fn self_comparer_mut(&mut self) -> ComparerMut<T>;
8    /// Convenience function for itereating through the `Comparer` and applying a function to each pair
9    fn compare_self<F: FnMut(&T, &T)>(&self, mut f: F) {
10        for (a, b) in self.self_comparer() {
11            f(a, b)
12        }
13    }
14    /// Same as `compare_self()` but also parses the indices of the elements to the function
15    fn compare_self_enumerated<F: FnMut((usize, &T), (usize, &T))>(&self, mut f: F) {
16        let mut c = self.self_comparer();
17        while let Some((a, b)) = c.next_enumerated() {
18            f(a, b);
19        }
20    }
21    /// Convenience function for itereating through the `ComparerMut` and applying a function to each pair
22    fn compare_self_mut<F: FnMut(&mut T, &mut T)>(&mut self, mut f: F) {
23        let mut c = self.self_comparer_mut();
24        while let Some((a, b)) = c.next() {
25           f(a, b)
26        }
27    }
28    /// Same as `compare_self_mut()` but also parses the indices of the elements to the function
29    fn compare_self_enumerated_mut<F: FnMut((usize, &mut T), (usize, &mut T))>(&mut self, mut f: F) {
30        let mut c = self.self_comparer_mut();
31        while let Some((a, b)) = c.next_enumerated() {
32            f(a, b);
33        }
34    }
35}
36
37impl<T> SliceCompareExt<T> for [T] {
38    fn self_comparer(&self) -> Comparer<T> {
39        Comparer::new(self)
40    }
41    fn self_comparer_mut(&mut self) -> ComparerMut<T> {
42        ComparerMut::new(self)
43    }
44}