2016-10-07 09:13:11 -04:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-12-22 22:41:09 -05:00
|
|
|
use std::mem;
|
2018-08-03 18:34:23 -06:00
|
|
|
use stable_hasher;
|
2017-12-22 22:41:09 -05:00
|
|
|
use serialize;
|
|
|
|
use serialize::opaque::{EncodeResult, Encoder, Decoder};
|
2016-10-07 09:13:11 -04:00
|
|
|
|
2017-12-22 22:41:09 -05:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
|
2017-05-17 16:41:07 +02:00
|
|
|
pub struct Fingerprint(u64, u64);
|
2016-10-07 09:13:11 -04:00
|
|
|
|
|
|
|
impl Fingerprint {
|
2017-12-20 14:06:11 +01:00
|
|
|
|
|
|
|
pub const ZERO: Fingerprint = Fingerprint(0, 0);
|
2016-10-07 09:13:11 -04:00
|
|
|
|
2017-05-17 16:41:07 +02:00
|
|
|
#[inline]
|
2016-10-07 09:13:11 -04:00
|
|
|
pub fn from_smaller_hash(hash: u64) -> Fingerprint {
|
2017-05-17 16:41:07 +02:00
|
|
|
Fingerprint(hash, hash)
|
2016-10-07 09:13:11 -04:00
|
|
|
}
|
|
|
|
|
2017-05-17 16:41:07 +02:00
|
|
|
#[inline]
|
2016-10-07 09:13:11 -04:00
|
|
|
pub fn to_smaller_hash(&self) -> u64 {
|
2017-05-17 16:41:07 +02:00
|
|
|
self.0
|
2016-10-07 09:13:11 -04:00
|
|
|
}
|
2016-12-14 01:45:03 +02:00
|
|
|
|
2017-10-27 23:53:57 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn as_value(&self) -> (u64, u64) {
|
|
|
|
(self.0, self.1)
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:00:55 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn combine(self, other: Fingerprint) -> Fingerprint {
|
|
|
|
// See https://stackoverflow.com/a/27952689 on why this function is
|
|
|
|
// implemented this way.
|
|
|
|
Fingerprint(
|
|
|
|
self.0.wrapping_mul(3).wrapping_add(other.0),
|
|
|
|
self.1.wrapping_mul(3).wrapping_add(other.1)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-07-03 11:16:38 +02:00
|
|
|
// Combines two hashes in an order independent way. Make sure this is what
|
|
|
|
// you want.
|
|
|
|
#[inline]
|
|
|
|
pub fn combine_commutative(self, other: Fingerprint) -> Fingerprint {
|
|
|
|
let a = (self.1 as u128) << 64 | self.0 as u128;
|
|
|
|
let b = (other.1 as u128) << 64 | other.0 as u128;
|
|
|
|
|
|
|
|
let c = a.wrapping_add(b);
|
|
|
|
|
|
|
|
Fingerprint((c >> 64) as u64, c as u64)
|
|
|
|
}
|
|
|
|
|
2016-12-14 01:45:03 +02:00
|
|
|
pub fn to_hex(&self) -> String {
|
2017-05-17 16:41:07 +02:00
|
|
|
format!("{:x}{:x}", self.0, self.1)
|
2016-12-14 01:45:03 +02:00
|
|
|
}
|
2017-06-12 17:00:55 +02:00
|
|
|
|
2017-12-22 22:41:09 -05:00
|
|
|
pub fn encode_opaque(&self, encoder: &mut Encoder) -> EncodeResult {
|
|
|
|
let bytes: [u8; 16] = unsafe { mem::transmute([self.0.to_le(), self.1.to_le()]) };
|
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
encoder.emit_raw_bytes(&bytes);
|
|
|
|
Ok(())
|
2017-12-22 22:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode_opaque<'a>(decoder: &mut Decoder<'a>) -> Result<Fingerprint, String> {
|
|
|
|
let mut bytes = [0; 16];
|
|
|
|
|
|
|
|
decoder.read_raw_bytes(&mut bytes)?;
|
|
|
|
|
|
|
|
let [l, r]: [u64; 2] = unsafe { mem::transmute(bytes) };
|
|
|
|
|
|
|
|
Ok(Fingerprint(u64::from_le(l), u64::from_le(r)))
|
|
|
|
}
|
2016-10-07 09:13:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::fmt::Display for Fingerprint {
|
2018-05-09 01:41:44 +02:00
|
|
|
fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
2017-05-17 16:41:07 +02:00
|
|
|
write!(formatter, "{:x}-{:x}", self.0, self.1)
|
2016-10-07 09:13:11 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 01:45:03 +02:00
|
|
|
|
|
|
|
impl stable_hasher::StableHasherResult for Fingerprint {
|
2017-10-16 14:06:07 +02:00
|
|
|
fn finish(hasher: stable_hasher::StableHasher<Self>) -> Self {
|
|
|
|
let (_0, _1) = hasher.finalize();
|
|
|
|
Fingerprint(_0, _1)
|
2016-12-14 01:45:03 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-08 23:36:37 +02:00
|
|
|
|
2018-08-03 16:41:30 -06:00
|
|
|
impl_stable_hash_via_hash!(Fingerprint);
|
2017-12-22 22:41:09 -05:00
|
|
|
|
|
|
|
impl serialize::UseSpecializedEncodable for Fingerprint { }
|
|
|
|
|
|
|
|
impl serialize::UseSpecializedDecodable for Fingerprint { }
|
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
impl serialize::SpecializedEncoder<Fingerprint> for serialize::opaque::Encoder {
|
2017-12-22 22:41:09 -05:00
|
|
|
fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
|
|
|
|
f.encode_opaque(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> serialize::SpecializedDecoder<Fingerprint> for serialize::opaque::Decoder<'a> {
|
|
|
|
fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
|
|
|
|
Fingerprint::decode_opaque(self)
|
|
|
|
}
|
|
|
|
}
|