style: Make PrecomputedHasher fail loudly and with error messages when used incorrectly.

This commit is contained in:
Emilio Cobos Álvarez 2017-08-07 13:50:05 +02:00
parent 720b17a54b
commit 3d54c0710f
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

@ -25,12 +25,12 @@ use stylist::Rule;
/// A hasher implementation that doesn't hash anything, because it expects its /// A hasher implementation that doesn't hash anything, because it expects its
/// input to be a suitable u32 hash. /// input to be a suitable u32 hash.
pub struct PrecomputedHasher { pub struct PrecomputedHasher {
hash: u32, hash: Option<u32>,
} }
impl Default for PrecomputedHasher { impl Default for PrecomputedHasher {
fn default() -> Self { fn default() -> Self {
Self { hash: 0 } Self { hash: None }
} }
} }
@ -43,17 +43,19 @@ pub type PrecomputedHashSet<K> = HashSet<K, BuildHasherDefault<PrecomputedHasher
impl Hasher for PrecomputedHasher { impl Hasher for PrecomputedHasher {
#[inline] #[inline]
fn write(&mut self, _: &[u8]) { fn write(&mut self, _: &[u8]) {
unreachable!() unreachable!("Called into PrecomputedHasher with something that isn't \
a u32")
} }
#[inline] #[inline]
fn write_u32(&mut self, i: u32) { fn write_u32(&mut self, i: u32) {
self.hash = i; debug_assert!(self.hash.is_none());
self.hash = Some(i);
} }
#[inline] #[inline]
fn finish(&self) -> u64 { fn finish(&self) -> u64 {
self.hash as u64 self.hash.expect("PrecomputedHasher wasn't fed?") as u64
} }
} }