1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! [`Cacheable`] trait + [`Field`] accessor — the identity contract for
//! entries stored in a `Punnu`.
//!
//! A `Cacheable` type names its identity column (`Self::Id`), declares
//! a generated companion struct of [`Field`] accessors (`Self::Fields`),
//! and exposes both a deterministic `id()` extractor and a `fields()`
//! constructor that wires every accessor to its real extractor.
//!
//! The companion struct is normally produced by `#[derive(Cacheable)]`
//! (see `sassi-macros`), but hand impls are supported when the macro
//! doesn't fit. Both `id()` and `fields()` are required trait methods
//! so generic code like `PunnuScope<T: Cacheable>` can call `T::fields()`
//! without knowing the concrete type.
//!
//! Correct use starts with explicit identity boundaries: view/projection
//! types should get their own `Punnu<Projection>` instances, collection
//! aggregates should use wrapper structs rather than sentinel values, and
//! tenant/substrate identity should live in the type, id, wrapper, fetcher, or
//! caller-owned pool selection logic that actually enforces that boundary.
//!
//! When a `Punnu<T>` has an L2 backend, `T::cache_type_name()` contributes to
//! the backend keyspace. Use `#[cacheable(type_name = "...")]` or a manual
//! override for long-lived/shared L2 data so backend keys do not change when
//! Rust modules are renamed. The derive default is for local caches, examples,
//! and tests; it is not a durable schema identifier.
use Hash;
use PhantomData;
/// Identity contract for entries stored in a `Punnu`.
///
/// Caches the **canonical shape** of `T`. Projection or view types should get
/// their own independent `Punnu<ProjectionT>` instance so the type system keeps
/// them disjoint at compile time. The identity-map invariant ("one `id()` → one
/// cached entry") then holds per `Punnu` because the type signature fixes the
/// shape.
///
/// `cache_type_name()` is part of the L2 backend keyspace. Derived types can set
/// it with `#[cacheable(type_name = "myapp.User")]`; hand impls can override the
/// method directly. Treat explicit names as durable schema identifiers: they
/// should be unique inside a namespace and reused only for wire-compatible
/// payloads keyed by the same ids.
/// Field accessor.
///
/// Carries both the column / serde-key name (used by downstream
/// SQL-emitting consumers like djogi) and the in-memory extractor used
/// by sassi's predicate evaluator. The two halves let the same
/// `Field<T, V>` value participate in a SQL `WHERE` emit on a backend
/// AND a Rust-side `evaluate()` walk on a frontend, without diverging.
///
/// Internal storage is `pub(crate)` to keep the (name, extractor) pair
/// stable post-construction. Use [`Field::name`] and [`Field::extract`]
/// accessors instead of direct field access.
///
/// # Example
///
/// ```
/// use sassi::Field;
///
/// struct User { id: i64, age: u32 }
///
/// // Normally generated by `#[derive(Cacheable)]`; shown here for
/// // documentation:
/// let age_field: Field<User, u32> = Field::new("age", |u| &u.age);
/// let alice = User { id: 1, age: 30 };
/// assert_eq!(*age_field.extract(&alice), 30);
/// assert_eq!(age_field.name(), "age");
/// ```
// Manual `Copy` / `Clone` rather than derive: the derived bounds would
// require `T: Copy` / `V: Copy` (because of the `PhantomData<(T, V)>`
// field), which we don't want — `Field<T, V>` should be `Copy` for any
// `T`, `V` since all of its real-data fields (`&'static str`, function
// pointer, `PhantomData`) are `Copy` regardless of `T` / `V`.
// `Default` for `Field<T, V>` — produces a no-op accessor. Used by the
// `Default` impl on the generated `Fields` companion struct so callers
// can construct it via `T::Fields::default()`. Real wiring happens via
// the `T::fields()` trait method.