Skip to main content

solverforge_scoring/stream/
key_extract.rs

1// KeyExtract trait and adapters for zero-erasure join key extraction.
2//
3// The `KeyExtract` trait replaces bare `Fn(&S, &A, usize) -> K` bounds,
4// allowing named adapter structs to be used as associated types in `JoinTarget`.
5
6use std::marker::PhantomData;
7
8// Extracts a join key from a solution and entity.
9//
10// Blanket impl for `Fn(&S, &A, usize) -> K` preserves all existing usage.
11// `EntityKeyAdapter` wraps entity-only key functions for the self-join case.
12pub trait KeyExtract<S, A, K>: Send + Sync {
13    // Extracts the key from the entity.
14    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
27// Wraps an entity-only key function `Fn(&A) -> K` as a `KeyExtract`.
28//
29// Used in the self-join case where the user passes `equal(|a: &A| key_fn(a))`.
30// The solution and index parameters are ignored.
31pub struct EntityKeyAdapter<KA> {
32    key_fn: KA,
33}
34
35impl<KA> EntityKeyAdapter<KA> {
36    // Creates a new `EntityKeyAdapter` from an entity-only key function.
37    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
62// Phantom type parameter usage to avoid requiring Clone/Send on D.
63pub struct PhantomKey<T>(PhantomData<fn() -> T>);