Skip to main content

solverforge_scoring/stream/relational/
row.rs

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