aimdb_core/context.rs
1//! Runtime context for AimDB services
2//!
3//! Provides a unified interface to runtime capabilities (time, sleep, logging)
4//! as a concrete value wrapping `Arc<dyn RuntimeOps>` and the clock time is
5//! read from โ no runtime type parameter.
6
7use crate::clock::{InstanceClock, Stamp};
8use crate::executor::{BoxFuture, LogLevel, RuntimeOps};
9use alloc::sync::Arc;
10
11/// Unified runtime context for AimDB services
12///
13/// Wraps the dyn-safe [`RuntimeOps`] capability surface and a clock so
14/// services can use timing and logging without knowing the runtime adapter
15/// type. Cheap to clone (two `Arc`s).
16///
17/// Every context an `AimDb` hands out carries the instance clock, the one that
18/// stamps writes (design 058 ยง3.1).
19#[derive(Clone)]
20pub struct RuntimeContext {
21 ops: Arc<dyn RuntimeOps>,
22 clock: Arc<dyn InstanceClock>,
23}
24
25impl RuntimeContext {
26 /// A context with a clock of its own, created now โ for code outside an
27 /// instance, such as a standalone client engine. Inside an instance use
28 /// `AimDb::runtime_ctx()`, which shares the instance clock.
29 pub fn new(ops: Arc<dyn RuntimeOps>) -> Self {
30 let clock = ops.new_clock();
31 Self { ops, clock }
32 }
33
34 /// A context over `ops` that reads time from `clock`.
35 pub fn with_clock(ops: Arc<dyn RuntimeOps>, clock: Arc<dyn InstanceClock>) -> Self {
36 Self { ops, clock }
37 }
38
39 /// Access time utilities
40 ///
41 /// Returns a time accessor for clock reads and sleeping.
42 pub fn time(&self) -> Time<'_> {
43 Time {
44 ops: &*self.ops,
45 clock: &*self.clock,
46 }
47 }
48
49 /// The clock this context reads time from.
50 pub fn clock(&self) -> &Arc<dyn InstanceClock> {
51 &self.clock
52 }
53
54 /// Access logging utilities
55 pub fn log(&self) -> Log<'_> {
56 Log { ops: &*self.ops }
57 }
58
59 /// Direct access to the underlying runtime capabilities.
60 pub fn ops(&self) -> &Arc<dyn RuntimeOps> {
61 &self.ops
62 }
63}
64
65/// Time utilities accessor for RuntimeContext
66///
67/// Durations are plain [`core::time::Duration`]; instants are [`Stamp`]s
68/// on the context's clock.
69pub struct Time<'a> {
70 ops: &'a dyn RuntimeOps,
71 clock: &'a dyn InstanceClock,
72}
73
74impl Time<'_> {
75 /// Monotonic reading of the context's clock: inside an instance, time
76 /// since the instance started, on the clock that stamps writes.
77 pub fn now(&self) -> Stamp {
78 self.clock.now()
79 }
80
81 /// Wall-clock time as `(seconds, nanoseconds)` since the Unix epoch, if
82 /// the runtime has a real-time clock (`None` on e.g. a bare MCU without
83 /// an RTC anchor). May be adjusted; unrelated to [`now`](Self::now).
84 pub fn unix_time(&self) -> Option<(u64, u32)> {
85 self.clock.unix_time()
86 }
87
88 /// Completes after at least `duration` has elapsed.
89 ///
90 /// The future is boxed (it crosses the `dyn RuntimeOps` boundary), so each
91 /// call costs one small heap allocation. That is fine for periodic loops
92 /// and timeouts; in a no_std hot path that sleeps every iteration, await
93 /// the adapter's native timer (e.g. `embassy_time::Timer`) directly
94 /// instead.
95 pub fn sleep(&self, duration: core::time::Duration) -> BoxFuture {
96 self.ops.sleep(duration)
97 }
98
99 /// Convenience: sleep for `ms` milliseconds.
100 pub fn sleep_millis(&self, ms: u64) -> BoxFuture {
101 self.ops.sleep(core::time::Duration::from_millis(ms))
102 }
103
104 /// Convenience: sleep for `secs` seconds.
105 pub fn sleep_secs(&self, secs: u64) -> BoxFuture {
106 self.ops.sleep(core::time::Duration::from_secs(secs))
107 }
108}
109
110/// Log utilities accessor for RuntimeContext
111///
112/// Provides structured logging operations.
113pub struct Log<'a> {
114 ops: &'a dyn RuntimeOps,
115}
116
117impl Log<'_> {
118 /// Log an informational message
119 pub fn info(&self, message: &str) {
120 self.ops.log(LogLevel::Info, message)
121 }
122
123 /// Log a debug message
124 pub fn debug(&self, message: &str) {
125 self.ops.log(LogLevel::Debug, message)
126 }
127
128 /// Log a warning message
129 pub fn warn(&self, message: &str) {
130 self.ops.log(LogLevel::Warn, message)
131 }
132
133 /// Log an error message
134 pub fn error(&self, message: &str) {
135 self.ops.log(LogLevel::Error, message)
136 }
137}