rustc_data_structures/
svh.rs

1//! Calculation and management of a Strict Version Hash for crates
2//!
3//! The SVH is used for incremental compilation to track when HIR
4//! nodes have changed between compilations, and also to detect
5//! mismatches where we have two versions of the same crate that were
6//! compiled from distinct sources.
7
8use std::fmt;
9
10use rustc_macros::{Decodable_NoContext, Encodable_NoContext};
11
12use crate::fingerprint::Fingerprint;
13use crate::stable_hasher;
14
15#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable_NoContext, Decodable_NoContext, Hash)]
16pub struct Svh {
17    hash: Fingerprint,
18}
19
20impl Svh {
21    /// Creates a new `Svh` given the hash. If you actually want to
22    /// compute the SVH from some HIR, you want the `calculate_svh`
23    /// function found in `rustc_incremental`.
24    pub fn new(hash: Fingerprint) -> Svh {
25        Svh { hash }
26    }
27
28    pub fn as_u128(self) -> u128 {
29        self.hash.as_u128()
30    }
31
32    pub fn to_hex(self) -> String {
33        format!("{:032x}", self.hash.as_u128())
34    }
35}
36
37impl fmt::Display for Svh {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.pad(&self.to_hex())
40    }
41}
42
43impl<T> stable_hasher::HashStable<T> for Svh {
44    #[inline]
45    fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) {
46        let Svh { hash } = *self;
47        hash.hash_stable(ctx, hasher);
48    }
49}