about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-10-10 18:22:20 +0200
committerGitHub <noreply@github.com>2021-10-10 18:22:20 +0200
commit06cfd0af48d5e32a8b88c50c65bf46518d217e4e (patch)
tree73c6b0576282350e92822969a33686e5a2d5a0b5
parentcfa5391f0bb99571295cd378b7305410dab6f215 (diff)
parent749194d847bd81d16baf836c8e954f43408ddfa8 (diff)
downloadrust-06cfd0af48d5e32a8b88c50c65bf46518d217e4e.tar.gz
rust-06cfd0af48d5e32a8b88c50c65bf46518d217e4e.zip
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.
-rw-r--r--library/core/src/hash/mod.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs
index da3f20d18e5..540160bc4c2 100644
--- a/library/core/src/hash/mod.rs
+++ b/library/core/src/hash/mod.rs
@@ -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 {