diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2018-08-03 12:22:22 -0600 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2018-08-09 10:00:25 -0600 |
| commit | bf103700c6e2aa51e0531ab807b554c17ce9ba7d (patch) | |
| tree | f124d81af68202044d3b30ce04258a0399c035da /src/librustc_data_structures | |
| parent | b73535f1e9c46a698527fab51b1cc9fad3f53412 (diff) | |
| download | rust-bf103700c6e2aa51e0531ab807b554c17ce9ba7d.tar.gz rust-bf103700c6e2aa51e0531ab807b554c17ce9ba7d.zip | |
Move SVH structure to data structures
Diffstat (limited to 'src/librustc_data_structures')
| -rw-r--r-- | src/librustc_data_structures/lib.rs | 2 | ||||
| -rw-r--r-- | src/librustc_data_structures/svh.rs | 84 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index dd90cf7ae19..b8c21afc386 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -46,6 +46,7 @@ extern crate stable_deref_trait; extern crate rustc_rayon as rayon; extern crate rustc_rayon_core as rayon_core; extern crate rustc_hash; +extern crate serialize; // See librustc_cratesio_shim/Cargo.toml for a comment explaining this. #[allow(unused_extern_crates)] @@ -53,6 +54,7 @@ extern crate rustc_cratesio_shim; pub use rustc_serialize::hex::ToHex; +pub mod svh; pub mod accumulate_vec; pub mod array_vec; pub mod base_n; diff --git a/src/librustc_data_structures/svh.rs b/src/librustc_data_structures/svh.rs new file mode 100644 index 00000000000..94f132562b5 --- /dev/null +++ b/src/librustc_data_structures/svh.rs @@ -0,0 +1,84 @@ +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Calculation and management of a Strict Version Hash for crates +//! +//! 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. + +use std::fmt; +use std::hash::{Hash, Hasher}; +use serialize::{Encodable, Decodable, Encoder, Decoder}; + +use stable_hasher; + +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +pub struct Svh { + hash: u64, +} + +impl Svh { + /// Create a new `Svh` given the hash. If you actually want to + /// compute the SVH from some HIR, you want the `calculate_svh` + /// function found in `librustc_incremental`. + pub fn new(hash: u64) -> Svh { + Svh { hash: hash } + } + + pub fn as_u64(&self) -> u64 { + self.hash + } + + pub fn to_string(&self) -> String { + format!("{:016x}", self.hash) + } +} + +impl Hash for Svh { + fn hash<H>(&self, state: &mut H) where H: Hasher { + self.hash.to_le().hash(state); + } +} + +impl fmt::Display for Svh { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.pad(&self.to_string()) + } +} + +impl Encodable for Svh { + fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_u64(self.as_u64().to_le()) + } +} + +impl Decodable for Svh { + fn decode<D: Decoder>(d: &mut D) -> Result<Svh, D::Error> { + d.read_u64() + .map(u64::from_le) + .map(Svh::new) + } +} + +impl<T> stable_hasher::HashStable<T> for Svh { + #[inline] + fn hash_stable<W: stable_hasher::StableHasherResult>( + &self, + ctx: &mut T, + hasher: &mut stable_hasher::StableHasher<W> + ) { + let Svh { + hash + } = *self; + hash.hash_stable(ctx, hasher); + } +} |
