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.
|
|
|
|
|
2016-12-14 01:45:03 +02:00
|
|
|
use rustc_data_structures::stable_hasher;
|
2017-05-17 16:41:07 +02:00
|
|
|
use std::mem;
|
|
|
|
use std::slice;
|
2016-10-07 09:13:11 -04:00
|
|
|
|
2017-05-31 15:53:35 +01:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy, RustcEncodable, RustcDecodable)]
|
2017-05-17 16:41:07 +02:00
|
|
|
pub struct Fingerprint(u64, u64);
|
2016-10-07 09:13:11 -04:00
|
|
|
|
|
|
|
impl Fingerprint {
|
|
|
|
#[inline]
|
|
|
|
pub fn zero() -> Fingerprint {
|
2017-05-17 16:41:07 +02:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-10-07 09:13:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::fmt::Display for Fingerprint {
|
|
|
|
fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
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 {
|
|
|
|
fn finish(mut hasher: stable_hasher::StableHasher<Self>) -> Self {
|
2017-05-17 16:41:07 +02:00
|
|
|
let hash_bytes: &[u8] = hasher.finalize();
|
|
|
|
|
|
|
|
assert!(hash_bytes.len() >= mem::size_of::<u64>() * 2);
|
|
|
|
let hash_bytes: &[u64] = unsafe {
|
|
|
|
slice::from_raw_parts(hash_bytes.as_ptr() as *const u64, 2)
|
|
|
|
};
|
|
|
|
|
|
|
|
// The bytes returned bytes the Blake2B hasher are always little-endian.
|
|
|
|
Fingerprint(u64::from_le(hash_bytes[0]), u64::from_le(hash_bytes[1]))
|
2016-12-14 01:45:03 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-08 23:36:37 +02:00
|
|
|
|
|
|
|
impl<CTX> stable_hasher::HashStable<CTX> for Fingerprint {
|
2017-05-17 16:41:07 +02:00
|
|
|
#[inline]
|
2017-05-08 23:36:37 +02:00
|
|
|
fn hash_stable<W: stable_hasher::StableHasherResult>(&self,
|
|
|
|
_: &mut CTX,
|
|
|
|
hasher: &mut stable_hasher::StableHasher<W>) {
|
2017-05-17 16:41:07 +02:00
|
|
|
::std::hash::Hash::hash(self, hasher);
|
2017-05-08 23:36:37 +02:00
|
|
|
}
|
|
|
|
}
|