]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/svh.rs
change svh to store a u64
[rust.git] / src / librustc / hir / svh.rs
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 //!
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.
17
18 use std::fmt;
19 use std::hash::{Hash, Hasher};
20
21 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
22 pub struct Svh {
23     hash: u64,
24 }
25
26 impl Svh {
27     /// Create a new `Svh` given the hash. If you actually want to
28     /// compute the SVH from some HIR, you want the `calculate_svh`
29     /// function found in `librustc_incremental`.
30     pub fn new(hash: u64) -> Svh {
31         Svh { hash: hash }
32     }
33
34     pub fn as_u64(&self) -> u64 {
35         self.hash
36     }
37
38     pub fn to_string(&self) -> String {
39         let hash = self.hash;
40         return (0..64).step_by(4).map(|i| hex(hash >> i)).collect();
41
42         fn hex(b: u64) -> char {
43             let b = (b & 0xf) as u8;
44             let b = match b {
45                 0 ... 9 => '0' as u8 + b,
46                 _ => 'a' as u8 + b - 10,
47             };
48             b as char
49         }
50     }
51 }
52
53 impl Hash for Svh {
54     fn hash<H>(&self, state: &mut H) where H: Hasher {
55         self.hash.to_le().hash(state);
56     }
57 }
58
59 impl fmt::Display for Svh {
60     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61         f.pad(&self.to_string())
62     }
63 }