-
-
Notifications
You must be signed in to change notification settings - Fork 357
Open
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...
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels