use std::any::Any;
use std::fmt::{self, Debug};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::Ordering;
use portable_atomic::AtomicU128;
use siphasher::sip128::{Hasher128, SipHasher13};
#[derive(Clone)]
pub struct LazyHash<T: ?Sized> {
hash: HashLock,
value: T,
}
impl<T: Default> Default for LazyHash<T> {
#[inline]
fn default() -> Self {
Self::new(Default::default())
}
}
impl<T> LazyHash<T> {
#[inline]
pub fn new(value: T) -> Self {
Self { hash: HashLock::new(), value }
}
#[inline]
pub fn into_inner(self) -> T {
self.value
}
}
impl<T: Hash + ?Sized + 'static> LazyHash<T> {
#[inline]
fn load_or_compute_hash(&self) -> u128 {
self.hash.get_or_insert_with(|| hash_item(&self.value))
}
}
#[inline]
fn hash_item<T: Hash + ?Sized + 'static>(item: &T) -> u128 {
let mut state = SipHasher13::new();
item.type_id().hash(&mut state);
item.hash(&mut state);
state.finish128().as_u128()
}
impl<T: Hash + ?Sized + 'static> Hash for LazyHash<T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u128(self.load_or_compute_hash());
}
}
impl<T> From<T> for LazyHash<T> {
#[inline]
fn from(value: T) -> Self {
Self::new(value)
}
}
impl<T: Hash + ?Sized + 'static> Eq for LazyHash<T> {}
impl<T: Hash + ?Sized + 'static> PartialEq for LazyHash<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.load_or_compute_hash() == other.load_or_compute_hash()
}
}
impl<T: ?Sized> Deref for LazyHash<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T: ?Sized + 'static> DerefMut for LazyHash<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.hash.reset();
&mut self.value
}
}
impl<T: Debug> Debug for LazyHash<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}
#[derive(Clone)]
pub struct ManuallyHash<T: ?Sized> {
hash: u128,
value: T,
}
impl<T> ManuallyHash<T> {
#[inline]
pub fn new(value: T, hash: u128) -> Self {
Self { hash, value }
}
#[inline]
pub fn into_inner(self) -> T {
self.value
}
}
impl<T: ?Sized> Hash for ManuallyHash<T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u128(self.hash);
}
}
impl<T: ?Sized> Eq for ManuallyHash<T> {}
impl<T: ?Sized> PartialEq for ManuallyHash<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
}
impl<T: ?Sized> Deref for ManuallyHash<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T: Debug> Debug for ManuallyHash<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}
pub struct HashLock(AtomicU128);
impl HashLock {
pub const fn new() -> Self {
Self(AtomicU128::new(0))
}
#[inline]
pub fn get_or_insert_with(&self, f: impl FnOnce() -> u128) -> u128 {
let mut hash = self.get();
if hash == 0 {
hash = f();
self.0.store(hash, Ordering::Relaxed);
}
hash
}
#[inline]
pub fn reset(&mut self) {
*self.0.get_mut() = 0;
}
#[inline]
fn get(&self) -> u128 {
self.0.load(Ordering::Relaxed)
}
}
impl Default for HashLock {
fn default() -> Self {
Self::new()
}
}
impl Clone for HashLock {
fn clone(&self) -> Self {
Self(AtomicU128::new(self.get()))
}
}