1use std::collections::HashSet;
2
3pub trait SmartHash {
4 type option;
5
6 fn into_option(&self) -> Self::option;
7}
8
9pub trait SmartHashOpt where Self : Default { }
10
11pub trait SmartHashSet where <Self as SmartHashSet>::output : SmartHash, <Self as SmartHashSet>::option : Default
12{
13 type output;
14 type option;
15
16 fn get_matching<'a>(&'a self, options : <<Self as SmartHashSet>::output as SmartHash>::option) -> Option<Vec<&'a Self::output>>;
17
18 fn get_none_default(&self) -> Self::option {
19 Self::option::default()
20 }
21}
22
23impl<SH> SmartHashSet for HashSet<SH>
24 where SH : SmartHash + std::hash::Hash + std::cmp::Eq,
25 <SH as SmartHash>::option : std::cmp::PartialEq + Default,
26{
27 type output = SH;
28 type option = SH::option;
29
30 fn get_matching<'a>(&'a self, options : <SH as SmartHash>::option) -> Option<Vec<&'a SH>> {
31 let mut matches : Vec<&SH> = Vec::new();
32
33 for item in self.iter() {
34 let option = item.into_option();
35 if option == options {
36 matches.push(&item);
37 }
38 }
39
40 if matches.len() > 0 {
41 Some(matches)
42 } else {
43 None
44 }
45 }
46}