1 //! Calculation and management of a Strict Version Hash for crates
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.
9 use std::hash::{Hash, Hasher};
10 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
12 use crate::stable_hasher;
14 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
20 /// Creates a new `Svh` given the hash. If you actually want to
21 /// compute the SVH from some HIR, you want the `calculate_svh`
22 /// function found in `librustc_incremental`.
23 pub fn new(hash: u64) -> Svh {
27 pub fn as_u64(&self) -> u64 {
31 pub fn to_string(&self) -> String {
32 format!("{:016x}", self.hash)
37 fn hash<H>(&self, state: &mut H) where H: Hasher {
38 self.hash.to_le().hash(state);
42 impl fmt::Display for Svh {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 f.pad(&self.to_string())
48 impl Encodable for Svh {
49 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
50 s.emit_u64(self.as_u64().to_le())
54 impl Decodable for Svh {
55 fn decode<D: Decoder>(d: &mut D) -> Result<Svh, D::Error> {
62 impl<T> stable_hasher::HashStable<T> for Svh {
64 fn hash_stable<W: stable_hasher::StableHasherResult>(
67 hasher: &mut stable_hasher::StableHasher<W>
72 hash.hash_stable(ctx, hasher);