diff options
Diffstat (limited to 'macros/src/util/iterator_ext.rs')
-rw-r--r-- | macros/src/util/iterator_ext.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/macros/src/util/iterator_ext.rs b/macros/src/util/iterator_ext.rs new file mode 100644 index 0000000..86db6cb --- /dev/null +++ b/macros/src/util/iterator_ext.rs @@ -0,0 +1,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 + } +} |