|
| 1 | +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; |
| 2 | +use std::fmt; |
| 3 | +use std::ops::BitXorAssign; |
| 4 | +use crate::stable_hasher::{StableHasher, StableHasherResult}; |
| 5 | + |
| 6 | +#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 7 | +pub struct Hash64 { |
| 8 | + inner: u64, |
| 9 | +} |
| 10 | + |
| 11 | +impl Hash64 { |
| 12 | + pub const ZERO: Hash64 = Hash64 { inner: 0 }; |
| 13 | + |
| 14 | + #[inline] |
| 15 | + pub(crate) fn new(n: u64) -> Self { |
| 16 | + Self { inner: n } |
| 17 | + } |
| 18 | + |
| 19 | + #[inline] |
| 20 | + pub fn as_u64(self) -> u64 { |
| 21 | + self.inner |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl BitXorAssign<u64> for Hash64 { |
| 26 | + fn bitxor_assign(&mut self, rhs: u64) { |
| 27 | + self.inner ^= rhs; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl<S: Encoder> Encodable<S> for Hash64 { |
| 32 | + #[inline] |
| 33 | + fn encode(&self, s: &mut S) { |
| 34 | + s.emit_raw_bytes(&self.inner.to_le_bytes()); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<D: Decoder> Decodable<D> for Hash64 { |
| 39 | + #[inline] |
| 40 | + fn decode(d: &mut D) -> Self { |
| 41 | + Self { inner: u64::from_le_bytes(d.read_raw_bytes(8).try_into().unwrap()) } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl StableHasherResult for Hash64 { |
| 46 | + #[inline] |
| 47 | + fn finish(hasher: StableHasher) -> Self { |
| 48 | + Self { inner: hasher.finalize().0 } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl fmt::Debug for Hash64 { |
| 53 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 54 | + self.inner.fmt(f) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl fmt::LowerHex for Hash64 { |
| 59 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 60 | + fmt::LowerHex::fmt(&self.inner, f) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 65 | +pub struct Hash128 { |
| 66 | + inner: u128, |
| 67 | +} |
| 68 | + |
| 69 | +impl Hash128 { |
| 70 | + #[inline] |
| 71 | + pub fn truncate(self) -> Hash64 { |
| 72 | + Hash64 { inner: self.inner as u64 } |
| 73 | + } |
| 74 | + |
| 75 | + #[inline] |
| 76 | + pub fn wrapping_add(self, other: Self) -> Self { |
| 77 | + Self { |
| 78 | + inner: self.inner.wrapping_add(other.inner), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + #[inline] |
| 83 | + pub fn as_u128(self) -> u128 { |
| 84 | + self.inner |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<S: Encoder> Encodable<S> for Hash128 { |
| 89 | + #[inline] |
| 90 | + fn encode(&self, s: &mut S) { |
| 91 | + s.emit_raw_bytes(&self.inner.to_le_bytes()); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<D: Decoder> Decodable<D> for Hash128 { |
| 96 | + #[inline] |
| 97 | + fn decode(d: &mut D) -> Self { |
| 98 | + Self { inner: u128::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()) } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl StableHasherResult for Hash128 { |
| 103 | + #[inline] |
| 104 | + fn finish(hasher: StableHasher) -> Self { |
| 105 | + let (_0, _1) = hasher.finalize(); |
| 106 | + Self { inner: u128::from(_0) | (u128::from(_1) << 64) } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl fmt::Debug for Hash128 { |
| 111 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 112 | + self.inner.fmt(f) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl fmt::LowerHex for Hash128 { |
| 117 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 118 | + fmt::LowerHex::fmt(&self.inner, f) |
| 119 | + } |
| 120 | +} |
0 commit comments