use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Debug, Default)]
pub struct ContentHash(AtomicU64);
impl ContentHash {
pub fn get_or_compute(&self, compute: impl FnOnce() -> u64) -> u64 {
let cached = self.0.load(Ordering::Relaxed);
if cached != 0 {
return cached;
}
let computed = compute();
let to_store = if computed == 0 { 1 } else { computed };
self.0.store(to_store, Ordering::Relaxed);
to_store
}
}
impl Clone for ContentHash {
fn clone(&self) -> Self {
ContentHash(AtomicU64::new(0))
}
}
impl PartialEq for ContentHash {
fn eq(&self, _other: &Self) -> bool {
true
}
}