Skip to main content

calandria_sim/simulation/
view.rs

1//! Immutable post-action state exposed to deterministic monitors.
2
3use alloc::collections::BTreeMap;
4
5use crate::{DutyId, Model, Topology};
6
7use super::{DutySnapshot, DutyState, SimulationSnapshot};
8
9/// Immutable view of model and kernel state after one committed action.
10#[derive(Clone, Copy, Debug)]
11pub struct SimulationView<'a, M: Model> {
12    model: &'a M,
13    topology: &'a Topology,
14    states: &'a BTreeMap<DutyId, DutyState>,
15    snapshot: SimulationSnapshot,
16}
17
18impl<'a, M: Model> SimulationView<'a, M> {
19    pub(crate) const fn new(
20        model: &'a M,
21        topology: &'a Topology,
22        states: &'a BTreeMap<DutyId, DutyState>,
23        snapshot: SimulationSnapshot,
24    ) -> Self {
25        Self {
26            model,
27            topology,
28            states,
29            snapshot,
30        }
31    }
32
33    /// Returns immutable consumer-owned model state.
34    pub const fn model(self) -> &'a M {
35        self.model
36    }
37
38    /// Returns the fixed deterministic topology.
39    pub const fn topology(self) -> &'a Topology {
40        self.topology
41    }
42
43    /// Returns one owner's current state.
44    pub fn duty(self, duty: DutyId) -> Option<DutySnapshot> {
45        self.states
46            .get(&duty)
47            .copied()
48            .map(|state| DutySnapshot::new(duty, state))
49    }
50
51    /// Returns bounded global kernel state.
52    pub const fn snapshot(self) -> SimulationSnapshot {
53        self.snapshot
54    }
55}