Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 8 | use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 9 | use std::fmt; |
| 10 | use std::hash::{Hash, Hasher}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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` |
Chris Wailes | 32f7835 | 2021-07-20 14:04:55 -0700 | [diff] [blame^] | 22 | /// function found in `rustc_incremental`. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 37 | fn hash<H>(&self, state: &mut H) |
| 38 | where |
| 39 | H: Hasher, |
| 40 | { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 41 | self.hash.to_le().hash(state); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl fmt::Display for Svh { |
| 46 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 47 | f.pad(&self.to_string()) |
| 48 | } |
| 49 | } |
| 50 | |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 51 | impl<S: Encoder> Encodable<S> for Svh { |
| 52 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 53 | s.emit_u64(self.as_u64().to_le()) |
| 54 | } |
| 55 | } |
| 56 | |
ThiƩbaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 57 | impl<D: Decoder> Decodable<D> for Svh { |
| 58 | fn decode(d: &mut D) -> Result<Svh, D::Error> { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 59 | d.read_u64().map(u64::from_le).map(Svh::new) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | impl<T> stable_hasher::HashStable<T> for Svh { |
| 64 | #[inline] |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 65 | fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 66 | let Svh { hash } = *self; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 67 | hash.hash_stable(ctx, hasher); |
| 68 | } |
| 69 | } |