use std::fmt::Debug;
use std::hash::Hash;
use allocative::Allocative;
use dupe::Dupe;
#[cfg(feature = "pagable_dep")]
use pagable::Pagable;
use serde::Deserialize;
use serde::Serialize;
use crate::hasher::StarlarkHasher;
use crate::mix_u32::mix_u32;
#[derive(
Clone,
Copy,
Dupe,
PartialEq,
Eq,
Hash,
Debug,
Allocative,
Serialize,
Deserialize
)]
#[cfg_attr(feature = "pagable_dep", derive(Pagable))]
pub struct StarlarkHashValue(u32);
impl StarlarkHashValue {
#[inline]
pub fn new<K: Hash + ?Sized>(key: &K) -> Self {
let mut hasher = StarlarkHasher::default();
key.hash(&mut hasher);
hasher.finish_small()
}
#[inline]
pub const fn new_unchecked(hash: u32) -> Self {
StarlarkHashValue(hash)
}
#[inline]
pub const fn hash_64(h: u64) -> Self {
let h = h ^ (h >> 33);
let h = h.wrapping_mul(0xff51afd7ed558ccd);
let h = h ^ (h >> 33);
let h = h.wrapping_mul(0xc4ceb9fe1a85ec53);
let h = h ^ (h >> 33);
StarlarkHashValue::new_unchecked(h as u32)
}
#[inline]
pub const fn get(self) -> u32 {
self.0
}
#[inline]
pub fn promote(self) -> u64 {
mix_u32(self.0)
}
}