1use std::any::type_name;
2use std::cell::RefCell;
3use std::marker::PhantomData;
4use std::rc::Rc;
5
6use slotmap::{SecondaryMap, SlotMap};
7
8#[cfg(feature = "build")]
9use super::compiled::CompiledFlow;
10#[cfg(feature = "build")]
11use super::deploy::{DeployFlow, DeployResult};
12#[cfg(feature = "build")]
13use super::deploy_provider::{ClusterSpec, Deploy, ExternalSpec, IntoProcessSpec};
14use super::ir::HydroRoot;
15use crate::location::{Cluster, External, LocationKey, LocationType, Process};
16
17pub enum Sidecar {
20 Simple {
22 location_key: LocationKey,
23 future_expr: Box<syn::Expr>,
24 },
25 Bidi {
29 location_key: LocationKey,
30 sidecar_id: SidecarId,
31 sidecar_closure: Box<syn::Expr>,
32 },
33}
34#[cfg(feature = "sim")]
35#[cfg(stageleft_runtime)]
36use crate::sim::flow::SimFlow;
37use crate::staging_util::Invariant;
38
39#[stageleft::export(ExternalPortId, CycleId, ClockId, SidecarId, StmtId, HandoffId)]
40crate::newtype_counter! {
41 pub struct ExternalPortId(usize);
43
44 pub struct CycleId(usize);
46
47 pub struct ClockId(usize);
49
50 pub struct SidecarId(usize);
52
53 pub struct StmtId(usize);
55
56 pub struct HandoffId(usize);
58}
59
60impl CycleId {
61 #[cfg(feature = "build")]
62 pub(crate) fn as_ident(&self) -> syn::Ident {
63 syn::Ident::new(&format!("cycle_{}", self), proc_macro2::Span::call_site())
64 }
65}
66
67impl SidecarId {
68 pub fn idents(&self) -> (syn::Ident, syn::Ident) {
70 let span = proc_macro2::Span::call_site();
71 (
72 syn::Ident::new(&format!("__hydro_sidecar_{}_stream", self), span),
73 syn::Ident::new(&format!("__hydro_sidecar_{}_sink", self), span),
74 )
75 }
76}
77
78pub(crate) type FlowState = Rc<RefCell<FlowStateInner>>;
79
80pub(crate) struct FlowStateInner {
81 roots: Option<Vec<HydroRoot>>,
85
86 next_external_port: crate::Counter<ExternalPortId>,
88
89 next_cycle_id: crate::Counter<CycleId>,
91
92 next_clock_id: crate::Counter<ClockId>,
94
95 next_sidecar_id: crate::Counter<SidecarId>,
97
98 pub sidecars: Vec<Sidecar>,
101}
102
103impl FlowStateInner {
104 pub fn next_external_port(&mut self) -> ExternalPortId {
105 self.next_external_port.get_and_increment()
106 }
107
108 pub fn next_cycle_id(&mut self) -> CycleId {
109 self.next_cycle_id.get_and_increment()
110 }
111
112 pub fn next_clock_id(&mut self) -> ClockId {
113 self.next_clock_id.get_and_increment()
114 }
115
116 pub fn next_sidecar_id(&mut self) -> SidecarId {
117 self.next_sidecar_id.get_and_increment()
118 }
119
120 pub fn push_root(&mut self, root: HydroRoot) {
121 self.roots
122 .as_mut()
123 .expect("Attempted to add a root to a flow that has already been finalized. No roots can be added after the flow has been compiled.")
124 .push(root);
125 }
126
127 pub fn try_push_root(&mut self, root: HydroRoot) {
128 if let Some(roots) = self.roots.as_mut() {
129 roots.push(root);
130 }
131 }
132}
133
134pub struct FlowBuilder<'a> {
135 flow_state: FlowState,
137
138 locations: SlotMap<LocationKey, LocationType>,
140 location_names: SecondaryMap<LocationKey, String>,
142 #[cfg(feature = "sim")]
145 location_version: SecondaryMap<LocationKey, u32>,
146 #[cfg(feature = "sim")]
150 location_version_group_root: SecondaryMap<LocationKey, LocationKey>,
151
152 #[cfg_attr(
154 not(feature = "build"),
155 expect(dead_code, reason = "unused without build")
156 )]
157 flow_name: String,
158
159 finalized: bool,
162
163 _phantom: Invariant<'a>,
168}
169
170impl Drop for FlowBuilder<'_> {
171 fn drop(&mut self) {
172 if !self.finalized && !std::thread::panicking() {
173 panic!(
174 "Dropped FlowBuilder without finalizing, you may have forgotten to call `with_default_optimize`, `optimize_with`, or `finalize`."
175 );
176 }
177 }
178}
179
180#[expect(missing_docs, reason = "TODO")]
181impl<'a> FlowBuilder<'a> {
182 #[expect(
184 clippy::new_without_default,
185 reason = "call `new` explicitly, not `default`"
186 )]
187 pub fn new() -> Self {
188 let mut name = std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "unknown".to_owned());
189 if let Ok(bin_path) = std::env::current_exe()
190 && let Some(bin_name) = bin_path.file_stem()
191 {
192 name = format!("{}/{}", name, bin_name.display());
193 }
194 Self::with_name(name)
195 }
196
197 pub fn with_name(name: impl Into<String>) -> Self {
199 Self {
200 flow_state: Rc::new(RefCell::new(FlowStateInner {
201 roots: Some(vec![]),
202 next_external_port: crate::Counter::default(),
203 next_cycle_id: crate::Counter::default(),
204 next_clock_id: crate::Counter::default(),
205 next_sidecar_id: crate::Counter::default(),
206 sidecars: Vec::new(),
207 })),
208 locations: SlotMap::with_key(),
209 location_names: SecondaryMap::new(),
210 #[cfg(feature = "sim")]
211 location_version: SecondaryMap::new(),
212 #[cfg(feature = "sim")]
213 location_version_group_root: SecondaryMap::new(),
214 flow_name: name.into(),
215 finalized: false,
216 _phantom: PhantomData,
217 }
218 }
219
220 pub(crate) fn flow_state(&self) -> &FlowState {
221 &self.flow_state
222 }
223
224 fn insert_location(&mut self, ty: LocationType, name: String) -> LocationKey {
225 let key = self.locations.insert(ty);
226 self.location_names.insert(key, name);
227 #[cfg(feature = "sim")]
228 {
229 self.location_version.insert(key, 0);
230 self.location_version_group_root.insert(key, key);
231 }
232 key
233 }
234
235 pub fn process<P>(&mut self) -> Process<'a, P> {
236 let key = self.insert_location(LocationType::Process, type_name::<P>().to_owned());
237 Process {
238 key,
239 flow_state: self.flow_state().clone(),
240 _phantom: PhantomData,
241 }
242 }
243
244 pub fn cluster<C>(&mut self) -> Cluster<'a, C> {
245 let key = self.insert_location(LocationType::Cluster, type_name::<C>().to_owned());
246 Cluster {
247 key,
248 flow_state: self.flow_state().clone(),
249 _phantom: PhantomData,
250 }
251 }
252
253 pub fn external<E>(&mut self) -> External<'a, E> {
254 let key = self.insert_location(LocationType::External, type_name::<E>().to_owned());
255 External {
256 key,
257 flow_state: self.flow_state().clone(),
258 _phantom: PhantomData,
259 }
260 }
261
262 #[cfg(feature = "sim")]
263 pub fn next_version<C>(&mut self, cluster: &Cluster<'a, C>) -> Cluster<'a, C> {
264 let group_root = self.location_version_group_root[cluster.key];
265 let version = self
266 .location_version_group_root
267 .values()
268 .filter(|&&r| r == group_root)
269 .count() as u32;
270 let key = self.insert_location(LocationType::Cluster, type_name::<C>().to_owned());
271 self.location_version.insert(key, version);
272 self.location_version_group_root.insert(key, group_root);
273 Cluster {
274 key,
275 flow_state: self.flow_state().clone(),
276 _phantom: PhantomData,
277 }
278 }
279}
280
281#[cfg(feature = "build")]
282#[cfg_attr(docsrs, doc(cfg(feature = "build")))]
283#[expect(missing_docs, reason = "TODO")]
284impl<'a> FlowBuilder<'a> {
285 pub fn finalize(mut self) -> super::built::BuiltFlow<'a> {
286 self.finalized = true;
287
288 let mut flow_state = self.flow_state.borrow_mut();
289 let mut ir = flow_state.roots.take().unwrap();
290 let sidecars = std::mem::take(&mut flow_state.sidecars);
291 drop(flow_state);
292
293 super::ir::unify_atomic_ticks(&mut ir);
294
295 super::built::BuiltFlow {
296 ir,
297 locations: std::mem::take(&mut self.locations),
298 location_names: std::mem::take(&mut self.location_names),
299 sidecars,
300 flow_name: std::mem::take(&mut self.flow_name),
301 #[cfg(feature = "sim")]
302 location_version: std::mem::take(&mut self.location_version),
303 #[cfg(feature = "sim")]
304 location_version_group_root: std::mem::take(&mut self.location_version_group_root),
305 _phantom: PhantomData,
306 }
307 }
308
309 pub fn with_default_optimize<D: Deploy<'a>>(self) -> DeployFlow<'a, D> {
310 self.finalize().with_default_optimize()
311 }
312
313 pub fn optimize_with(self, f: impl FnOnce(&mut [HydroRoot])) -> super::built::BuiltFlow<'a> {
314 self.finalize().optimize_with(f)
315 }
316
317 pub fn with_process<P, D: Deploy<'a>>(
318 self,
319 process: &Process<P>,
320 spec: impl IntoProcessSpec<'a, D>,
321 ) -> DeployFlow<'a, D> {
322 self.with_default_optimize().with_process(process, spec)
323 }
324
325 pub fn with_remaining_processes<D: Deploy<'a>, S: IntoProcessSpec<'a, D> + 'a>(
326 self,
327 spec: impl Fn() -> S,
328 ) -> DeployFlow<'a, D> {
329 self.with_default_optimize().with_remaining_processes(spec)
330 }
331
332 pub fn with_external<P, D: Deploy<'a>>(
333 self,
334 process: &External<P>,
335 spec: impl ExternalSpec<'a, D>,
336 ) -> DeployFlow<'a, D> {
337 self.with_default_optimize().with_external(process, spec)
338 }
339
340 pub fn with_remaining_externals<D: Deploy<'a>, S: ExternalSpec<'a, D> + 'a>(
341 self,
342 spec: impl Fn() -> S,
343 ) -> DeployFlow<'a, D> {
344 self.with_default_optimize().with_remaining_externals(spec)
345 }
346
347 pub fn with_cluster<C, D: Deploy<'a>>(
348 self,
349 cluster: &Cluster<C>,
350 spec: impl ClusterSpec<'a, D>,
351 ) -> DeployFlow<'a, D> {
352 self.with_default_optimize().with_cluster(cluster, spec)
353 }
354
355 pub fn with_remaining_clusters<D: Deploy<'a>, S: ClusterSpec<'a, D> + 'a>(
356 self,
357 spec: impl Fn() -> S,
358 ) -> DeployFlow<'a, D> {
359 self.with_default_optimize().with_remaining_clusters(spec)
360 }
361
362 pub fn compile<D: Deploy<'a, InstantiateEnv = ()>>(self) -> CompiledFlow<'a> {
363 self.with_default_optimize::<D>().compile()
364 }
365
366 pub fn deploy<D: Deploy<'a>>(self, env: &mut D::InstantiateEnv) -> DeployResult<'a, D> {
367 self.with_default_optimize().deploy(env)
368 }
369
370 #[cfg(feature = "sim")]
371 pub fn sim(self) -> SimFlow<'a> {
374 self.finalize().sim()
375 }
376
377 pub fn from_built<'b>(built: &super::built::BuiltFlow) -> FlowBuilder<'b> {
378 FlowBuilder {
379 flow_state: Rc::new(RefCell::new(FlowStateInner {
380 roots: None,
381 next_external_port: crate::Counter::default(),
382 next_cycle_id: crate::Counter::default(),
383 next_clock_id: crate::Counter::default(),
384 next_sidecar_id: crate::Counter::default(),
385 sidecars: Vec::new(),
386 })),
387 locations: built.locations.clone(),
388 location_names: built.location_names.clone(),
389 #[cfg(feature = "sim")]
390 location_version: built.location_version.clone(),
391 #[cfg(feature = "sim")]
392 location_version_group_root: built.location_version_group_root.clone(),
393 flow_name: built.flow_name.clone(),
394 finalized: false,
395 _phantom: PhantomData,
396 }
397 }
398
399 #[doc(hidden)] pub fn replace_ir(&mut self, roots: Vec<HydroRoot>) {
401 self.flow_state.borrow_mut().roots = Some(roots);
402 }
403
404 #[doc(hidden)] pub fn next_clock_id(&mut self) -> ClockId {
406 self.flow_state.borrow_mut().next_clock_id()
407 }
408
409 #[doc(hidden)] pub fn next_cycle_id(&mut self) -> CycleId {
411 self.flow_state.borrow_mut().next_cycle_id()
412 }
413}