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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! High-performance graph-based stream processing runtime.
//!
//! The `runtime` module provides the core execution engine for wavelet's cooperative
//! stream processing model. Built around a computation graph where nodes represent
//! stream processors and edges define data dependencies, the runtime delivers
//! deterministic, low-latency execution without the overhead of async runtimes
//! or actor systems.
//!
//! # Architecture Overview
//!
//! ## Computation Model
//! - **Nodes**: Stateful stream processors that transform data
//! - **Relationships**: Define when nodes should execute (`Trigger` vs `Observe`)
//! - **Cooperative scheduling**: Nodes voluntarily yield control after processing
//! - **Dependency ordering**: Execution follows graph topology (depth-first, scheduled first)
//! - **Incremental computation**: Only recompute when dependencies actually change
//!
//! ## Core Components
//!
//! ### [`Executor`]
//! The central execution engine that orchestrates:
//! - Graph topology management and node lifecycle
//! - Event-driven scheduling (I/O, timers, yields)
//! - Dependency-ordered execution cycles
//! - Garbage collection and resource cleanup
//!
//! ### [`Node<T>`] and [`NodeBuilder<T>`]
//! Type-safe containers for node state with controlled mutation:
//! - **Data-oriented design**: Separate data (`T`) from behavior (cycle functions)
//! - **Controlled mutation**: Data changes only within cycle functions
//! - **Builder pattern**: Fluent API for configuring relationships and lifecycle
//!
//! ### [`Runtime<C>`]
//! Complete runtime orchestration combining:
//! - **Clock abstraction**: Consistent time across execution cycles
//! - **Execution modes**: Different CPU/latency trade-offs (`Spin`, `Park`)
//! - **Runtime loops**: Automated execution patterns for different use cases
//!
//! ### Event System
//! Unified event handling for external stimulus:
//! - **I/O Events**: Network sockets, file handles, external notifications
//! - **Timer Events**: Time-based scheduling with precise expiration
//! - **Yield Events**: Immediate re-scheduling for continued processing
//!
//! # Design Principles
//!
//! ## Single-Threaded Cooperative Model
//! - **Predictable performance**: No hidden thread spawning or context switching
//! - **Deterministic execution**: Same inputs always produce the same execution order
//! - **Zero-cost abstractions**: Direct function calls without async overhead
//! - **Resource control**: Explicit management of CPU, memory, and I/O
//!
//! ## Data-Oriented Design
//! - **Type safety**: Compile-time guarantees about node data types
//! - **Memory efficiency**: Minimal indirection and cache-friendly layouts
//! - **Controlled mutation**: Runtime coordinates when and how data changes
//! - **Clear ownership**: Data lifecycle tied to node lifecycle
//!
//! ## Event-Driven Execution
//! - **External integration**: Clean interfaces to operating system events
//! - **Backpressure handling**: Natural flow control through graph topology
//! - **Resource efficiency**: Sleep when no work is available
//! - **Low latency**: Direct event dispatch without queueing overhead
//!
//! # Usage Patterns
//!
//! ## Basic Stream Processing
//! ```rust, ignore
//! use wavelet::runtime::*;
//!
//! let mut executor = Executor::new();
//!
//! // Create data source
//! let data_source = NodeBuilder::new(DataSource::new())
//! .on_init(|executor, _, idx| {
//! executor.yield_driver().yield_now(idx); // Start processing
//! })
//! .build(&mut executor, |source, ctx| {
//! if let Some(data) = source.poll_data() {
//! source.latest = data;
//! Control::Broadcast // Notify downstream
//! } else {
//! Control::Unchanged
//! }
//! });
//!
//! // Create processor that reacts to data
//! let processor = NodeBuilder::new(Processor::new())
//! .triggered_by(&data_source)
//! .build(&mut executor, |proc, ctx| {
//! proc.process_data();
//! Control::Unchanged
//! });
//!
//! // Run the graph
//! let runtime = RealtimeRuntime::new(ExecutionMode::Park);
//! runtime.run_forever();
//! ```
//!
//! ## I/O Integration
//! ```rust, ignore
//! let (network_node, notifier) = NodeBuilder::new(NetworkHandler::new())
//! .build_with_notifier(&mut executor, |handler, ctx| {
//! match handler.socket.try_read(&mut handler.buffer) {
//! Ok(0) => Control::Sweep, // Connection closed
//! Ok(n) => {
//! handler.process_bytes(n);
//! Control::Broadcast
//! }
//! Err(e) if e.kind() == ErrorKind::WouldBlock => {
//! // Re-register for readiness
//! handler.reregister_interest(ctx);
//! Control::Unchanged
//! }
//! Err(_) => Control::Sweep, // Connection error
//! }
//! })?;
//!
//! // External thread can wake the network node
//! notifier.notify()?;
//! ```
//!
//! ## Dynamic Graph Construction
//! ```rust, ignore
//! let spawner = NodeBuilder::new(DynamicSpawner::new())
//! .build(&mut executor, |spawner, ctx| {
//! if spawner.should_create_worker() {
//! ctx.spawn_subgraph(|executor| {
//! let worker = NodeBuilder::new(Worker::new())
//! .triggered_by(&spawner.work_queue)
//! .build(executor, process_work);
//! });
//! }
//! Control::Unchanged
//! });
//! ```
//!
//! # Performance Characteristics
//!
//! - **Latency**: Sub-microsecond node execution overhead
//! - **Throughput**: Millions of events per second on modern hardware
//! - **Memory**: Predictable allocation patterns, minimal runtime overhead
//! - **CPU**: Efficient utilization with configurable sleep/spin strategies
//! - **Determinism**: Consistent performance across runs with same inputs
//!
//! # Target Applications
//!
//! The runtime excels in domains requiring:
//! - **Financial systems**: Low-latency trading, risk management, market data
//! - **Real-time analytics**: Live dashboards, alerting, stream aggregation
//! - **IoT processing**: Sensor data, device management, edge computing
//! - **Protocol handling**: Stateful network protocols, message parsing
//! - **Media processing**: Audio/video pipelines, real-time effects
//!
//! For request/response workloads or applications requiring automatic parallelism,
//! consider using async runtimes like Tokio alongside wavelet for the appropriate
//! components of your system.
use ;
pub use *;
use EnumAsInner;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
/// Execution mode for the real-time runtime.
///
/// Spin mode never parks the thread. Trades off
/// high cpu usage for the lowest possible latency.
///
/// Park mode will park the thread if no event
/// is available at the time of the poll. This
/// trades off latency for energy usage.
pub type TestRuntime = ;
pub type RealtimeRuntime = ;
pub type HistoricalRuntime = ;
/// A complete runtime instance that combines executor, clock, and execution mode.
///
/// The `Runtime` orchestrates the execution loop by:
/// - Using the clock to provide consistent time snapshots
/// - Running the executor with the configured execution mode
/// - Handling different clock types (Precision, Historical, Test) appropriately
///
/// The type parameters ensure compile-time guarantees about clock and mode
/// compatibility.