]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/svh.rs
save-analysis: make sure we save the def for the last segment of a path
[rust.git] / src / librustc_data_structures / 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 use serialize::{Encodable, Decodable, Encoder, Decoder};
21
22 use stable_hasher;
23
24 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
25 pub struct Svh {
26     hash: u64,
27 }
28
29 impl Svh {
30     /// Create a new `Svh` given the hash. If you actually want to
31     /// compute the SVH from some HIR, you want the `calculate_svh`
32     /// function found in `librustc_incremental`.
33     pub fn new(hash: u64) -> Svh {
34         Svh { hash: hash }
35     }
36
37     pub fn as_u64(&self) -> u64 {
38         self.hash
39     }
40
41     pub fn to_string(&self) -> String {
42         format!("{:016x}", self.hash)
43     }
44 }
45
46 impl Hash for Svh {
47     fn hash<H>(&self, state: &mut H) where H: Hasher {
48         self.hash.to_le().hash(state);
49     }
50 }
51
52 impl fmt::Display for Svh {
53     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54         f.pad(&self.to_string())
55     }
56 }
57
58 impl Encodable for Svh {
59     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
60         s.emit_u64(self.as_u64().to_le())
61     }
62 }
63
64 impl Decodable for Svh {
65     fn decode<D: Decoder>(d: &mut D) -> Result<Svh, D::Error> {
66         d.read_u64()
67          .map(u64::from_le)
68          .map(Svh::new)
69     }
70 }
71
72 impl<T> stable_hasher::HashStable<T> for Svh {
73     #[inline]
74     fn hash_stable<W: stable_hasher::StableHasherResult>(
75         &self,
76         ctx: &mut T,
77         hasher: &mut stable_hasher::StableHasher<W>
78     ) {
79         let Svh {
80             hash
81         } = *self;
82         hash.hash_stable(ctx, hasher);
83     }
84 }