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
//! Owns `{config, context, history}` and runs the Elm loop.
//!
//! Persist **after** a successful step, never from an entry action.
//!
// rustbrain: [[docs/concepts/phase-space-snapshot]]
// rustbrain: [[docs/adr/0015-persist-after-rtc]]
// rustbrain: [[docs/edge_cases/panic-during-rtc]]
use crate::history::HistoryStore;
use crate::machine::{Boot, Machine};
use crate::rtc::Storm;
use crate::snapshot::Snapshot;
use crate::sub::Sub;
/// Running Newton machine: configuration + extended state + history sidecar.
#[derive(Clone, Debug)]
pub struct Runtime<M: Machine> {
machine: M,
model: M::Model,
history: M::History,
}
impl<M: Machine> Runtime<M> {
/// Boot from flags. Returns the runtime and the initial entry commands.
pub fn boot(flags: M::Flags) -> (Self, M::Cmd) {
let Boot {
machine,
model,
history,
cmd,
} = M::init(flags);
(
Self {
machine,
model,
history,
},
cmd,
)
}
/// Wrap an already-constructed triple (tests, restore scaffolding).
pub const fn new(machine: M, model: M::Model, history: M::History) -> Self {
Self {
machine,
model,
history,
}
}
/// From [`Machine::init`].
pub fn from_boot(boot: Boot<M>) -> (Self, M::Cmd) {
(
Self {
machine: boot.machine,
model: boot.model,
history: boot.history,
},
boot.cmd,
)
}
/// Hot path: one external message, one command. Does not execute the command.
///
/// Storm panics if the chart's `update` panics on [`Storm`] (the default
/// for machines that call [`crate::rtc::unwrap_storm`]). Use
/// [`Runtime::try_apply`] to Halt instead of die.
pub fn apply(&mut self, msg: M::Msg) -> M::Cmd {
self.machine.update(&mut self.model, &mut self.history, msg)
}
/// Apply `msg` only when `gate` is true (category-change / lift).
///
/// The host classifies. A tick that does not move an XOR child is
/// not a message. If `gate` is false, `msg` is dropped and this returns
/// `None` — prefer [`crate::host::changed`] so you only build `msg` on
/// a real change.
pub fn apply_if(&mut self, gate: bool, msg: M::Msg) -> Option<M::Cmd> {
if gate {
Some(self.apply(msg))
} else {
None
}
}
/// [`Runtime::apply`] that returns [`Storm`] instead of panicking.
///
/// Only useful if the machine **overrides** [`Machine::try_update`].
/// The default `try_update` calls `update`, so a panicking `update`
/// still panics here.
pub fn try_apply(&mut self, msg: M::Msg) -> Result<M::Cmd, Storm> {
self.machine
.try_update(&mut self.model, &mut self.history, msg)
}
/// Pure view.
pub fn view(&self) -> M::View {
self.machine.view(&self.model)
}
/// Subscriptions for the current configuration.
pub fn subscriptions(&self) -> Sub<M::Msg> {
self.machine.subscriptions(&self.model)
}
/// Whether `id` is in the active configuration.
pub fn in_state(&self, id: M::NodeId) -> bool {
self.machine.in_state(id)
}
/// [`Machine::project`]: compact key for a
/// host [`ChordTable`](crate::ChordTable) (or the host’s own map).
/// Not a Harel node.
#[inline]
pub fn project(&self) -> crate::bits::Bits {
self.machine.project()
}
/// Active node ids written into `out`.
pub fn configuration(&self, out: &mut [M::NodeId]) -> usize {
self.machine.configuration(out)
}
/// Borrow the configuration tree.
#[inline]
pub fn machine(&self) -> &M {
&self.machine
}
/// Borrow extended state.
#[inline]
pub fn model(&self) -> &M::Model {
&self.model
}
/// Borrow the history sidecar.
#[inline]
pub fn history(&self) -> &M::History {
&self.history
}
}
impl<M: Machine> Runtime<M>
where
M: Clone,
M::Model: Clone,
M::History: Clone,
{
/// Phase-space point after a completed step.
pub fn snapshot(&self) -> Snapshot<M, M::Model, M::History> {
Snapshot::new(
self.machine.clone(),
self.model.clone(),
self.history.clone(),
)
}
/// Replace the live triple. Host must still reconcile the world
/// (broker, sockets) after restore.
pub fn restore(&mut self, snap: Snapshot<M, M::Model, M::History>) {
self.machine = snap.config;
self.model = snap.context;
self.history = snap.history;
}
/// Save after RTC completed. Does not execute commands.
pub fn persist<S>(&self, store: &mut S) -> Result<(), S::Error>
where
S: HistoryStore<Config = M, Context = M::Model, History = M::History>,
{
store.save(&self.snapshot())
}
/// Load a snapshot if the store has one. Does not reconcile the host.
pub fn load<S>(store: &S) -> Result<Option<Self>, S::Error>
where
S: HistoryStore<Config = M, Context = M::Model, History = M::History>,
{
Ok(store.load()?.map(|snap| Self {
machine: snap.config,
model: snap.context,
history: snap.history,
}))
}
}