Issue
HeaderName implements Borrow<str>, but it's Hash implementation can give different results compared to hashing the string we receive from calling borrow() on it.
Example :
let build_hasher = BuildHasherDefault::<DefaultHasher>::default();
let accept_charset = http::header::ACCEPT_CHARSET;
let borrowed: &str = accept_charset.borrow();
assert!(borrowed == "accept-charset");
assert!(build_hasher.hash_one(borrowed) != build_hasher.hash_one(accept_charset));
This is unexpected, as the stdlib documentation for Borrow says
In particular Eq, Ord and Hash must be equivalent for borrowed and owned values
This causes issues if you put a HeaderName as the key of a "normal" hashmap, because indexing by &str will compile, but be a bug since the hash don't match.
Potential fix
Either:
- add a custom
Hash implementation for HeaderName that hashes to the str value of the header
impl Hash for HeaderName {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
- remove the Borrow trait implementation. As mentioned in the stdlib documentation,
AsRef is sufficient if the guarantees that Borrow should gives cannot be respected. Of course that would be a breaking change...