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