use std::marker::PhantomData;
pub trait KeyExtract<S, A, K>: Send + Sync {
fn extract(&self, s: &S, a: &A, idx: usize) -> K;
}
impl<S, A, K, F> KeyExtract<S, A, K> for F
where
F: Fn(&S, &A, usize) -> K + Send + Sync,
{
#[inline]
fn extract(&self, s: &S, a: &A, idx: usize) -> K {
self(s, a, idx)
}
}
pub struct EntityKeyAdapter<KA> {
key_fn: KA,
}
impl<KA> EntityKeyAdapter<KA> {
pub fn new(key_fn: KA) -> Self {
Self { key_fn }
}
}
impl<S, A, K, KA> KeyExtract<S, A, K> for EntityKeyAdapter<KA>
where
KA: Fn(&A) -> K + Send + Sync,
{
#[inline]
fn extract(&self, _s: &S, a: &A, _idx: usize) -> K {
(self.key_fn)(a)
}
}
impl<KA: Clone> Clone for EntityKeyAdapter<KA> {
fn clone(&self) -> Self {
Self {
key_fn: self.key_fn.clone(),
}
}
}
impl<KA: Copy> Copy for EntityKeyAdapter<KA> {}
pub struct PhantomKey<T>(PhantomData<fn() -> T>);