]> git.lizzy.rs Git - rust.git/blob - src/librustc/ich/fingerprint.rs
Use in-memory representation for Fingerprint that is more amenable to hashing.
[rust.git] / src / librustc / ich / fingerprint.rs
1 // Copyright 2016 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 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
12 use rustc_data_structures::stable_hasher;
13 use std::mem;
14 use std::slice;
15
16 #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
17 pub struct Fingerprint(u64, u64);
18
19 impl Fingerprint {
20     #[inline]
21     pub fn zero() -> Fingerprint {
22         Fingerprint(0, 0)
23     }
24
25     #[inline]
26     pub fn from_smaller_hash(hash: u64) -> Fingerprint {
27         Fingerprint(hash, hash)
28     }
29
30     #[inline]
31     pub fn to_smaller_hash(&self) -> u64 {
32         self.0
33     }
34
35     pub fn to_hex(&self) -> String {
36         format!("{:x}{:x}", self.0, self.1)
37     }
38 }
39
40 impl Encodable for Fingerprint {
41     #[inline]
42     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
43         s.emit_u64(self.0.to_le())?;
44         s.emit_u64(self.1.to_le())
45     }
46 }
47
48 impl Decodable for Fingerprint {
49     #[inline]
50     fn decode<D: Decoder>(d: &mut D) -> Result<Fingerprint, D::Error> {
51         let _0 = u64::from_le(d.read_u64()?);
52         let _1 = u64::from_le(d.read_u64()?);
53         Ok(Fingerprint(_0, _1))
54     }
55 }
56
57 impl ::std::fmt::Display for Fingerprint {
58     fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
59         write!(formatter, "{:x}-{:x}", self.0, self.1)
60     }
61 }
62
63 impl stable_hasher::StableHasherResult for Fingerprint {
64     fn finish(mut hasher: stable_hasher::StableHasher<Self>) -> Self {
65         let hash_bytes: &[u8] = hasher.finalize();
66
67         assert!(hash_bytes.len() >= mem::size_of::<u64>() * 2);
68         let hash_bytes: &[u64] = unsafe {
69             slice::from_raw_parts(hash_bytes.as_ptr() as *const u64, 2)
70         };
71
72         // The bytes returned bytes the Blake2B hasher are always little-endian.
73         Fingerprint(u64::from_le(hash_bytes[0]), u64::from_le(hash_bytes[1]))
74     }
75 }
76
77 impl<CTX> stable_hasher::HashStable<CTX> for Fingerprint {
78     #[inline]
79     fn hash_stable<W: stable_hasher::StableHasherResult>(&self,
80                                           _: &mut CTX,
81                                           hasher: &mut stable_hasher::StableHasher<W>) {
82         ::std::hash::Hash::hash(self, hasher);
83     }
84 }