solverforge_scoring/stream/
key_extract.rs1use std::marker::PhantomData;
7
8pub trait KeyExtract<S, A, K>: Send + Sync {
13 fn extract(&self, s: &S, a: &A, idx: usize) -> K;
15}
16
17impl<S, A, K, F> KeyExtract<S, A, K> for F
18where
19 F: Fn(&S, &A, usize) -> K + Send + Sync,
20{
21 #[inline]
22 fn extract(&self, s: &S, a: &A, idx: usize) -> K {
23 self(s, a, idx)
24 }
25}
26
27pub struct EntityKeyAdapter<KA> {
32 key_fn: KA,
33}
34
35impl<KA> EntityKeyAdapter<KA> {
36 pub fn new(key_fn: KA) -> Self {
38 Self { key_fn }
39 }
40}
41
42impl<S, A, K, KA> KeyExtract<S, A, K> for EntityKeyAdapter<KA>
43where
44 KA: Fn(&A) -> K + Send + Sync,
45{
46 #[inline]
47 fn extract(&self, _s: &S, a: &A, _idx: usize) -> K {
48 (self.key_fn)(a)
49 }
50}
51
52impl<KA: Clone> Clone for EntityKeyAdapter<KA> {
53 fn clone(&self) -> Self {
54 Self {
55 key_fn: self.key_fn.clone(),
56 }
57 }
58}
59
60impl<KA: Copy> Copy for EntityKeyAdapter<KA> {}
61
62pub struct PhantomKey<T>(PhantomData<fn() -> T>);