Skip to content

HeaderName implements Borrow<str> but doesn't hash like str #824

@paullegranddc

Description

@paullegranddc

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions