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
//! User-facing execution context passed to task and bridge process callbacks.
use core::ops::Deref;
use cu29_clock::{RobotClock, RobotClockMock};
/// Execution context passed to task and bridge callbacks.
///
/// `CuContext` provides callback code with:
/// - time access through `clock` and `Deref<Target = RobotClock>`
/// - current execution sequence id via `cl_id()`
/// - process instance metadata via `instance_id()`
/// - compile-time subsystem identity via `subsystem_code()`
/// - current component metadata via `current_component_id()`
/// - current task metadata via `task_id()` / `task_index()`
///
/// The execution sequence id matches the copper-list id of the iteration being
/// processed. It is also available in other lifecycle callbacks
/// (`start`/`preprocess`/`postprocess`/`stop`) for continuity, but outside
/// `process` callbacks it must not be treated as a live copper-list handle.
///
/// The runtime creates one context per execution loop and updates transient
/// fields such as the currently executing component/task before each callback.
#[derive(Clone, Debug)]
pub struct CuContext {
/// Runtime clock. Kept as a field for direct access (`context.clock.now()`).
pub clock: RobotClock,
cl_id: u64,
instance_id: u32,
subsystem_code: u16,
task_ids: &'static [&'static str],
current_component_index: Option<usize>,
current_task_index: Option<usize>,
}
impl CuContext {
/// Starts a context builder from a clock.
pub fn builder(clock: RobotClock) -> CuContextBuilder {
CuContextBuilder {
clock,
cl_id: 0,
instance_id: 0,
subsystem_code: 0,
task_ids: &[],
}
}
/// Creates a context from an existing clock with default metadata.
///
/// Defaults:
/// - `cl_id = 0`
/// - no task id table
pub fn from_clock(clock: RobotClock) -> Self {
Self::builder(clock).build()
}
/// Creates a context backed by a real robot clock.
///
/// Defaults:
/// - `cl_id = 0`
/// - no task id table
#[cfg(feature = "std")]
pub fn new_with_clock() -> Self {
Self::from_clock(RobotClock::new())
}
/// Creates a context backed by a mock clock.
///
/// Returns both the context and its [`RobotClockMock`] control handle.
pub fn new_mock_clock() -> (Self, RobotClockMock) {
let (clock, mock) = RobotClock::mock();
(Self::from_clock(clock), mock)
}
/// Internal constructor used by runtime internals and code generation.
pub(crate) fn new(
clock: RobotClock,
clid: u64,
instance_id: u32,
subsystem_code: u16,
task_ids: &'static [&'static str],
) -> Self {
Self {
clock,
cl_id: clid,
instance_id,
subsystem_code,
task_ids,
current_component_index: None,
current_task_index: None,
}
}
/// Internal constructor used by generated runtime code.
#[doc(hidden)]
pub fn from_runtime_metadata(
clock: RobotClock,
clid: u64,
instance_id: u32,
subsystem_code: u16,
task_ids: &'static [&'static str],
) -> Self {
Self::new(clock, clid, instance_id, subsystem_code, task_ids)
}
/// Sets the currently executing component index.
pub fn set_current_component(&mut self, component_index: usize) {
self.current_component_index = Some(component_index);
}
/// Clears the currently executing component.
pub fn clear_current_component(&mut self) {
self.current_component_index = None;
}
/// Sets the currently executing task index.
pub fn set_current_task(&mut self, task_index: usize) {
self.current_component_index = Some(task_index);
self.current_task_index = Some(task_index);
}
/// Clears the currently executing task.
pub fn clear_current_task(&mut self) {
self.current_task_index = None;
}
/// Returns the current execution sequence id.
///
/// In `process` callbacks, this value is the id of the copper-list being
/// processed. In other lifecycle callbacks, this value is still meaningful
/// for sequencing but does not imply that a copper-list instance is alive.
pub fn cl_id(&self) -> u64 {
self.cl_id
}
/// Returns the runtime instance id attached to this context.
pub fn instance_id(&self) -> u32 {
self.instance_id
}
/// Returns the compile-time subsystem code for this Copper process.
pub fn subsystem_code(&self) -> u16 {
self.subsystem_code
}
/// Returns the current component index, if any.
pub fn current_component_id(&self) -> Option<usize> {
self.current_component_index
}
/// Returns the current task index, if any.
pub fn task_index(&self) -> Option<usize> {
self.current_task_index
}
/// Returns the current task id, if any.
pub fn task_id(&self) -> Option<&'static str> {
self.current_task_index
.and_then(|idx| self.task_ids.get(idx).copied())
}
#[cfg(feature = "std")]
pub(crate) fn with_cl_id(&self, cl_id: u64) -> Self {
let mut context = self.clone();
context.cl_id = cl_id;
context
}
}
/// Builder for [`CuContext`].
#[derive(Clone, Debug)]
pub struct CuContextBuilder {
clock: RobotClock,
cl_id: u64,
instance_id: u32,
subsystem_code: u16,
task_ids: &'static [&'static str],
}
impl CuContextBuilder {
/// Sets the copper-list id for the context.
pub fn cl_id(mut self, cl_id: u64) -> Self {
self.cl_id = cl_id;
self
}
/// Sets the runtime instance id carried by the context.
pub fn instance_id(mut self, instance_id: u32) -> Self {
self.instance_id = instance_id;
self
}
/// Sets the static task id table for task metadata access.
pub fn task_ids(mut self, task_ids: &'static [&'static str]) -> Self {
self.task_ids = task_ids;
self
}
/// Builds a context value.
pub fn build(self) -> CuContext {
CuContext::new(
self.clock,
self.cl_id,
self.instance_id,
self.subsystem_code,
self.task_ids,
)
}
}
impl Deref for CuContext {
type Target = RobotClock;
fn deref(&self) -> &Self::Target {
&self.clock
}
}
#[cfg(test)]
mod tests {
use super::CuContext;
use cu29_clock::RobotClock;
#[test]
fn default_instance_id_is_zero() {
let ctx = CuContext::from_clock(RobotClock::default());
assert_eq!(ctx.instance_id(), 0);
assert_eq!(ctx.subsystem_code(), 0);
}
#[test]
fn builder_overrides_instance_id() {
let ctx = CuContext::builder(RobotClock::default())
.cl_id(7)
.instance_id(42)
.build();
assert_eq!(ctx.cl_id(), 7);
assert_eq!(ctx.instance_id(), 42);
assert_eq!(ctx.subsystem_code(), 0);
}
#[test]
fn runtime_metadata_sets_subsystem_code() {
let ctx = CuContext::from_runtime_metadata(RobotClock::default(), 9, 42, 7, &[]);
assert_eq!(ctx.cl_id(), 9);
assert_eq!(ctx.instance_id(), 42);
assert_eq!(ctx.subsystem_code(), 7);
assert_eq!(ctx.current_component_id(), None);
assert_eq!(ctx.task_index(), None);
}
#[test]
fn task_scope_updates_component_scope() {
let mut ctx = CuContext::builder(RobotClock::default())
.task_ids(&["task-0"])
.build();
ctx.set_current_task(0);
assert_eq!(ctx.current_component_id(), Some(0));
assert_eq!(ctx.task_index(), Some(0));
assert_eq!(ctx.task_id(), Some("task-0"));
}
#[test]
fn component_scope_can_exist_without_task_scope() {
let mut ctx = CuContext::from_clock(RobotClock::default());
ctx.set_current_component(7);
ctx.clear_current_task();
assert_eq!(ctx.current_component_id(), Some(7));
assert_eq!(ctx.task_index(), None);
assert_eq!(ctx.task_id(), None);
}
}