Skip to content

Commit 83f785b

Browse files
committed
Further elaborate the lack of guarantees from Hasher
1 parent ebdcb08 commit 83f785b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

library/core/src/hash/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,29 @@ pub use macros::Hash;
268268
/// instance (with [`write`] and [`write_u8`] etc.). Most of the time, `Hasher`
269269
/// instances are used in conjunction with the [`Hash`] trait.
270270
///
271-
/// This trait makes no assumptions about how the various `write_*` methods are
271+
/// This trait provides no guarantees about how the various `write_*` methods are
272272
/// defined and implementations of [`Hash`] should not assume that they work one
273273
/// way or another. You cannot assume, for example, that a [`write_u32`] call is
274-
/// equivalent to four calls of [`write_u8`].
274+
/// equivalent to four calls of [`write_u8`]. Nor can you assume that adjacent
275+
/// `write` calls are merged, so it's possible, for example, that
276+
/// ```
277+
/// # fn foo(hasher: &mut impl std::hash::Hasher) {
278+
/// hasher.write(&[1, 2]);
279+
/// hasher.write(&[3, 4, 5, 6]);
280+
/// # }
281+
/// ```
282+
/// and
283+
/// ```
284+
/// # fn foo(hasher: &mut impl std::hash::Hasher) {
285+
/// hasher.write(&[1, 2, 3, 4]);
286+
/// hasher.write(&[5, 6]);
287+
/// # }
288+
/// ```
289+
/// end up producing different hashes.
290+
///
291+
/// Thus to produce the same hash value, [`Hash`] implementations must ensure
292+
/// for equivalent items that exactly the same sequence of calls is made -- the
293+
/// same methods with the same parameters in the same order.
275294
///
276295
/// # Examples
277296
///

0 commit comments

Comments
 (0)