`HeaderName` implements `Borrow<str>` but doesn't hash like `str`
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 62/100
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.
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...
- Dominant language
- Rust
- Stars
- 1.4k
- Forks
- 378
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 5
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.
More from hyperium/http
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
-
Difficulty 3/5 1-2 days Newbie friendliness 58/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
Similar issues
-
bug CLI custom-model
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
rust-bitcoin/rust-bitcoin#6930 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
fulcrumgenomics/ferro-hgvs#2251 ·
-
A-allocators A-docs C-enhancement T-libs
Difficulty 2/5 1-3 hours Newbie friendliness 75/100