easier 0.3.0

making rust easier
Documentation
use std::collections::HashSet;
use std::hash::Hash;
use std::vec::IntoIter;

pub trait UniqueExtension: IntoIterator {
    /// Returns an iterator with unique items
    /// The ordering is guaranteed to be the same as the original
    /// ```rust
    /// use easier::prelude::*;
    /// let arr = ["1", "1", "22", "333", "333", "1"];
    /// let mut unique = arr.unique();
    /// assert_eq!(unique.next(), Some("1"));
    /// ```
    fn unique(self) -> IntoIter<Self::Item>
    where
        Self::Item: Eq + Hash + Clone;
}

impl<I> UniqueExtension for I
where
    I: IntoIterator,
{
    ///note this turns into a vec, then back into an iterator
    fn unique(self) -> IntoIter<Self::Item>
    where
        Self::Item: Eq + Hash + Clone,
    {
        let mut map = HashSet::new();
        let mut vec = vec![];
        for item in self {
            if map.contains(&item) {
                continue;
            }
            map.insert(item.clone());
            vec.push(item);
        }
        vec.into_iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::*;

    #[test]
    fn test_unique() {
        let arr = ["1", "1", "22", "333", "333", "1"];
        let mut unique = arr.unique();
        assert_eq!(unique.next(), Some("1"));
        assert_eq!(unique.next(), Some("22"));
        assert_eq!(unique.next(), Some("333"));
        assert_eq!(unique.next(), None);
    }
    #[test]
    fn doc() {
        let vec = vec![1, 3, 2, 2, 1];
        let uniques = vec.into_iter().unique().to_vec();
        assert_eq!(uniques, [1, 3, 2]);
    }
}