Skip to main content

StableHash

Trait StableHash 

Source
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 PartialEq implementations:

    • x == y implies stable_hash(x) == stable_hash(y), and
    • x != y implies stable_hash(x) != stable_hash(y).

    That second condition is usually not required for hash functions (e.g. Hash). In practice this means that stable_hash must feed any information into the hasher that a PartialEq comparison 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. The StableHasher takes care of endianness and isize/usize platform differences.

§Consistency across compiler stages

Because the stage1 compiler is built against the bootstrap stdlib, but the stage2 compiler uses in-tree stdlib, implementations of StableHash must not rely on Hash impls that were provided or derived by the standard library. Doing so leads to weird bugs and CI failures that can block changes to those stdlib hash impls.

(Unfortunately, as of late 2026 we have many stable-hash impls that do leak details of stdlib hash impls.)

See https://github.com/rust-lang/rust/pull/164458 for more context.

Required Methods§

Source

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".

Implementations on Foreign Types§

Source§

impl StableHash for !

Source§

fn stable_hash<Hcx>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher)

Source§

impl StableHash for ()

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for Hash64

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for Hash128

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for Ordering

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for OsStr

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for Path

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for PathBuf

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for String

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for [u8]

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for bool

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for char

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for f32

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for f64

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for i8

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for i16

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for i32

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for i64

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for i128

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for isize

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for str

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl StableHash for u8

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for u16

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for u32

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for u64

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for u128

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl StableHash for usize

Source§

fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher)

Source§

impl<'a, T> StableHash for &'a T
where T: StableHash + ?Sized,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<A, const N: usize> StableHash for SmallVec<[A; N]>
where A: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<I: Idx, T> StableHash for IndexSlice<I, T>
where T: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<I: Idx, T> StableHash for IndexVec<I, T>
where T: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<I: Idx> StableHash for DenseBitSet<I>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, _hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<K, R> StableHash for IndexSet<K, R>
where K: StableHash + Eq + Hash, R: BuildHasher,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<K, V, R> StableHash for IndexMap<K, V, R>
where K: StableHash + Eq + Hash, V: StableHash, R: BuildHasher,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<K, V> !StableHash for HashMap<K, V>

Source§

impl<K, V> StableHash for BTreeMap<K, V>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<K> StableHash for BTreeSet<K>
where K: StableHash + StableOrd,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<R: Idx, C: Idx> StableHash for BitMatrix<R, C>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, _hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T1, T2, T3, T4> StableHash for (T1, T2, T3, T4)
where T1: StableHash, T2: StableHash, T3: StableHash, T4: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T1, T2, T3> StableHash for (T1, T2, T3)
where T1: StableHash, T2: StableHash, T3: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T1, T2> StableHash for Result<T1, T2>
where T1: StableHash, T2: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T1: StableHash, T2: StableHash> StableHash for (T1, T2)

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T1: StableHash> StableHash for (T1,)

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T: ?Sized + StableHash> StableHash for Arc<T>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T: ?Sized + StableHash> StableHash for Box<T>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T: ?Sized + StableHash> StableHash for Rc<T>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T: StableHash + ZeroablePrimitive> StableHash for NonZero<T>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T: StableHash> StableHash for ThinVec<T>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T: StableHash> StableHash for Vec<T>

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T: StableHash> StableHash for [T]

Source§

default fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T> !StableHash for Discriminant<T>

There is no way to stable-hash a Discriminant without leaking unstable details of how the host compiler chooses and hashes discriminants.

That would be bad, because stable hashes must be consistent between the stage1 compiler (built with the bootstrap compiler/stdlib) and the stage2 compiler (built with the in-tree compiler/stdlib).

See https://github.com/rust-lang/rust/issues/164734 for more context.

If you need to manually stable-hash a discriminant, instead derive StableHash on a dedicated “tag” enum, and then hash the appropriate tag for each variant. For an example, see the impl of StableHash for Result.

Source§

impl<T> StableHash for Option<T>
where T: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<T> StableHash for PhantomData<T>

Source§

fn stable_hash<Hcx>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher)

Source§

impl<T> StableHash for RangeInclusive<T>
where T: StableHash,

Source§

fn stable_hash<Hcx: StableHashCtxt>( &self, hcx: &mut Hcx, hasher: &mut StableHasher, )

Source§

impl<V> !StableHash for HashSet<V>

Implementors§