]>
Commit | Line | Data |
---|---|---|
54a0048b SL |
1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! Calculation and management of a Strict Version Hash for crates | |
12 | //! | |
a7813a04 XL |
13 | //! The SVH is used for incremental compilation to track when HIR |
14 | //! nodes have changed between compilations, and also to detect | |
15 | //! mismatches where we have two versions of the same crate that were | |
16 | //! compiled from distinct sources. | |
54a0048b SL |
17 | |
18 | use std::fmt; | |
a7813a04 | 19 | use std::hash::{Hash, Hasher}; |
9e0c209e | 20 | use serialize::{Encodable, Decodable, Encoder, Decoder}; |
54a0048b | 21 | |
a7813a04 | 22 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
54a0048b | 23 | pub struct Svh { |
a7813a04 | 24 | hash: u64, |
54a0048b SL |
25 | } |
26 | ||
27 | impl Svh { | |
28 | /// Create a new `Svh` given the hash. If you actually want to | |
29 | /// compute the SVH from some HIR, you want the `calculate_svh` | |
a7813a04 XL |
30 | /// function found in `librustc_incremental`. |
31 | pub fn new(hash: u64) -> Svh { | |
54a0048b SL |
32 | Svh { hash: hash } |
33 | } | |
34 | ||
a7813a04 XL |
35 | pub fn as_u64(&self) -> u64 { |
36 | self.hash | |
37 | } | |
54a0048b | 38 | |
a7813a04 XL |
39 | pub fn to_string(&self) -> String { |
40 | format!("{:016x}", self.hash) | |
54a0048b | 41 | } |
a7813a04 | 42 | } |
54a0048b | 43 | |
a7813a04 XL |
44 | impl Hash for Svh { |
45 | fn hash<H>(&self, state: &mut H) where H: Hasher { | |
46 | self.hash.to_le().hash(state); | |
54a0048b SL |
47 | } |
48 | } | |
49 | ||
50 | impl fmt::Display for Svh { | |
51 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
a7813a04 | 52 | f.pad(&self.to_string()) |
54a0048b SL |
53 | } |
54 | } | |
9e0c209e SL |
55 | |
56 | impl Encodable for Svh { | |
57 | fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { | |
58 | s.emit_u64(self.as_u64().to_le()) | |
59 | } | |
60 | } | |
61 | ||
62 | impl Decodable for Svh { | |
63 | fn decode<D: Decoder>(d: &mut D) -> Result<Svh, D::Error> { | |
64 | d.read_u64() | |
65 | .map(u64::from_le) | |
66 | .map(Svh::new) | |
67 | } | |
68 | } |