Skip to content

Commit 2c4d7a5

Browse files
authored
Rollup merge of #96828 - scottmcm:clarify-hasher-write, r=Amanieu
Further elaborate the lack of guarantees from `Hasher` I realized that I got too excited in #94598 by adding new methods, and forgot to do the documentation to really answer the core question in #94026. This PR just has that doc update. r? `@Amanieu`
2 parents cdaa5c0 + 83f785b commit 2c4d7a5

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)