2014-02-25 03:45:20 +00:00
|
|
|
//! Calculation and management of a Strict Version Hash for crates
|
|
|
|
//!
|
2016-05-06 09:01:09 +00:00
|
|
|
//! The SVH is used for incremental compilation to track when HIR
|
|
|
|
//! nodes have changed between compilations, and also to detect
|
|
|
|
//! mismatches where we have two versions of the same crate that were
|
|
|
|
//! compiled from distinct sources.
|
2014-02-25 03:45:20 +00:00
|
|
|
|
2024-07-28 22:13:50 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use rustc_macros::{Decodable_Generic, Encodable_Generic};
|
|
|
|
|
2023-04-15 17:53:50 +00:00
|
|
|
use crate::fingerprint::Fingerprint;
|
2019-02-08 16:36:22 +00:00
|
|
|
use crate::stable_hasher;
|
2018-08-03 18:22:22 +00:00
|
|
|
|
2023-12-31 19:35:32 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable_Generic, Decodable_Generic, Hash)]
|
2014-02-25 03:45:20 +00:00
|
|
|
pub struct Svh {
|
2023-04-15 17:53:50 +00:00
|
|
|
hash: Fingerprint,
|
2014-02-25 03:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Svh {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Creates a new `Svh` given the hash. If you actually want to
|
2016-03-28 21:36:56 +00:00
|
|
|
/// compute the SVH from some HIR, you want the `calculate_svh`
|
2021-04-07 19:47:01 +00:00
|
|
|
/// function found in `rustc_incremental`.
|
2023-04-15 17:53:50 +00:00
|
|
|
pub fn new(hash: Fingerprint) -> Svh {
|
2018-11-06 20:05:44 +00:00
|
|
|
Svh { hash }
|
2014-02-25 03:45:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 18:28:30 +00:00
|
|
|
pub fn as_u128(self) -> u128 {
|
|
|
|
self.hash.as_u128()
|
2016-05-06 09:01:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 18:28:30 +00:00
|
|
|
pub fn to_hex(self) -> String {
|
|
|
|
format!("{:032x}", self.hash.as_u128())
|
2016-03-28 21:36:56 +00:00
|
|
|
}
|
2014-02-25 03:45:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:45:07 +00:00
|
|
|
impl fmt::Display for Svh {
|
2019-02-08 16:36:22 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-04-30 18:28:30 +00:00
|
|
|
f.pad(&self.to_hex())
|
2014-02-25 03:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 23:02:39 +00:00
|
|
|
|
2018-08-03 18:22:22 +00:00
|
|
|
impl<T> stable_hasher::HashStable<T> for Svh {
|
|
|
|
#[inline]
|
2019-09-26 22:54:39 +00:00
|
|
|
fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) {
|
2019-12-22 22:42:04 +00:00
|
|
|
let Svh { hash } = *self;
|
2018-08-03 18:22:22 +00:00
|
|
|
hash.hash_stable(ctx, hasher);
|
|
|
|
}
|
|
|
|
}
|