aboutsummaryrefslogtreecommitdiff
path: root/macros/src/util/iterator_ext.rs
blob: 86db6cbabbb52975c2a03f3dda3e8290ae1b07b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::collections::HashMap;
use std::hash::Hash;

pub trait IteratorExt<Item>
{
    fn find_duplicate(&mut self) -> Option<Item>;
}

impl<Iter> IteratorExt<Iter::Item> for Iter
where
    Iter: Iterator,
    Iter::Item: Eq + Hash + Copy,
{
    fn find_duplicate(&mut self) -> Option<Iter::Item>
    {
        let mut iterated_item_map = HashMap::<Iter::Item, ()>::new();

        for item in self {
            if iterated_item_map.contains_key(&item) {
                return Some(item);
            }

            iterated_item_map.insert(item, ());
        }

        None
    }
}