2019-02-08 16:36:22 +00:00
|
|
|
use crate::stable_hasher;
|
2020-06-11 14:49:57 +00:00
|
|
|
use rustc_serialize::{
|
|
|
|
opaque::{self, EncodeResult},
|
|
|
|
Decodable, Encodable,
|
|
|
|
};
|
2020-09-02 01:27:02 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2017-12-23 03:41:09 +00:00
|
|
|
use std::mem;
|
2016-10-07 13:13:11 +00:00
|
|
|
|
2020-09-02 01:27:02 +00:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
|
2017-05-17 14:41:07 +00:00
|
|
|
pub struct Fingerprint(u64, u64);
|
2016-10-07 13:13:11 +00:00
|
|
|
|
|
|
|
impl Fingerprint {
|
2017-12-20 13:06:11 +00:00
|
|
|
pub const ZERO: Fingerprint = Fingerprint(0, 0);
|
2016-10-07 13:13:11 +00:00
|
|
|
|
2017-05-17 14:41:07 +00:00
|
|
|
#[inline]
|
2016-10-07 13:13:11 +00:00
|
|
|
pub fn from_smaller_hash(hash: u64) -> Fingerprint {
|
2017-05-17 14:41:07 +00:00
|
|
|
Fingerprint(hash, hash)
|
2016-10-07 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 14:41:07 +00:00
|
|
|
#[inline]
|
2016-10-07 13:13:11 +00:00
|
|
|
pub fn to_smaller_hash(&self) -> u64 {
|
2017-05-17 14:41:07 +00:00
|
|
|
self.0
|
2016-10-07 13:13:11 +00:00
|
|
|
}
|
2016-12-13 23:45:03 +00:00
|
|
|
|
2017-10-27 21:53:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn as_value(&self) -> (u64, u64) {
|
|
|
|
(self.0, self.1)
|
|
|
|
}
|
|
|
|
|
2017-06-12 15:00:55 +00: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),
|
2019-12-22 22:42:04 +00:00
|
|
|
self.1.wrapping_mul(3).wrapping_add(other.1),
|
2017-06-12 15:00:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-07-03 09:16:38 +00: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 {
|
2019-06-26 12:04:37 +00:00
|
|
|
let a = u128::from(self.1) << 64 | u128::from(self.0);
|
|
|
|
let b = u128::from(other.1) << 64 | u128::from(other.0);
|
2018-07-03 09:16:38 +00:00
|
|
|
|
|
|
|
let c = a.wrapping_add(b);
|
|
|
|
|
|
|
|
Fingerprint((c >> 64) as u64, c as u64)
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
pub fn to_hex(&self) -> String {
|
2017-05-17 14:41:07 +00:00
|
|
|
format!("{:x}{:x}", self.0, self.1)
|
2016-12-13 23:45:03 +00:00
|
|
|
}
|
2017-06-12 15:00:55 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
pub fn encode_opaque(&self, encoder: &mut opaque::Encoder) -> EncodeResult {
|
2017-12-23 03:41:09 +00:00
|
|
|
let bytes: [u8; 16] = unsafe { mem::transmute([self.0.to_le(), self.1.to_le()]) };
|
|
|
|
|
2018-06-04 20:14:02 +00:00
|
|
|
encoder.emit_raw_bytes(&bytes);
|
|
|
|
Ok(())
|
2017-12-23 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
pub fn decode_opaque(decoder: &mut opaque::Decoder<'_>) -> Result<Fingerprint, String> {
|
2017-12-23 03:41:09 +00:00
|
|
|
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 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::fmt::Display for Fingerprint {
|
2019-02-08 16:36:22 +00:00
|
|
|
fn fmt(&self, formatter: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
2017-05-17 14:41:07 +00:00
|
|
|
write!(formatter, "{:x}-{:x}", self.0, self.1)
|
2016-10-07 13:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 23:45:03 +00:00
|
|
|
|
2020-09-02 01:27:02 +00:00
|
|
|
impl Hash for Fingerprint {
|
|
|
|
#[inline]
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
state.write_fingerprint(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait FingerprintHasher {
|
|
|
|
fn write_fingerprint(&mut self, fingerprint: &Fingerprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<H: Hasher> FingerprintHasher for H {
|
|
|
|
#[inline]
|
|
|
|
default fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
|
|
|
|
self.write_u64(fingerprint.0);
|
|
|
|
self.write_u64(fingerprint.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FingerprintHasher for crate::unhash::Unhasher {
|
|
|
|
#[inline]
|
|
|
|
fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
|
|
|
|
// `Unhasher` only wants a single `u64`
|
|
|
|
self.write_u64(fingerprint.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
impl stable_hasher::StableHasherResult for Fingerprint {
|
2018-12-04 15:26:34 +00:00
|
|
|
#[inline]
|
2019-09-26 22:54:39 +00:00
|
|
|
fn finish(hasher: stable_hasher::StableHasher) -> Self {
|
2017-10-16 12:06:07 +00:00
|
|
|
let (_0, _1) = hasher.finalize();
|
|
|
|
Fingerprint(_0, _1)
|
2016-12-13 23:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-08 21:36:37 +00:00
|
|
|
|
2018-08-03 22:41:30 +00:00
|
|
|
impl_stable_hash_via_hash!(Fingerprint);
|
2017-12-23 03:41:09 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
|
|
|
|
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
|
|
|
s.encode_fingerprint(self)
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 03:41:09 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
|
|
|
|
fn decode(d: &mut D) -> Result<Self, D::Error> {
|
|
|
|
d.decode_fingerprint()
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 03:41:09 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
pub trait FingerprintEncoder: rustc_serialize::Encoder {
|
|
|
|
fn encode_fingerprint(&mut self, f: &Fingerprint) -> Result<(), Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FingerprintDecoder: rustc_serialize::Decoder {
|
|
|
|
fn decode_fingerprint(&mut self) -> Result<Fingerprint, Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: rustc_serialize::Encoder> FingerprintEncoder for E {
|
|
|
|
default fn encode_fingerprint(&mut self, _: &Fingerprint) -> Result<(), E::Error> {
|
|
|
|
panic!("Cannot encode `Fingerprint` with `{}`", std::any::type_name::<E>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FingerprintEncoder for opaque::Encoder {
|
|
|
|
fn encode_fingerprint(&mut self, f: &Fingerprint) -> EncodeResult {
|
2017-12-23 03:41:09 +00:00
|
|
|
f.encode_opaque(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
impl<D: rustc_serialize::Decoder> FingerprintDecoder for D {
|
|
|
|
default fn decode_fingerprint(&mut self) -> Result<Fingerprint, D::Error> {
|
|
|
|
panic!("Cannot decode `Fingerprint` with `{}`", std::any::type_name::<D>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FingerprintDecoder for opaque::Decoder<'_> {
|
|
|
|
fn decode_fingerprint(&mut self) -> Result<Fingerprint, String> {
|
2017-12-23 03:41:09 +00:00
|
|
|
Fingerprint::decode_opaque(self)
|
|
|
|
}
|
|
|
|
}
|