Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#824 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
rust
領域
api

調査の方向性

HeaderName の Borrow と Hash の実装から始め、issue の hash_one の例を使って不一致を理解し、必要な保証については stdlib の Borrow ドキュメントを参照してください。ハッシュが as_str() と一致するべきか、それとも Borrow を削除するべきかを判断し、その後、HeaderName とその借用文字列のハッシュが一致すること、および hashmap の検索が正しく動作することを確認してください。

索引モデルが issue の本文から書いたものです。

説明

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...
主要言語
Rust
スター
1.4k
フォーク
378
平均マージ
1日 21時間
マージ済み PR(30日)
5

環境構築

このプロジェクトの環境構築ファイルはまだ確認していません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

hyperium/http のほかの issue

hyperium/http の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。