1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

don't panic in nullhasher misuse

This commit is contained in:
Robin Appelman 2024-04-07 16:42:36 +02:00
commit 9d38663c79

View file

@ -1,9 +1,10 @@
use fnv::FnvHasher;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::hash::{BuildHasher, Hasher}; use std::hash::{BuildHasher, Hasher};
/// A dummy hasher that maps simply returns the hashed u64 /// A dummy hasher that maps simply returns the hashed u64
/// ///
/// trying to hash anything but a u64 will result in a panic /// trying to hash anything but a u64 will result in using fnvhash
pub struct NullHasher { pub struct NullHasher {
data: u64, data: u64,
} }
@ -15,8 +16,14 @@ impl Hasher for NullHasher {
} }
#[inline] #[inline]
fn write(&mut self, _msg: &[u8]) { fn write(&mut self, msg: &[u8]) {
panic!("can only hash u64,u32,u16"); let mut hasher = FnvHasher::default();
hasher.write(msg);
self.data = hasher.finish();
}
#[inline]
fn write_u8(&mut self, data: u8) {
self.data = data as u64
} }
#[inline] #[inline]