pub trait StableHash {
// Required method
fn stable_hash<Hcx: StableHashCtxt>(
&self,
hcx: &mut Hcx,
hasher: &mut StableHasher,
);
}Expand description
Something that implements StableHash can be hashed in a way that is
stable across multiple compilation sessions.
Note that StableHash imposes rather more strict requirements than usual
hash functions:
-
Stable hashes are sometimes used as identifiers. Therefore they must conform to the corresponding
PartialEqimplementations:x == yimpliesstable_hash(x) == stable_hash(y), andx != yimpliesstable_hash(x) != stable_hash(y).
That second condition is usually not required for hash functions (e.g.
Hash). In practice this means thatstable_hashmust feed any information into the hasher that aPartialEqcomparison takes into account. See #49300 for an example where violating this invariant has caused trouble in the past. -
stable_hash()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. -
stable_hash()must be independent of the host architecture. TheStableHashertakes care of endianness andisize/usizeplatform differences.
Required Methods§
fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.