use std::hash::{DefaultHasher, Hash, Hasher};
use rustolio_utils::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Decode, Encode)]
pub struct Key {
hash: u64,
ty: KeyType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Decode, Encode)]
pub enum KeyType {
ReadWrite,
ReadSecureWrite,
SecureReadWrite,
}
impl Key {
pub fn from_value(value: &impl std::hash::Hash, ty: KeyType) -> Self {
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
ty.hash(&mut hasher);
let hash = hasher.finish();
Self { hash, ty }
}
pub fn hash(&self) -> u64 {
self.hash
}
pub fn ty(&self) -> KeyType {
self.ty
}
}
impl std::hash::Hash for Key {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.hash);
}
}
pub type KeyState = std::hash::BuildHasherDefault<KeyHasher>;
#[derive(Debug, Default)]
pub struct KeyHasher {
state: u64,
}
impl Hasher for KeyHasher {
fn finish(&self) -> u64 {
self.state
}
fn write(&mut self, _: &[u8]) {
unreachable!("write_u64 should always be used");
}
fn write_u64(&mut self, i: u64) {
self.state = i;
}
}