-
Notifications
You must be signed in to change notification settings - Fork 13.3k
HashMap
docs don't mention {Eq, Hash}
consistency
#23320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This belongs to the class of properties that are great to check for with quickcheck. use std::fmt::Debug;
use std::hash::{hash, Hash, SipHasher};
use quickcheck::{Arbitrary, quickcheck};
fn prop_sane_hash<T: Arbitrary + Debug + Hash + PartialEq>(a: T, b: T) -> bool {
a != b || hash::<_, SipHasher>(&a) == hash::<_, SipHasher>(&b)
}
fn main() {
quickcheck(prop_sane_hash as fn(Foo, Foo) -> bool);
} |
I think this is a generic property that should be mentioned in Hash, not just HashMap/HashSet. |
steveklabnik
added a commit
to steveklabnik/rust
that referenced
this issue
Mar 28, 2015
Manishearth
added a commit
to Manishearth/rust
that referenced
this issue
Mar 29, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While it may not be possible to enforce this at the type level, the docs for
HashMap
(andHashSet
) should specify that its key type's{Eq, Hash}
implementations must be consistent for the map to have reasonable behavior. The exact requirement isThis is always the case for derived implementations, but the following example is nonsensical if
Foo
is used as aHashMap
key:There could additionally be some kind of debug-level assertion of this behavior, or possibly a lint that checks to make sure that the fields that are referenced in
PartialEq::eq
andHash::hash
are consistent. The lint wouldn't be perfect, but could be useful.The text was updated successfully, but these errors were encountered: