|
| 1 | +//! rustc encodes a lot of hashes. If hashes are stored as `u64` or `u128`, a `derive(Encodable)` |
| 2 | +//! will apply varint encoding to the hashes, which is less efficient than directly encoding the 8 |
| 3 | +//! or 16 bytes of the hash. |
| 4 | +//! |
| 5 | +//! The types in this module represent 64-bit or 128-bit hashes produced by a `StableHasher`. |
| 6 | +//! `Hash64` and `Hash128` expose some utilty functions to encourage users to not extract the inner |
| 7 | +//! hash value as an integer type and accidentally apply varint encoding to it. |
| 8 | +//! |
| 9 | +//! In contrast with `Fingerprint`, users of these types cannot and should not attempt to construct |
| 10 | +//! and decompose these types into constitutent pieces. The point of these types is only to |
| 11 | +//! connect the fact that they can only be produced by a `StableHasher` to their |
| 12 | +//! `Encode`/`Decode` impls. |
| 13 | +
|
| 14 | +use crate::stable_hasher::{StableHasher, StableHasherResult}; |
| 15 | +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; |
| 16 | +use std::fmt; |
| 17 | +use std::ops::BitXorAssign; |
| 18 | + |
| 19 | +#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 20 | +pub struct Hash64 { |
| 21 | + inner: u64, |
| 22 | +} |
| 23 | + |
| 24 | +impl Hash64 { |
| 25 | + pub const ZERO: Hash64 = Hash64 { inner: 0 }; |
| 26 | + |
| 27 | + #[inline] |
| 28 | + pub(crate) fn new(n: u64) -> Self { |
| 29 | + Self { inner: n } |
| 30 | + } |
| 31 | + |
| 32 | + #[inline] |
| 33 | + pub fn as_u64(self) -> u64 { |
| 34 | + self.inner |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl BitXorAssign<u64> for Hash64 { |
| 39 | + #[inline] |
| 40 | + fn bitxor_assign(&mut self, rhs: u64) { |
| 41 | + self.inner ^= rhs; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<S: Encoder> Encodable<S> for Hash64 { |
| 46 | + #[inline] |
| 47 | + fn encode(&self, s: &mut S) { |
| 48 | + s.emit_raw_bytes(&self.inner.to_le_bytes()); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<D: Decoder> Decodable<D> for Hash64 { |
| 53 | + #[inline] |
| 54 | + fn decode(d: &mut D) -> Self { |
| 55 | + Self { inner: u64::from_le_bytes(d.read_raw_bytes(8).try_into().unwrap()) } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl StableHasherResult for Hash64 { |
| 60 | + #[inline] |
| 61 | + fn finish(hasher: StableHasher) -> Self { |
| 62 | + Self { inner: hasher.finalize().0 } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl fmt::Debug for Hash64 { |
| 67 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 68 | + self.inner.fmt(f) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl fmt::LowerHex for Hash64 { |
| 73 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 74 | + fmt::LowerHex::fmt(&self.inner, f) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 79 | +pub struct Hash128 { |
| 80 | + inner: u128, |
| 81 | +} |
| 82 | + |
| 83 | +impl Hash128 { |
| 84 | + #[inline] |
| 85 | + pub fn truncate(self) -> Hash64 { |
| 86 | + Hash64 { inner: self.inner as u64 } |
| 87 | + } |
| 88 | + |
| 89 | + #[inline] |
| 90 | + pub fn wrapping_add(self, other: Self) -> Self { |
| 91 | + Self { inner: self.inner.wrapping_add(other.inner) } |
| 92 | + } |
| 93 | + |
| 94 | + #[inline] |
| 95 | + pub fn as_u128(self) -> u128 { |
| 96 | + self.inner |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl<S: Encoder> Encodable<S> for Hash128 { |
| 101 | + #[inline] |
| 102 | + fn encode(&self, s: &mut S) { |
| 103 | + s.emit_raw_bytes(&self.inner.to_le_bytes()); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl<D: Decoder> Decodable<D> for Hash128 { |
| 108 | + #[inline] |
| 109 | + fn decode(d: &mut D) -> Self { |
| 110 | + Self { inner: u128::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()) } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl StableHasherResult for Hash128 { |
| 115 | + #[inline] |
| 116 | + fn finish(hasher: StableHasher) -> Self { |
| 117 | + let (_0, _1) = hasher.finalize(); |
| 118 | + Self { inner: u128::from(_0) | (u128::from(_1) << 64) } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl fmt::Debug for Hash128 { |
| 123 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 124 | + self.inner.fmt(f) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl fmt::LowerHex for Hash128 { |
| 129 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 130 | + fmt::LowerHex::fmt(&self.inner, f) |
| 131 | + } |
| 132 | +} |
0 commit comments