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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
use std::collections::HashSet; use std::hash::Hash; use crate::{GrowableProducer, Iterable}; impl<T> Iterable for HashSet<T> { type C = Self; type CC<U> = HashSet<U>; fn add_one(mut self, a: Self::Item) -> Self::C where Self::C: GrowableProducer<Self::Item>, { self.grow_one(a); self } } impl<'a, T> Iterable for &'a HashSet<T> { type C = HashSet<&'a T>; type CC<U> = HashSet<U>; } delegate_into_iterator!(HashSet<T>, impl <T>); delegate_into_iterator!(&'a HashSet<T>, impl <'a, T: 'a>); delegate_from_iterator!(HashSet<T>, T, impl <T: Eq + Hash>); delegate_extend!(HashSet<T>, T, impl <T: Eq + Hash>); #[cfg(test)] mod tests { use maplit::*; use super::*; #[test] fn test_c() { let v = hashset![1, 2, 3]; let res = v.filter(|i| i > &1); assert_eq!(res, hashset![2, 3]); } #[test] fn test_cc() { let v = hashset![1, 2, 3]; let res = v.map(|i| i.to_string()); assert_eq!( res, hashset!["1".to_string(), "2".to_string(), "3".to_string()] ); } #[test] fn test_c_r() { let v = hashset![1, 2, 3]; let res = (&v).filter(|i| i > &&1); assert_eq!(res, hashset![&2, &3]); } #[test] fn test_cc_r() { let v = hashset![1, 2, 3]; let res = (&v).map(|i| i.to_string()); assert_eq!( res, hashset!["1".to_string(), "2".to_string(), "3".to_string()] ); } }