Skip to main content

solverforge_scoring/stream/
key_extract.rs

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