HashSet containing struct containing HashMap


#1

Hi all,

I am trying to build a data structure that contains a HashSet of structs that contains HashMaps but it seems that neither HashMap nor HashSet implement the Hash trait since trying to derive Hash for the struct results in the following error:

<anon>:6:5: 6:38 error: the trait `core::hash::Hash` is not implemented for the type `std::collections::hash::map::HashMap<collections::string::String, collections::string::String>` [E0277]
<anon>:6     pub vars: HashMap<String, String>,
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:4:39: 4:43 note: in this expansion of #[derive_Hash] (defined in <anon>)
<anon>:6:5: 6:38 help: see the detailed explanation for E0277
<anon>:6:5: 6:38 note: required by `core::hash::Hash::hash`
error: aborting due to previous error
playpen: application terminated with error code 101

Here is the isolated case: http://is.gd/PpR71b

What is the motivation for not implementing the Hash trait for HashMap (and HashSet)? Is the only way to get this to work to manually implement the Hash trait for struct Foo that contains the HashMap?


#2

HashMap can’t implement Hash for technical reasons that I can’t remember off the top of my head, but I think it’s at least in part due to the lack of a deterministic iteration order over elements.

You might consider BTreeMap and BTreeSet for nested use.


#3

Understood. Thanks!


#4

The problem is that Hasher lacks an interface to denote unordered collections of values. Perhaps adding such a thing would be a worthwile endeavor. Otherwise HashMap could use a secondary Hasher to get its values’ hashes and xor those, but this would likely be more complex and lead to less optimal code.