Skip to main content

bornera_core/
identity.rs

1//! Domain-specific identities owned by the connection policy.
2
3/// A logical remote endpoint.
4#[repr(transparent)]
5#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
6pub struct EndpointId(u64);
7
8impl EndpointId {
9    /// Creates an endpoint identity.
10    pub const fn new(value: u64) -> Self {
11        Self(value)
12    }
13
14    /// Returns the fixed-width representation.
15    pub const fn get(self) -> u64 {
16        self.0
17    }
18}
19
20/// An opaque traffic lane within an endpoint.
21#[repr(transparent)]
22#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
23pub struct LaneId(u32);
24
25impl LaneId {
26    /// Creates a lane identity.
27    pub const fn new(value: u32) -> Self {
28        Self(value)
29    }
30
31    /// Returns the fixed-width representation.
32    pub const fn get(self) -> u32 {
33        self.0
34    }
35}
36
37/// A physical connection slot.
38#[repr(transparent)]
39#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
40pub struct ConnectionId(u64);
41
42impl ConnectionId {
43    /// Creates a connection identity.
44    pub const fn new(value: u64) -> Self {
45        Self(value)
46    }
47
48    /// Returns the fixed-width representation.
49    pub const fn get(self) -> u64 {
50        self.0
51    }
52}
53
54/// One exact lifetime of a physical connection.
55#[repr(transparent)]
56#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
57pub struct ConnectionEpoch(u64);
58
59impl ConnectionEpoch {
60    /// Creates an epoch identity.
61    pub const fn new(value: u64) -> Self {
62        Self(value)
63    }
64
65    /// Returns the fixed-width representation.
66    pub const fn get(self) -> u64 {
67        self.0
68    }
69}
70
71/// An accepted operation owned by one epoch.
72#[repr(transparent)]
73#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
74pub struct OperationId(u64);
75
76impl OperationId {
77    /// Creates an operation identity.
78    pub const fn new(value: u64) -> Self {
79        Self(value)
80    }
81
82    /// Returns the fixed-width representation.
83    pub const fn get(self) -> u64 {
84        self.0
85    }
86}
87
88/// A transport effect whose completion may return later.
89#[repr(transparent)]
90#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
91pub struct EffectId(u64);
92
93impl EffectId {
94    /// Creates an effect identity.
95    pub const fn new(value: u64) -> Self {
96        Self(value)
97    }
98
99    /// Returns the fixed-width representation.
100    pub const fn get(self) -> u64 {
101        self.0
102    }
103}
104
105/// A protocol-visible response matching identity.
106#[repr(transparent)]
107#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
108pub struct MatchKey(u32);
109
110impl MatchKey {
111    /// Creates a match key.
112    pub const fn new(value: u32) -> Self {
113        Self(value)
114    }
115
116    /// Returns the fixed-width representation.
117    pub const fn get(self) -> u32 {
118        self.0
119    }
120}
121
122/// Initial deterministic values for generated identities.
123#[derive(Clone, Copy, Debug, Eq, PartialEq)]
124pub struct IdentitySeeds {
125    operation: u64,
126    effect: u64,
127}
128
129impl IdentitySeeds {
130    /// Starts both generated identity domains at zero.
131    pub const ZERO: Self = Self::new(0, 0);
132
133    /// Creates deterministic identity seeds.
134    pub const fn new(operation: u64, effect: u64) -> Self {
135        Self { operation, effect }
136    }
137
138    /// Returns the first operation identity.
139    pub const fn operation(self) -> u64 {
140        self.operation
141    }
142
143    /// Returns the first effect identity.
144    pub const fn effect(self) -> u64 {
145        self.effect
146    }
147}
148
149#[derive(Debug)]
150pub(crate) struct IdentityGenerator {
151    operation: Option<u64>,
152    effect: Option<u64>,
153}
154
155impl IdentityGenerator {
156    pub(crate) const fn new(seeds: IdentitySeeds) -> Self {
157        Self {
158            operation: Some(seeds.operation()),
159            effect: Some(seeds.effect()),
160        }
161    }
162
163    pub(crate) const fn available(&self) -> bool {
164        self.operation.is_some() && self.effect.is_some()
165    }
166
167    pub(crate) fn take(&mut self) -> Option<(OperationId, EffectId)> {
168        let operation = self.operation?;
169        let effect = self.effect?;
170        self.operation = operation.checked_add(1);
171        self.effect = effect.checked_add(1);
172        Some((OperationId::new(operation), EffectId::new(effect)))
173    }
174}