bingus-blog/src/hash_arc_store.rs

38 lines
906 B
Rust
Raw Normal View History

2024-04-18 04:05:38 +03:00
use std::hash::{DefaultHasher, Hash, Hasher};
use std::marker::PhantomData;
use std::sync::Arc;
pub struct HashArcStore<T, Lookup>
where
Lookup: Hash,
{
inner: Option<Arc<T>>,
hash: Option<u64>,
_phantom: PhantomData<Lookup>,
}
impl<T, Lookup> HashArcStore<T, Lookup>
where
Lookup: Hash,
{
pub fn new() -> Self {
Self {
inner: None,
hash: None,
_phantom: PhantomData,
}
}
pub fn get_or_init(&mut self, key: &Lookup, init: impl Fn(&Lookup) -> Arc<T>) -> Arc<T> {
let mut h = DefaultHasher::new();
key.hash(&mut h);
let hash = h.finish();
if !self.hash.is_some_and(|inner_hash| inner_hash == hash) {
self.inner = Some(init(key));
2024-04-21 00:23:43 +03:00
self.hash = Some(hash);
2024-04-18 04:05:38 +03:00
}
// safety: please.
unsafe { self.inner.as_ref().unwrap_unchecked().clone() }
}
}