Skip to content

incorrect hash impl #72

@conradludgate

Description

@conradludgate

https://doc.rust-lang.org/std/hash/trait.Hash.html#hash-and-eq

When implementing both Hash and Eq, it is important that the following property holds:
k1 == k2 -> hash(k1) == hash(k2)
In other words, if two keys are equal, their hashes must also be equal

UniCase<S: AsRef<str>> implements Hash + Eq, so the above requirement must be satisfied. The following test case fails:

use std::hash::BuildHasher;
use foldhash::quality::RandomState;
use unicase::UniCase;

let k1 = UniCase::unicode("Foo");
let k2 = UniCase::new("foO");

assert_eq!(k1, k2);

let h = RandomState::default();
assert_eq!(h.hash_one(k1), h.hash_one(k2));

This occurs because UniCase::unicode produces b"f", b"o", b"o", while UniCase::new (with ascii) produces b'f', b'o', b'o', which is not guaranteed to be the same under Hasher.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions