Skip to main content

bb_runtime/
ids.rs

1//! Engine-internal IDs. Wire/IR IDs come from `bb_ir::ids`
2//! re-exported here for a single import surface.
3//!
4//! Each integer ID is a `#[repr(transparent)]` newtype so the
5//! borrow checker rejects cross-type substitution.
6
7use std::fmt;
8
9pub use bb_ir::ids::{ComponentTag, OpsetId, PeerId, RequestId};
10
11// --- Macro helpers ----------------------------------------------
12
13macro_rules! u64_id {
14    ($(#[$attr:meta])* $name:ident) => {
15        $(#[$attr])*
16        #[derive(
17            Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord,
18            serde::Serialize, serde::Deserialize,
19        )]
20        #[repr(transparent)]
21        pub struct $name(u64);
22
23        impl $name {
24            /// Construct from an explicit value.
25            pub const fn new(inner: u64) -> Self { Self(inner) }
26
27            /// Inner value accessor.
28            pub const fn as_u64(self) -> u64 { self.0 }
29        }
30
31        impl fmt::Display for $name {
32            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33                write!(f, "{}({})", stringify!($name), self.0)
34            }
35        }
36
37        impl From<u64> for $name {
38            fn from(inner: u64) -> Self { Self(inner) }
39        }
40    };
41}
42
43macro_rules! u32_id {
44    ($(#[$attr:meta])* $name:ident) => {
45        $(#[$attr])*
46        #[derive(
47            Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord,
48            serde::Serialize, serde::Deserialize,
49        )]
50        #[repr(transparent)]
51        pub struct $name(u32);
52
53        impl $name {
54            /// Construct from an explicit value.
55            pub const fn new(inner: u32) -> Self { Self(inner) }
56
57            /// Inner value accessor.
58            pub const fn as_u32(self) -> u32 { self.0 }
59        }
60
61        impl fmt::Display for $name {
62            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63                write!(f, "{}({})", stringify!($name), self.0)
64            }
65        }
66
67        impl From<u32> for $name {
68            fn from(inner: u32) -> Self { Self(inner) }
69        }
70    };
71}
72
73// --- Engine-internal integer IDs --------------------------------
74
75u64_id! {
76    /// IR-level value site. Names a slot inside a `GraphProto`'s
77    /// flow that the engine fills with a slot value.
78    NodeSiteId
79}
80
81u64_id! {
82    /// Op handle within a graph. Positional refs encode
83    /// `(graph_idx << 32) | node_idx`; one indexed lookup per
84    /// invoke, no HashMap probe.
85    OpRef
86}
87
88impl OpRef {
89    /// Pack a `(graph_idx, node_idx)` into one `OpRef`.
90    pub const fn pack(graph_idx: u32, node_idx: u32) -> Self {
91        Self::new(((graph_idx as u64) << 32) | (node_idx as u64))
92    }
93
94    /// Unpack a positional `OpRef`. Globally-counter-minted refs
95    /// have a zero high half.
96    pub const fn split(self) -> (u32, u32) {
97        let v = self.as_u64();
98        ((v >> 32) as u32, v as u32)
99    }
100}
101
102u64_id! {
103    /// Per-execution identifier; survives async completions and
104    /// cross-Node wire hops.
105    ExecId
106}
107
108u64_id! {
109    /// Async-dispatch command id finalized via
110    /// `ctx.complete_command(cmd, outputs)`.
111    CommandId
112}
113
114u32_id! {
115    /// Dense per-Node component instance handle.
116    ComponentRef
117}
118