pub trait HashStable<CTX> {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher);
}
Expand description
Something that implements HashStable<CTX>
can be hashed in a way that is
stable across multiple compilation sessions.
Note that HashStable
imposes rather more strict requirements than usual
hash functions:
-
Stable hashes are sometimes used as identifiers. Therefore they must conform to the corresponding
PartialEq
implementations:x == y
implieshash_stable(x) == hash_stable(y)
, andx != y
implieshash_stable(x) != hash_stable(y)
.
That second condition is usually not required for hash functions (e.g.
Hash
). In practice this means thathash_stable
must feed any information into the hasher that aPartialEq
comparison takes into account. See #49300 for an example where violating this invariant has caused trouble in the past. -
hash_stable()
must be independent of the current compilation session. E.g. they must not hash memory addresses or other things that are “randomly” assigned per compilation session. -
hash_stable()
must be independent of the host architecture. TheStableHasher
takes care of endianness andisize
/usize
platform differences.