]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/svh.rs
Rollup merge of #62994 - iluuu1994:test-for-43398, r=nikomatsakis
[rust.git] / src / librustc_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
8 use std::fmt;
9 use std::hash::{Hash, Hasher};
10 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
11
12 use crate::stable_hasher;
13
14 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
15 pub struct Svh {
16     hash: u64,
17 }
18
19 impl Svh {
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 {
24         Svh { hash }
25     }
26
27     pub fn as_u64(&self) -> u64 {
28         self.hash
29     }
30
31     pub fn to_string(&self) -> String {
32         format!("{:016x}", self.hash)
33     }
34 }
35
36 impl Hash for Svh {
37     fn hash<H>(&self, state: &mut H) where H: Hasher {
38         self.hash.to_le().hash(state);
39     }
40 }
41
42 impl fmt::Display for Svh {
43     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44         f.pad(&self.to_string())
45     }
46 }
47
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())
51     }
52 }
53
54 impl Decodable for Svh {
55     fn decode<D: Decoder>(d: &mut D) -> Result<Svh, D::Error> {
56         d.read_u64()
57          .map(u64::from_le)
58          .map(Svh::new)
59     }
60 }
61
62 impl<T> stable_hasher::HashStable<T> for Svh {
63     #[inline]
64     fn hash_stable<W: stable_hasher::StableHasherResult>(
65         &self,
66         ctx: &mut T,
67         hasher: &mut stable_hasher::StableHasher<W>
68     ) {
69         let Svh {
70             hash
71         } = *self;
72         hash.hash_stable(ctx, hasher);
73     }
74 }