rustc_data_structures/
fingerprint.rs1use std::hash::{Hash, Hasher};
2
3use rustc_hashes::Hash64;
4use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
5
6use crate::stable_hasher::{FromStableHash, StableHasherHash, impl_stable_traits_for_trivial_type};
7
8#[cfg(test)]
9mod tests;
10
11#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
12#[repr(C)]
13pub struct Fingerprint(u64, u64);
14
15pub trait FingerprintComponent {
16    fn as_u64(&self) -> u64;
17}
18
19impl FingerprintComponent for Hash64 {
20    #[inline]
21    fn as_u64(&self) -> u64 {
22        Hash64::as_u64(*self)
23    }
24}
25
26impl FingerprintComponent for u64 {
27    #[inline]
28    fn as_u64(&self) -> u64 {
29        *self
30    }
31}
32
33impl Fingerprint {
34    pub const ZERO: Fingerprint = Fingerprint(0, 0);
35
36    #[inline]
37    pub fn new<A, B>(_0: A, _1: B) -> Fingerprint
38    where
39        A: FingerprintComponent,
40        B: FingerprintComponent,
41    {
42        Fingerprint(_0.as_u64(), _1.as_u64())
43    }
44
45    #[inline]
46    pub fn to_smaller_hash(&self) -> Hash64 {
47        Hash64::new(self.0.wrapping_mul(3).wrapping_add(self.1))
53    }
54
55    #[inline]
56    pub fn split(&self) -> (Hash64, Hash64) {
57        (Hash64::new(self.0), Hash64::new(self.1))
58    }
59
60    #[inline]
61    pub fn combine(self, other: Fingerprint) -> Fingerprint {
62        Fingerprint(
65            self.0.wrapping_mul(3).wrapping_add(other.0),
66            self.1.wrapping_mul(3).wrapping_add(other.1),
67        )
68    }
69
70    #[inline]
71    pub(crate) fn as_u128(self) -> u128 {
72        u128::from(self.1) << 64 | u128::from(self.0)
73    }
74
75    #[inline]
78    pub fn combine_commutative(self, other: Fingerprint) -> Fingerprint {
79        let a = u128::from(self.1) << 64 | u128::from(self.0);
80        let b = u128::from(other.1) << 64 | u128::from(other.0);
81
82        let c = a.wrapping_add(b);
83
84        Fingerprint(c as u64, (c >> 64) as u64)
85    }
86
87    pub fn to_hex(&self) -> String {
88        format!("{:x}{:x}", self.0, self.1)
89    }
90
91    #[inline]
92    pub fn to_le_bytes(&self) -> [u8; 16] {
93        let mut result = [0u8; 16];
96
97        let first_half: &mut [u8; 8] = (&mut result[0..8]).try_into().unwrap();
98        *first_half = self.0.to_le_bytes();
99
100        let second_half: &mut [u8; 8] = (&mut result[8..16]).try_into().unwrap();
101        *second_half = self.1.to_le_bytes();
102
103        result
104    }
105
106    #[inline]
107    pub fn from_le_bytes(bytes: [u8; 16]) -> Fingerprint {
108        Fingerprint(
109            u64::from_le_bytes(bytes[0..8].try_into().unwrap()),
110            u64::from_le_bytes(bytes[8..16].try_into().unwrap()),
111        )
112    }
113}
114
115impl std::fmt::Display for Fingerprint {
116    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        write!(formatter, "{:x}-{:x}", self.0, self.1)
118    }
119}
120
121impl Hash for Fingerprint {
122    #[inline]
123    fn hash<H: Hasher>(&self, state: &mut H) {
124        state.write_fingerprint(self);
125    }
126}
127
128trait FingerprintHasher {
129    fn write_fingerprint(&mut self, fingerprint: &Fingerprint);
130}
131
132impl<H: Hasher> FingerprintHasher for H {
133    #[inline]
134    default fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
135        self.write_u64(fingerprint.0);
136        self.write_u64(fingerprint.1);
137    }
138}
139
140impl FingerprintHasher for crate::unhash::Unhasher {
141    #[inline]
142    fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
143        self.write_u64(fingerprint.0.wrapping_add(fingerprint.1));
156    }
157}
158
159impl FromStableHash for Fingerprint {
160    type Hash = StableHasherHash;
161
162    #[inline]
163    fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
164        Fingerprint(_0, _1)
165    }
166}
167
168impl_stable_traits_for_trivial_type!(Fingerprint);
169
170impl<E: Encoder> Encodable<E> for Fingerprint {
171    #[inline]
172    fn encode(&self, s: &mut E) {
173        s.emit_raw_bytes(&self.to_le_bytes());
174    }
175}
176
177impl<D: Decoder> Decodable<D> for Fingerprint {
178    #[inline]
179    fn decode(d: &mut D) -> Self {
180        Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap())
181    }
182}
183
184#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), repr(packed))]
200#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
201pub struct PackedFingerprint(Fingerprint);
202
203impl std::fmt::Display for PackedFingerprint {
204    #[inline]
205    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
206        let copy = self.0;
208        copy.fmt(formatter)
209    }
210}
211
212impl<E: Encoder> Encodable<E> for PackedFingerprint {
213    #[inline]
214    fn encode(&self, s: &mut E) {
215        let copy = self.0;
217        copy.encode(s);
218    }
219}
220
221impl<D: Decoder> Decodable<D> for PackedFingerprint {
222    #[inline]
223    fn decode(d: &mut D) -> Self {
224        Self(Fingerprint::decode(d))
225    }
226}
227
228impl From<Fingerprint> for PackedFingerprint {
229    #[inline]
230    fn from(f: Fingerprint) -> PackedFingerprint {
231        PackedFingerprint(f)
232    }
233}
234
235impl From<PackedFingerprint> for Fingerprint {
236    #[inline]
237    fn from(f: PackedFingerprint) -> Fingerprint {
238        f.0
239    }
240}