`HeaderName` implements `Borrow<str>` but doesn't hash like `str`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 1.4k
- Forks
- 378
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 5
Description
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
Hashimplementation forHeaderNamethat 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,
AsRefis sufficient if the guarantees thatBorrowshould gives cannot be respected. Of course that would be a breaking change...
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at HeaderName's Borrow and Hash implementations, using the issue's hash_one example to understand the mismatch and the stdlib Borrow documentation for the required guarantees. Decide whether hashing should match as_str() or Borrow should be removed, then verify that hashing HeaderName and its borrowed string agrees and that hashmap lookup behaves correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 62/100