Dear all,
I've implemented a Set datatype as a Vec. Equality is then defined as two Vecs having the same elements, but in possibly ordered differently in the Vecs. Elements in a set cannot be (totally) ordered, hence Equal sets cannot be written uniquely.
For example, if the element type is Prod(usize,usize) where
Prod(1,2) partial_cmp Prod(2,1) is None
then Set(Vec![Prod(2,1),Prod(1,2)]) partial_cmp Set(Vec![Prod(1,2),Prod(2,1)]) is Equal.
So far, so good. Now I'd like to have a HashMap of Sets, and thus require Set to be hashable.
Clippy rightly complains that I shouldn't derive Hash with my own PartialEq.
This is the obvious (wrong) hasher since it does not satisfy a == b => hash(a) == hash(b)
pub struct Set {
set: Vec<Prod>
}
impl Hash for Set {
fn hash<H: Hasher>(&self, hasher: &mut H) {
for s in &self.set { s.hash(hasher); }
}
}
I really can't think of an implementation of hash, other than the empty (very inefficient) hasher.
impl Hash for Set {
fn hash<H: Hasher>(&self, hasher: &mut H) { }
}
I must be missing something?
Thanks,
Kees