Skip to main content

solverforge_scoring/stream/relational/
row.rs

1/* Borrowed row shapes for relational operators.
2
3A row is either a leaf borrowing one entity or a concatenation of a prior
4row with a new entity reference. Concatenation is structural — no arity
5ceiling, no erased values — so a later join's key closure can inspect any
6earlier binding. Rows borrow; they never own entities and never hold
7solution borrows between callbacks.
8*/
9
10/* A borrowed view of one source entity.
11
12The lifetime is the traversal borrow, not a stored solution reference.
13`index` carries the semantic source slice index alongside the entity so
14low-level filters keep exact index semantics without storage-ID leakage.
15*/
16#[derive(Debug, PartialEq, Eq)]
17pub struct Leaf<'r, T> {
18    pub entity: &'r T,
19    pub index: usize,
20}
21
22impl<'r, T> Leaf<'r, T> {
23    pub fn new(entity: &'r T, index: usize) -> Leaf<'r, T> {
24        Leaf { entity, index }
25    }
26}
27
28impl<T> Copy for Leaf<'_, T> {}
29impl<T> Clone for Leaf<'_, T> {
30    fn clone(&self) -> Self {
31        *self
32    }
33}
34
35/* Recursive concatenation of a prior row with a new entity reference.
36
37A later join's left key closure receives the whole prior row in this
38shape, so it can inspect any earlier binding or combine several. Binding
39order walks the left spine first, matching authored tuple orientation.
40*/
41#[derive(Clone, Debug, PartialEq, Eq)]
42pub struct Concat<'r, Left, T> {
43    pub left: Left,
44    pub right: Leaf<'r, T>,
45}
46
47impl<'r, Left: Copy, T: Copy> Copy for Concat<'r, Left, T> {}
48
49impl<'r, Left, T> Concat<'r, Left, T> {
50    pub fn new(left: Left, entity: &'r T, index: usize) -> Concat<'r, Left, T> {
51        Concat {
52            left,
53            right: Leaf::new(entity, index),
54        }
55    }
56}
57
58/* Row depth as a compile-time property of the nesting.
59
60Depth keeps operator recursion monomorphized without runtime tags.
61`DEPTH` is read by row-shape assertions at operator construction sites.
62*/
63#[allow(dead_code)]
64pub trait Row {
65    // Number of bound entities in this row (leaf = 1).
66    const DEPTH: usize;
67}
68
69impl<'r, T> Row for Leaf<'r, T> {
70    const DEPTH: usize = 1;
71}
72
73impl<'r, Left: Row, T> Row for Concat<'r, Left, T> {
74    const DEPTH: usize = Left::DEPTH + 1;
75}