bluejay_validator/
utils.rs

1use std::cmp::{Eq, Ord};
2use std::collections::BTreeMap;
3use std::hash::Hash;
4
5pub fn duplicates<T: Copy, I: Iterator<Item = T>, K: Hash + Ord + Eq>(
6    iter: I,
7    key: fn(T) -> K,
8) -> impl Iterator<Item = (K, Vec<T>)> {
9    let indexed = iter.fold(
10        BTreeMap::new(),
11        |mut indexed: BTreeMap<K, Vec<T>>, el: T| {
12            indexed.entry(key(el)).or_default().push(el);
13            indexed
14        },
15    );
16
17    indexed.into_iter().filter(|(_, values)| values.len() > 1)
18}