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
/* Collector traits for grouping and aggregating stream matches.
Collectors aggregate stream matches within groups during `group_by()` operations.
They maintain incremental state for insert/retract operations.
*/
/* A collector that aggregates stream inputs into a result of type `R`.
Collectors are used in `group_by()` operations to reduce groups of stream
matches into summary values. `Input` is the borrowed match shape, such as `&A`
for unary streams and `(&A, &B)` for cross-join streams.
# Zero-Erasure Design
The collector owns any mapping functions and provides `extract()` to convert
stream matches to owned values. The accumulator owns retained values and returns
lightweight retraction tokens, avoiding copied or cloned collector payloads in
grouped state.
# Incremental Protocol
Collectors support incremental updates:
1. `create_accumulator()` creates a fresh accumulator
2. `extract(input)` converts a stream match to accumulator value
3. `accumulate(value)` moves value into accumulator and returns a retraction token
4. `retract(token)` removes the retained value represented by that token
5. `with_result()` exposes the current result without materializing an owned clone
This enables incremental score updates when stream matches are added/removed from groups.
*/
/* An accumulator that incrementally collects values.
Values are extracted by the collector's `extract()` method before being moved
into the accumulator.
*/