From f531b8122ebd7b07a50198b14727337763d05707 Mon Sep 17 00:00:00 2001 From: pierwill Date: Fri, 1 Oct 2021 12:39:08 -0500 Subject: [PATCH 1/3] docs: `std::hash::Hash` should ensure prefix-free data Closes #89429 --- library/core/src/hash/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index da3f20d18e510..83cb85843b6eb 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -153,9 +153,19 @@ mod sip; /// Thankfully, you won't need to worry about upholding this property when /// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`. /// +/// ## Prefix collisions +/// +/// Implementations of `hash` should ensure that the data they +/// pass to the `Hasher` are prefix-free. That is, different concatenations +/// of the same data should not produce the same output. +/// For example, the standard implementation of [`Hash` for `&str`][impl] passes an extra +/// `0xFF` byte to the `Hasher` so that the values `("ab", "c")` and `("a", +/// "bc")` hash differently. +/// /// [`HashMap`]: ../../std/collections/struct.HashMap.html /// [`HashSet`]: ../../std/collections/struct.HashSet.html /// [`hash`]: Hash::hash +/// [impl]: ../../std/primitive.str.html#impl-Hash #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "Hash"] pub trait Hash { From 2a5dcd58909d5f7c58e3845eb926ed118c87cfb0 Mon Sep 17 00:00:00 2001 From: pierwill Date: Fri, 1 Oct 2021 13:17:50 -0500 Subject: [PATCH 2/3] fix: edit description of "prefix-free" --- library/core/src/hash/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index 83cb85843b6eb..024dbfef80854 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -156,8 +156,10 @@ mod sip; /// ## Prefix collisions /// /// Implementations of `hash` should ensure that the data they -/// pass to the `Hasher` are prefix-free. That is, different concatenations -/// of the same data should not produce the same output. +/// pass to the `Hasher` are prefix-free. That is, +/// unequal values should cause two different byte sequences to be written, +/// and neither of the two sequences should be a prefix of the other. +/// /// For example, the standard implementation of [`Hash` for `&str`][impl] passes an extra /// `0xFF` byte to the `Hasher` so that the values `("ab", "c")` and `("a", /// "bc")` hash differently. From 749194d847bd81d16baf836c8e954f43408ddfa8 Mon Sep 17 00:00:00 2001 From: pierwill <19642016+pierwill@users.noreply.github.com> Date: Sat, 9 Oct 2021 13:53:29 -0500 Subject: [PATCH 3/3] Update library/core/src/hash/mod.rs Co-authored-by: Amanieu d'Antras --- library/core/src/hash/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index 024dbfef80854..540160bc4c2a4 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -157,7 +157,7 @@ mod sip; /// /// Implementations of `hash` should ensure that the data they /// pass to the `Hasher` are prefix-free. That is, -/// unequal values should cause two different byte sequences to be written, +/// unequal values should cause two different sequences of values to be written, /// and neither of the two sequences should be a prefix of the other. /// /// For example, the standard implementation of [`Hash` for `&str`][impl] passes an extra