]>
Commit | Line | Data |
---|---|---|
54a0048b SL |
1 | //! Calculation and management of a Strict Version Hash for crates |
2 | //! | |
a7813a04 XL |
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. | |
54a0048b | 7 | |
dfeec247 | 8 | use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; |
54a0048b | 9 | use std::fmt; |
a7813a04 | 10 | use std::hash::{Hash, Hasher}; |
54a0048b | 11 | |
9fa01778 | 12 | use crate::stable_hasher; |
b7449926 | 13 | |
a7813a04 | 14 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
54a0048b | 15 | pub struct Svh { |
a7813a04 | 16 | hash: u64, |
54a0048b SL |
17 | } |
18 | ||
19 | impl Svh { | |
9fa01778 | 20 | /// Creates a new `Svh` given the hash. If you actually want to |
54a0048b | 21 | /// compute the SVH from some HIR, you want the `calculate_svh` |
a7813a04 XL |
22 | /// function found in `librustc_incremental`. |
23 | pub fn new(hash: u64) -> Svh { | |
a1dfa0c6 | 24 | Svh { hash } |
54a0048b SL |
25 | } |
26 | ||
a7813a04 XL |
27 | pub fn as_u64(&self) -> u64 { |
28 | self.hash | |
29 | } | |
54a0048b | 30 | |
a7813a04 XL |
31 | pub fn to_string(&self) -> String { |
32 | format!("{:016x}", self.hash) | |
54a0048b | 33 | } |
a7813a04 | 34 | } |
54a0048b | 35 | |
a7813a04 | 36 | impl Hash for Svh { |
dfeec247 XL |
37 | fn hash<H>(&self, state: &mut H) |
38 | where | |
39 | H: Hasher, | |
40 | { | |
a7813a04 | 41 | self.hash.to_le().hash(state); |
54a0048b SL |
42 | } |
43 | } | |
44 | ||
45 | impl fmt::Display for Svh { | |
9fa01778 | 46 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
a7813a04 | 47 | f.pad(&self.to_string()) |
54a0048b SL |
48 | } |
49 | } | |
9e0c209e | 50 | |
3dfed10e XL |
51 | impl<S: Encoder> Encodable<S> for Svh { |
52 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
9e0c209e SL |
53 | s.emit_u64(self.as_u64().to_le()) |
54 | } | |
55 | } | |
56 | ||
3dfed10e XL |
57 | impl<D: Decoder> Decodable<D> for Svh { |
58 | fn decode(d: &mut D) -> Result<Svh, D::Error> { | |
dfeec247 | 59 | d.read_u64().map(u64::from_le).map(Svh::new) |
9e0c209e SL |
60 | } |
61 | } | |
7cac9316 | 62 | |
b7449926 XL |
63 | impl<T> stable_hasher::HashStable<T> for Svh { |
64 | #[inline] | |
e74abb32 | 65 | fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) { |
dfeec247 | 66 | let Svh { hash } = *self; |
b7449926 XL |
67 | hash.hash_stable(ctx, hasher); |
68 | } | |
69 | } |