hydro_lang/location/
dynamic.rs

1//! Definitions for interacting with locations using an untyped interface.
2//!
3//! Under the hood, locations are associated with a [`LocationId`] value that
4//! uniquely identifies the location. Manipulating these values is useful for
5//! observability and transforming the Hydro IR.
6
7use serde::{Deserialize, Serialize};
8
9#[cfg(stageleft_runtime)]
10use crate::compile::{builder::FlowState, ir::HydroIrMetadata};
11
12#[expect(missing_docs, reason = "TODO")]
13#[derive(PartialEq, Eq, Clone, Debug, Hash, Serialize, Deserialize)]
14pub enum LocationId {
15    Process(usize),
16    Cluster(usize),
17    Tick(usize, Box<LocationId>),
18}
19
20#[expect(missing_docs, reason = "TODO")]
21impl LocationId {
22    pub fn root(&self) -> &LocationId {
23        match self {
24            LocationId::Process(_) => self,
25            LocationId::Cluster(_) => self,
26            LocationId::Tick(_, id) => id.root(),
27        }
28    }
29
30    pub fn is_root(&self) -> bool {
31        match self {
32            LocationId::Process(_) | LocationId::Cluster(_) => true,
33            LocationId::Tick(_, _) => false,
34        }
35    }
36
37    pub fn raw_id(&self) -> usize {
38        match self {
39            LocationId::Process(id) => *id,
40            LocationId::Cluster(id) => *id,
41            LocationId::Tick(_, _) => panic!("cannot get raw id for tick"),
42        }
43    }
44
45    pub fn swap_root(&mut self, new_root: LocationId) {
46        match self {
47            LocationId::Tick(_, id) => {
48                id.swap_root(new_root);
49            }
50            _ => {
51                assert!(new_root.is_root());
52                *self = new_root;
53            }
54        }
55    }
56}
57
58#[cfg(stageleft_runtime)]
59pub(crate) trait DynLocation: Clone {
60    fn id(&self) -> LocationId;
61
62    fn flow_state(&self) -> &FlowState;
63    fn is_top_level() -> bool;
64
65    fn new_node_metadata<T>(&self) -> HydroIrMetadata {
66        use stageleft::quote_type;
67
68        use crate::compile::ir::HydroIrOpMetadata;
69        use crate::compile::ir::backtrace::Backtrace;
70
71        HydroIrMetadata {
72            location_kind: self.id(),
73            output_type: Some(quote_type::<T>().into()),
74            cardinality: None,
75            tag: None,
76            op: HydroIrOpMetadata {
77                backtrace: Backtrace::get_backtrace(2),
78                cpu_usage: None,
79                network_recv_cpu_usage: None,
80                id: None,
81            },
82        }
83    }
84}