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
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
2014-02-25 03:45:20 +00:00
|
|
|
use std::fmt;
|
2016-05-06 09:01:09 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2014-02-25 03:45:20 +00:00
|
|
|
|
2019-02-08 16:36:22 +00:00
|
|
|
use crate::stable_hasher;
|
2018-08-03 18:22:22 +00:00
|
|
|
|
2016-05-06 09:01:09 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2014-02-25 03:45:20 +00:00
|
|
|
pub struct Svh {
|
2016-05-06 09:01:09 +00:00
|
|
|
hash: u64,
|
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`
|
2016-05-06 09:01:09 +00:00
|
|
|
/// function found in `librustc_incremental`.
|
|
|
|
pub fn new(hash: u64) -> Svh {
|
2018-11-06 20:05:44 +00:00
|
|
|
Svh { hash }
|
2014-02-25 03:45:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 09:01:09 +00:00
|
|
|
pub fn as_u64(&self) -> u64 {
|
|
|
|
self.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(&self) -> String {
|
2016-05-12 14:12:58 +00:00
|
|
|
format!("{:016x}", self.hash)
|
2014-02-25 03:45:20 +00:00
|
|
|
}
|
2016-05-06 09:01:09 +00:00
|
|
|
}
|
2016-03-28 21:36:56 +00:00
|
|
|
|
2016-05-06 09:01:09 +00:00
|
|
|
impl Hash for Svh {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where
|
|
|
|
H: Hasher,
|
|
|
|
{
|
2016-05-06 09:01:09 +00:00
|
|
|
self.hash.to_le().hash(state);
|
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 {
|
2016-05-06 09:01:09 +00:00
|
|
|
f.pad(&self.to_string())
|
2014-02-25 03:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 23:02:39 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
impl<S: Encoder> Encodable<S> for Svh {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2016-08-11 23:02:39 +00:00
|
|
|
s.emit_u64(self.as_u64().to_le())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
impl<D: Decoder> Decodable<D> for Svh {
|
|
|
|
fn decode(d: &mut D) -> Result<Svh, D::Error> {
|
2019-12-22 22:42:04 +00:00
|
|
|
d.read_u64().map(u64::from_le).map(Svh::new)
|
2016-08-11 23:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 14:12:57 +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);
|
|
|
|
}
|
|
|
|
}
|