1mod codelet_instance;
4mod lifecycle;
5mod schedule;
6mod sequence;
7mod statistics;
8mod task_clock;
9mod transition;
10mod vise;
11
12#[cfg(feature = "instantiate_from_json")]
13mod instantiate_from_json;
14
15pub use codelet_instance::*;
16pub use lifecycle::*;
17pub use schedule::*;
18pub use sequence::*;
19pub use statistics::*;
20pub use task_clock::*;
21pub use transition::*;
22pub use vise::*;
23
24#[cfg(feature = "instantiate_from_json")]
25pub use instantiate_from_json::*;
26
27use crate::{
28 channels::{RxBundle, TxBundle},
29 config::Config,
30 core::DefaultStatus,
31 prelude::Signals,
32};
33use eyre::Result;
34
35pub trait Codelet: Send {
37 type Status: CodeletStatus;
39
40 type Config: Config + Send;
42
43 type Rx: RxBundle;
45
46 type Tx: TxBundle;
48
49 type Signals: Signals;
51
52 fn build_bundles(cfg: &Self::Config) -> (Self::Rx, Self::Tx);
54
55 fn start(
57 &mut self,
58 _cx: Context<Self>,
59 _rx: &mut Self::Rx,
60 _tx: &mut Self::Tx,
61 ) -> Result<Self::Status> {
62 Ok(Self::Status::default_implementation_status())
63 }
64
65 fn stop(
67 &mut self,
68 _cx: Context<Self>,
69 _rx: &mut Self::Rx,
70 _tx: &mut Self::Tx,
71 ) -> Result<Self::Status> {
72 Ok(Self::Status::default_implementation_status())
73 }
74
75 fn step(
77 &mut self,
78 _cx: Context<Self>,
79 _rx: &mut Self::Rx,
80 _tx: &mut Self::Tx,
81 ) -> Result<Self::Status> {
82 Ok(Self::Status::default_implementation_status())
83 }
84
85 fn pause(&mut self) -> Result<Self::Status> {
87 Ok(Self::Status::default_implementation_status())
88 }
89
90 fn resume(&mut self) -> Result<Self::Status> {
93 Ok(Self::Status::default_implementation_status())
94 }
95}
96
97pub trait CodeletStatus: 'static + Send + Sync {
98 fn default_implementation_status() -> Self
100 where
101 Self: Sized;
102
103 fn as_default_status(&self) -> DefaultStatus;
105
106 fn label(&self) -> &str;
108}
109
110impl CodeletStatus for DefaultStatus {
111 fn default_implementation_status() -> Self {
112 DefaultStatus::Skipped
113 }
114
115 fn as_default_status(&self) -> DefaultStatus {
116 *self
117 }
118
119 fn label(&self) -> &str {
120 match self {
121 DefaultStatus::Skipped => "skipped",
122 DefaultStatus::Running => "running",
123 }
124 }
125}
126
127pub struct Context<'a, C>
129where
130 C: Codelet + ?Sized,
131{
132 pub clocks: &'a TaskClocks,
134
135 pub config: &'a C::Config,
137
138 pub config_aux: &'a <C::Config as Config>::Aux,
140
141 pub pulse: &'a CodeletPulse,
143
144 pub signals: &'a mut C::Signals,
146}
147
148pub struct CodeletPulse {
149 pub step_count: usize,
151}
152
153impl CodeletPulse {
154 pub(crate) fn new() -> Self {
155 Self { step_count: 0 }
156 }
157
158 pub(crate) fn on_step_post(&mut self) {
159 self.step_count += 1;
160 }
161}
162
163pub trait IntoInstance: Codelet + Sized {
182 fn into_instance<S: Into<String>>(self, name: S, config: Self::Config)
183 -> CodeletInstance<Self>;
184}
185
186impl<C> IntoInstance for C
187where
188 C: Codelet,
189{
190 fn into_instance<S: Into<String>>(
191 self,
192 name: S,
193 config: Self::Config,
194 ) -> CodeletInstance<Self> {
195 CodeletInstance::new(name, self, config)
196 }
197}
198
199pub trait Instantiate: Codelet + Sized {
219 fn instantiate<S: Into<String>>(name: S, config: Self::Config) -> CodeletInstance<Self>;
220}
221
222impl<C> Instantiate for C
223where
224 C: Codelet + Default,
225{
226 fn instantiate<S: Into<String>>(name: S, config: Self::Config) -> CodeletInstance<Self> {
227 CodeletInstance::new(name, C::default(), config)
228 }
229}