use std::borrow::Borrow;
use std::hash::Hash;
mod lfu;
mod lru;
mod lruk;
mod slab;
pub use lru::LruCache;
pub use lruk::LruKCache;
pub use lfu::LfuCache;
pub use slab::{Slab, Reinit};
#[derive(Clone)]
struct KeyRef<K> {
pub k: *const K,
}
impl<K> KeyRef<K> {
pub fn new(k: *const K) -> Self {
Self { k }
}
}
impl<K: Hash> Hash for KeyRef<K> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
unsafe {
(*self.k).hash(state);
}
}
}
impl<K: PartialEq> PartialEq for KeyRef<K> {
fn eq(&self, other: &Self) -> bool {
unsafe { (*self.k).eq(&*other.k) }
}
}
impl<K: Eq> Eq for KeyRef<K> {}
#[repr(transparent)]
struct KeyWrapper<Q: ?Sized>(Q);
impl<Q: ?Sized> KeyWrapper<Q> {
fn from_ref(key: &Q) -> &Self {
unsafe { &*(key as *const Q as *const KeyWrapper<Q>) }
}
}
impl<Q: ?Sized + Hash> Hash for KeyWrapper<Q> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
(self.0).hash(state);
}
}
impl<Q: ?Sized + PartialEq> PartialEq for KeyWrapper<Q> {
fn eq(&self, other: &Self) -> bool {
(self.0).eq(&other.0)
}
}
impl<Q: ?Sized + Eq> Eq for KeyWrapper<Q> {}
impl<K, Q> Borrow<KeyWrapper<Q>> for KeyRef<K>
where
K: Borrow<Q>,
Q: ?Sized,
{
fn borrow(&self) -> &KeyWrapper<Q> {
let key = unsafe { &*self.k }.borrow();
KeyWrapper::from_ref(key)
}
}