stash: deduplicate Fnv1aHasher; add derive for u64 wrapper

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ic2886815721f6eefc66a8ddacd44fb286a6a6964
This commit is contained in:
raf 2026-04-01 14:38:47 +03:00
commit d643376cd7
4 changed files with 108 additions and 58 deletions

View file

@ -1,4 +1,4 @@
use std::{collections::BinaryHeap, io::Read, time::Duration};
use std::{collections::BinaryHeap, hash::Hasher, io::Read, time::Duration};
use smol::Timer;
use wl_clipboard_rs::{
@ -15,36 +15,9 @@ use wl_clipboard_rs::{
use crate::{
clipboard::{self, ClipboardData, get_serving_pid},
db::{SqliteClipboardDb, nonblocking::AsyncClipboardDb},
hash::Fnv1aHasher,
};
/// FNV-1a hasher for deterministic hashing across process runs.
/// Unlike `DefaultHasher` (`SipHash`), this produces stable hashes.
struct Fnv1aHasher {
state: u64,
}
impl Fnv1aHasher {
const FNV_OFFSET: u64 = 0xCBF29CE484222325;
const FNV_PRIME: u64 = 0x100000001B3;
fn new() -> Self {
Self {
state: Self::FNV_OFFSET,
}
}
fn write(&mut self, bytes: &[u8]) {
for byte in bytes {
self.state ^= u64::from(*byte);
self.state = self.state.wrapping_mul(Self::FNV_PRIME);
}
}
fn finish(&self) -> u64 {
self.state
}
}
/// Wrapper to provide [`Ord`] implementation for `f64` by negating values.
/// This allows [`BinaryHeap`], which is a max-heap, to function as a min-heap.
/// Also see: