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