Skip to content

Commit 43279b2

Browse files
authored
Rollup merge of #90995 - the8472:hash-portability, r=dtolnay
Document non-guarantees for Hash Dependence on endianness and type sizes was reported for enum discriminants in #74215 but it is a more general issue since for example the default implementation of `Hasher::write_usize` uses native endianness. Additionally the implementations of library types are occasionally changed as their internal fields change or hashing gets optimized. ## Question Should this go on the module level documentation instead since it also concerns `Hasher` to some extent and not just `Hash`? resolves #74215
2 parents 14ef447 + 53fc69f commit 43279b2

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/core/src/hash/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ mod sip;
164164
/// `0xFF` byte to the `Hasher` so that the values `("ab", "c")` and `("a",
165165
/// "bc")` hash differently.
166166
///
167+
/// ## Portability
168+
///
169+
/// Due to differences in endianness and type sizes, data fed by `Hash` to a `Hasher`
170+
/// should not be considered portable across platforms. Additionally the data passed by most
171+
/// standard library types should not be considered stable between compiler versions.
172+
///
173+
/// This means tests shouldn't probe hard-coded hash values or data fed to a `Hasher` and
174+
/// instead should check consistency with `Eq`.
175+
///
176+
/// Serialization formats intended to be portable between platforms or compiler versions should
177+
/// either avoid encoding hashes or only rely on `Hash` and `Hasher` implementations that
178+
/// provide additional guarantees.
179+
///
167180
/// [`HashMap`]: ../../std/collections/struct.HashMap.html
168181
/// [`HashSet`]: ../../std/collections/struct.HashSet.html
169182
/// [`hash`]: Hash::hash

0 commit comments

Comments
 (0)