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