]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/fingerprint.rs
Auto merge of #69227 - Marwes:buffer_stderr, r=varkor
[rust.git] / src / librustc_data_structures / fingerprint.rs
1 use crate::stable_hasher;
2 use rustc_serialize::opaque::{Decoder, EncodeResult, Encoder};
3 use std::mem;
4
5 #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
6 pub struct Fingerprint(u64, u64);
7
8 impl Fingerprint {
9     pub const ZERO: Fingerprint = Fingerprint(0, 0);
10
11     #[inline]
12     pub fn from_smaller_hash(hash: u64) -> Fingerprint {
13         Fingerprint(hash, hash)
14     }
15
16     #[inline]
17     pub fn to_smaller_hash(&self) -> u64 {
18         self.0
19     }
20
21     #[inline]
22     pub fn as_value(&self) -> (u64, u64) {
23         (self.0, self.1)
24     }
25
26     #[inline]
27     pub fn combine(self, other: Fingerprint) -> Fingerprint {
28         // See https://stackoverflow.com/a/27952689 on why this function is
29         // implemented this way.
30         Fingerprint(
31             self.0.wrapping_mul(3).wrapping_add(other.0),
32             self.1.wrapping_mul(3).wrapping_add(other.1),
33         )
34     }
35
36     // Combines two hashes in an order independent way. Make sure this is what
37     // you want.
38     #[inline]
39     pub fn combine_commutative(self, other: Fingerprint) -> Fingerprint {
40         let a = u128::from(self.1) << 64 | u128::from(self.0);
41         let b = u128::from(other.1) << 64 | u128::from(other.0);
42
43         let c = a.wrapping_add(b);
44
45         Fingerprint((c >> 64) as u64, c as u64)
46     }
47
48     pub fn to_hex(&self) -> String {
49         format!("{:x}{:x}", self.0, self.1)
50     }
51
52     pub fn encode_opaque(&self, encoder: &mut Encoder) -> EncodeResult {
53         let bytes: [u8; 16] = unsafe { mem::transmute([self.0.to_le(), self.1.to_le()]) };
54
55         encoder.emit_raw_bytes(&bytes);
56         Ok(())
57     }
58
59     pub fn decode_opaque(decoder: &mut Decoder<'_>) -> Result<Fingerprint, String> {
60         let mut bytes = [0; 16];
61
62         decoder.read_raw_bytes(&mut bytes)?;
63
64         let [l, r]: [u64; 2] = unsafe { mem::transmute(bytes) };
65
66         Ok(Fingerprint(u64::from_le(l), u64::from_le(r)))
67     }
68 }
69
70 impl ::std::fmt::Display for Fingerprint {
71     fn fmt(&self, formatter: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
72         write!(formatter, "{:x}-{:x}", self.0, self.1)
73     }
74 }
75
76 impl stable_hasher::StableHasherResult for Fingerprint {
77     #[inline]
78     fn finish(hasher: stable_hasher::StableHasher) -> Self {
79         let (_0, _1) = hasher.finalize();
80         Fingerprint(_0, _1)
81     }
82 }
83
84 impl_stable_hash_via_hash!(Fingerprint);
85
86 impl rustc_serialize::UseSpecializedEncodable for Fingerprint {}
87
88 impl rustc_serialize::UseSpecializedDecodable for Fingerprint {}
89
90 impl rustc_serialize::SpecializedEncoder<Fingerprint> for Encoder {
91     fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
92         f.encode_opaque(self)
93     }
94 }
95
96 impl<'a> rustc_serialize::SpecializedDecoder<Fingerprint> for Decoder<'a> {
97     fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
98         Fingerprint::decode_opaque(self)
99     }
100 }