Skip to content

Commit 5cc56ac

Browse files
committed
Auto merge of rust-lang#83390 - clarfonthey:hasher_docs, r=Amanieu
Document Hasher spec decision from rust-lang#42951 Since that ticket was closed without the decision actually being documented. Fixes rust-lang#42951.
2 parents f851a3d + 1072a85 commit 5cc56ac

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

+27
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ pub trait Hash {
175175

176176
/// Feeds a slice of this type into the given [`Hasher`].
177177
///
178+
/// This method is meant as a convenience, but its implementation is
179+
/// also explicitly left unspecified. It isn't guaranteed to be
180+
/// equivalent to repeated calls of [`hash`] and implementations of
181+
/// [`Hash`] should keep that in mind and call [`hash`] themselves
182+
/// if the slice isn't treated as a whole unit in the [`PartialEq`]
183+
/// implementation.
184+
///
185+
/// For example, a [`VecDeque`] implementation might naïvely call
186+
/// [`as_slices`] and then [`hash_slice`] on each slice, but this
187+
/// is wrong since the two slices can change with a call to
188+
/// [`make_contiguous`] without affecting the [`PartialEq`]
189+
/// result. Since these slices aren't treated as singular
190+
/// units, and instead part of a larger deque, this method cannot
191+
/// be used.
192+
///
178193
/// # Examples
179194
///
180195
/// ```
@@ -186,6 +201,12 @@ pub trait Hash {
186201
/// Hash::hash_slice(&numbers, &mut hasher);
187202
/// println!("Hash is {:x}!", hasher.finish());
188203
/// ```
204+
///
205+
/// [`VecDeque`]: ../../std/collections/struct.VecDeque.html
206+
/// [`as_slices`]: ../../std/collections/struct.VecDeque.html#method.as_slices
207+
/// [`make_contiguous`]: ../../std/collections/struct.VecDeque.html#method.make_contiguous
208+
/// [`hash`]: Hash::hash
209+
/// [`hash_slice`]: Hash::hash_slice
189210
#[stable(feature = "hash_slice", since = "1.3.0")]
190211
fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
191212
where
@@ -221,6 +242,11 @@ pub use macros::Hash;
221242
/// instance (with [`write`] and [`write_u8`] etc.). Most of the time, `Hasher`
222243
/// instances are used in conjunction with the [`Hash`] trait.
223244
///
245+
/// This trait makes no assumptions about how the various `write_*` methods are
246+
/// defined and implementations of [`Hash`] should not assume that they work one
247+
/// way or another. You cannot assume, for example, that a [`write_u32`] call is
248+
/// equivalent to four calls of [`write_u8`].
249+
///
224250
/// # Examples
225251
///
226252
/// ```
@@ -240,6 +266,7 @@ pub use macros::Hash;
240266
/// [`finish`]: Hasher::finish
241267
/// [`write`]: Hasher::write
242268
/// [`write_u8`]: Hasher::write_u8
269+
/// [`write_u32`]: Hasher::write_u32
243270
#[stable(feature = "rust1", since = "1.0.0")]
244271
pub trait Hasher {
245272
/// Returns the hash value for the values written so far.

0 commit comments

Comments
 (0)