Skip to main content

trellis_core/
ids.rs

1use core::fmt;
2use core::num::NonZeroU64;
3
4/// Stable graph-local identity for a node.
5#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
6pub struct NodeId(NonZeroU64);
7
8impl NodeId {
9    pub(crate) fn from_index(index: u64) -> Self {
10        let value = NonZeroU64::new(index).expect("node ids start at 1");
11        Self(value)
12    }
13
14    /// Returns the opaque numeric value for deterministic inspection.
15    pub fn get(self) -> u64 {
16        self.0.get()
17    }
18}
19
20impl fmt::Debug for NodeId {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        write!(f, "NodeId({})", self.get())
23    }
24}
25
26/// Stable graph-local identity for a scope.
27#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
28pub struct ScopeId(NonZeroU64);
29
30impl ScopeId {
31    pub(crate) fn from_index(index: u64) -> Self {
32        let value = NonZeroU64::new(index).expect("scope ids start at 1");
33        Self(value)
34    }
35
36    /// Returns the opaque numeric value for deterministic inspection.
37    pub fn get(self) -> u64 {
38        self.0.get()
39    }
40}
41
42impl fmt::Debug for ScopeId {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "ScopeId({})", self.get())
45    }
46}
47
48/// Stable graph-local identity for a materialized output.
49#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
50pub struct OutputKey(NonZeroU64);
51
52impl OutputKey {
53    pub(crate) fn from_index(index: u64) -> Self {
54        let value = NonZeroU64::new(index).expect("output keys start at 1");
55        Self(value)
56    }
57
58    /// Returns the opaque numeric value for deterministic inspection.
59    pub fn get(self) -> u64 {
60        self.0.get()
61    }
62}
63
64impl fmt::Debug for OutputKey {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        write!(f, "OutputKey({})", self.get())
67    }
68}
69
70/// Monotonic graph revision marker.
71#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
72pub struct Revision(u64);
73
74impl Revision {
75    /// Creates a revision from a numeric value.
76    pub const fn new(value: u64) -> Self {
77        Self(value)
78    }
79
80    /// Returns the revision value.
81    pub const fn get(self) -> u64 {
82        self.0
83    }
84
85    pub(crate) const fn next(self) -> Self {
86        Self(self.0 + 1)
87    }
88}
89
90/// Monotonic transaction identity marker.
91#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
92pub struct TransactionId(u64);
93
94impl TransactionId {
95    /// Creates a transaction id from a numeric value.
96    pub const fn new(value: u64) -> Self {
97        Self(value)
98    }
99
100    /// Returns the transaction id value.
101    pub const fn get(self) -> u64 {
102        self.0
103    }
104
105    pub(crate) const fn next(self) -> Self {
106        Self(self.0 + 1)
107    }
108}