]>
Commit | Line | Data |
---|---|---|
c30ab7b3 SL |
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 | ||
2c00a5a8 | 11 | use std::mem; |
476ff2be | 12 | use rustc_data_structures::stable_hasher; |
2c00a5a8 XL |
13 | use serialize; |
14 | use serialize::opaque::{EncodeResult, Encoder, Decoder}; | |
c30ab7b3 | 15 | |
2c00a5a8 | 16 | #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)] |
7cac9316 | 17 | pub struct Fingerprint(u64, u64); |
c30ab7b3 SL |
18 | |
19 | impl Fingerprint { | |
ff7c6d11 XL |
20 | |
21 | pub const ZERO: Fingerprint = Fingerprint(0, 0); | |
c30ab7b3 | 22 | |
7cac9316 | 23 | #[inline] |
c30ab7b3 | 24 | pub fn from_smaller_hash(hash: u64) -> Fingerprint { |
7cac9316 | 25 | Fingerprint(hash, hash) |
c30ab7b3 SL |
26 | } |
27 | ||
7cac9316 | 28 | #[inline] |
c30ab7b3 | 29 | pub fn to_smaller_hash(&self) -> u64 { |
7cac9316 | 30 | self.0 |
c30ab7b3 | 31 | } |
476ff2be | 32 | |
abe05a73 XL |
33 | #[inline] |
34 | pub fn as_value(&self) -> (u64, u64) { | |
35 | (self.0, self.1) | |
36 | } | |
37 | ||
041b39d2 XL |
38 | #[inline] |
39 | pub fn combine(self, other: Fingerprint) -> Fingerprint { | |
40 | // See https://stackoverflow.com/a/27952689 on why this function is | |
41 | // implemented this way. | |
42 | Fingerprint( | |
43 | self.0.wrapping_mul(3).wrapping_add(other.0), | |
44 | self.1.wrapping_mul(3).wrapping_add(other.1) | |
45 | ) | |
46 | } | |
47 | ||
476ff2be | 48 | pub fn to_hex(&self) -> String { |
7cac9316 | 49 | format!("{:x}{:x}", self.0, self.1) |
476ff2be | 50 | } |
041b39d2 | 51 | |
2c00a5a8 XL |
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 | } | |
57 | ||
58 | pub fn decode_opaque<'a>(decoder: &mut Decoder<'a>) -> Result<Fingerprint, String> { | |
59 | let mut bytes = [0; 16]; | |
60 | ||
61 | decoder.read_raw_bytes(&mut bytes)?; | |
62 | ||
63 | let [l, r]: [u64; 2] = unsafe { mem::transmute(bytes) }; | |
64 | ||
65 | Ok(Fingerprint(u64::from_le(l), u64::from_le(r))) | |
66 | } | |
c30ab7b3 SL |
67 | } |
68 | ||
7cac9316 XL |
69 | impl ::std::fmt::Display for Fingerprint { |
70 | fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { | |
71 | write!(formatter, "{:x}-{:x}", self.0, self.1) | |
c30ab7b3 SL |
72 | } |
73 | } | |
74 | ||
7cac9316 | 75 | impl stable_hasher::StableHasherResult for Fingerprint { |
abe05a73 XL |
76 | fn finish(hasher: stable_hasher::StableHasher<Self>) -> Self { |
77 | let (_0, _1) = hasher.finalize(); | |
78 | Fingerprint(_0, _1) | |
c30ab7b3 SL |
79 | } |
80 | } | |
476ff2be | 81 | |
7cac9316 XL |
82 | impl<CTX> stable_hasher::HashStable<CTX> for Fingerprint { |
83 | #[inline] | |
84 | fn hash_stable<W: stable_hasher::StableHasherResult>(&self, | |
85 | _: &mut CTX, | |
86 | hasher: &mut stable_hasher::StableHasher<W>) { | |
87 | ::std::hash::Hash::hash(self, hasher); | |
476ff2be SL |
88 | } |
89 | } | |
2c00a5a8 XL |
90 | |
91 | impl serialize::UseSpecializedEncodable for Fingerprint { } | |
92 | ||
93 | impl serialize::UseSpecializedDecodable for Fingerprint { } | |
94 | ||
95 | impl<'a> serialize::SpecializedEncoder<Fingerprint> for serialize::opaque::Encoder<'a> { | |
96 | fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> { | |
97 | f.encode_opaque(self) | |
98 | } | |
99 | } | |
100 | ||
101 | impl<'a> serialize::SpecializedDecoder<Fingerprint> for serialize::opaque::Decoder<'a> { | |
102 | fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> { | |
103 | Fingerprint::decode_opaque(self) | |
104 | } | |
105 | } |