1use crate::bus::SimBus;
4use crate::clock::{Duration, Instant};
5use crate::io::NodeAddress;
6use heapless::Vec;
7
8pub trait SimNode<const N: usize, const Q: usize> {
20 type Error: core::fmt::Debug;
21
22 fn address(&self) -> &NodeAddress;
24
25 fn handle(&mut self, src: &NodeAddress, data: &[u8], now: Instant) -> Result<(), Self::Error>;
27
28 fn tick(&mut self, now: Instant) -> Result<(), Self::Error>;
33
34 fn drain_outbox(
39 &mut self,
40 out: &mut heapless::Vec<(NodeAddress, heapless::Vec<u8, N>), Q>,
41 ) -> usize;
42}
43
44pub trait SimNodeErased<const N: usize, const Q: usize> {
49 fn address(&self) -> &NodeAddress;
50 fn handle(&mut self, src: &NodeAddress, data: &[u8], now: Instant);
51 fn tick(&mut self, now: Instant);
52 fn drain_outbox(&mut self, out: &mut Vec<(NodeAddress, Vec<u8, N>), Q>) -> usize;
53}
54
55impl<const N: usize, const Q: usize, T> SimNodeErased<N, Q> for T
57where
58 T: SimNode<N, Q>,
59 T::Error: core::fmt::Debug,
60{
61 fn address(&self) -> &NodeAddress {
62 SimNode::address(self)
63 }
64
65 fn handle(&mut self, src: &NodeAddress, data: &[u8], now: Instant) {
66 if let Err(e) = SimNode::handle(self, src, data, now) {
67 let _ = e;
71 }
72 }
73
74 fn tick(&mut self, now: Instant) {
75 if let Err(e) = SimNode::tick(self, now) {
76 let _ = e;
77 }
78 }
79
80 fn drain_outbox(&mut self, out: &mut Vec<(NodeAddress, Vec<u8, N>), Q>) -> usize {
81 SimNode::drain_outbox(self, out)
82 }
83}
84
85pub struct SimRunner<const N: usize, const Q: usize> {
95 bus: SimBus<N, Q>,
96}
97
98impl<const N: usize, const Q: usize> SimRunner<N, Q> {
99 pub fn new(bus: SimBus<N, Q>) -> Self {
100 Self { bus }
101 }
102
103 pub fn tick(
108 &mut self,
109 nodes: &mut [&mut dyn SimNodeErased<N, Q>],
110 duration: Duration,
111 ) -> usize {
112 let delivered = self.bus.tick(duration);
113 let now = self.bus.now();
114 let mut count = 0;
115
116 for envelope in &delivered {
117 for node in nodes.iter_mut() {
118 if *node.address() == envelope.dst {
119 let _ = node.handle(&envelope.src, &envelope.data, now);
120 count += 1;
121 }
122 }
123 }
124
125 for node in nodes.iter_mut() {
126 let _ = node.tick(now);
127 }
128
129 let mut outbox = heapless::Vec::new();
130
131 for node in nodes.iter_mut() {
132 outbox.clear();
133 node.drain_outbox(&mut outbox);
134
135 let src = node.address().clone();
136 for (dst, data) in outbox.iter() {
137 self.bus.send(src.clone(), dst.clone(), data);
138 }
139 }
140
141 count
142 }
143
144 pub fn bus(&mut self) -> &mut SimBus<N, Q> {
145 &mut self.bus
146 }
147}
148
149