Skip to content

Commit

Permalink
Rollup merge of #89438 - pierwill:prefix-free-hash, r=Amanieu
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthiaskrgr authored Oct 10, 2021
2 parents cfa5391 + 749194d commit 06cfd0a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions library/core/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,21 @@ 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,
/// 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
/// `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 {
Expand Down

0 comments on commit 06cfd0a

Please sign in to comment.