Skip to content

Commit 06cfd0a

Browse files
authored
Rollup merge of #89438 - pierwill:prefix-free-hash, r=Amanieu
docs: `std::hash::Hash` should ensure prefix-free data Attempt to synthesize the discussion in #89429 into a suggestion regarding `Hash` implementations (not a hard requirement). Closes #89429.
2 parents cfa5391 + 749194d commit 06cfd0a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Diff for: library/core/src/hash/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,21 @@ mod sip;
153153
/// Thankfully, you won't need to worry about upholding this property when
154154
/// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`.
155155
///
156+
/// ## Prefix collisions
157+
///
158+
/// Implementations of `hash` should ensure that the data they
159+
/// pass to the `Hasher` are prefix-free. That is,
160+
/// unequal values should cause two different sequences of values to be written,
161+
/// and neither of the two sequences should be a prefix of the other.
162+
///
163+
/// For example, the standard implementation of [`Hash` for `&str`][impl] passes an extra
164+
/// `0xFF` byte to the `Hasher` so that the values `("ab", "c")` and `("a",
165+
/// "bc")` hash differently.
166+
///
156167
/// [`HashMap`]: ../../std/collections/struct.HashMap.html
157168
/// [`HashSet`]: ../../std/collections/struct.HashSet.html
158169
/// [`hash`]: Hash::hash
170+
/// [impl]: ../../std/primitive.str.html#impl-Hash
159171
#[stable(feature = "rust1", since = "1.0.0")]
160172
#[rustc_diagnostic_item = "Hash"]
161173
pub trait Hash {

0 commit comments

Comments
 (0)