-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
200 lines (171 loc) · 6.4 KB
/
lib.rs
File metadata and controls
200 lines (171 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-License-Identifier: Apache-2.0
// © James Ross Ω FLYING•ROBOTS <https://github.com/flyingrobots>
//! Shared ADR-0008 runtime schema primitives.
//!
//! This crate is the Echo-local shared owner for generated-or-generation-ready
//! runtime schema types that are not inherently ABI-only:
//!
//! - opaque runtime identifiers
//! - logical monotone counters
//! - structural runtime key types
//!
//! Adapter crates such as `echo-wasm-abi` may still wrap these types when the
//! host wire format needs a different serialization contract.
#![cfg_attr(not(feature = "std"), no_std)]
use core::fmt;
macro_rules! logical_counter {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct $name(pub u64);
impl $name {
/// Zero value for this logical counter.
pub const ZERO: Self = Self(0);
/// Largest representable counter value.
pub const MAX: Self = Self(u64::MAX);
/// Builds the counter from its raw logical value.
#[must_use]
pub const fn from_raw(raw: u64) -> Self {
Self(raw)
}
/// Returns the raw logical value.
#[must_use]
pub const fn as_u64(self) -> u64 {
self.0
}
/// Adds `rhs`, returning `None` on overflow.
#[must_use]
pub fn checked_add(self, rhs: u64) -> Option<Self> {
self.0.checked_add(rhs).map(Self)
}
/// Subtracts `rhs`, returning `None` on underflow.
#[must_use]
pub fn checked_sub(self, rhs: u64) -> Option<Self> {
self.0.checked_sub(rhs).map(Self)
}
/// Increments by one, returning `None` on overflow.
#[must_use]
pub fn checked_increment(self) -> Option<Self> {
self.checked_add(1)
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
};
}
/// Canonical 32-byte identifier payload used by shared runtime schema ids.
pub type RuntimeIdBytes = [u8; 32];
/// Opaque stable identifier for a worldline.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct WorldlineId(RuntimeIdBytes);
impl WorldlineId {
/// Reconstructs a worldline id from its canonical 32-byte representation.
#[must_use]
pub const fn from_bytes(bytes: RuntimeIdBytes) -> Self {
Self(bytes)
}
/// Returns the canonical byte representation of this id.
#[must_use]
pub const fn as_bytes(&self) -> &RuntimeIdBytes {
&self.0
}
}
/// Opaque stable identifier for a head.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct HeadId(RuntimeIdBytes);
impl HeadId {
/// Inclusive minimum key used by internal `BTreeMap` range queries.
pub const MIN: Self = Self([0u8; 32]);
/// Inclusive maximum key used by internal `BTreeMap` range queries.
pub const MAX: Self = Self([0xff; 32]);
/// Reconstructs a head id from its canonical 32-byte representation.
#[must_use]
pub const fn from_bytes(bytes: RuntimeIdBytes) -> Self {
Self(bytes)
}
/// Returns the canonical byte representation of this id.
#[must_use]
pub const fn as_bytes(&self) -> &RuntimeIdBytes {
&self.0
}
}
logical_counter!(
/// Per-worldline append identity for committed history.
WorldlineTick
);
logical_counter!(
/// Runtime-cycle correlation stamp. No wall-clock semantics.
GlobalTick
);
logical_counter!(
/// Control-plane generation token for scheduler runs.
///
/// This value is not provenance, replay state, or hash input.
RunId
);
/// Composite key identifying a writer head within its worldline.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WriterHeadKey {
/// The worldline this head targets.
pub worldline_id: WorldlineId,
/// The head identity within that worldline.
pub head_id: HeadId,
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::{GlobalTick, HeadId, RunId, WorldlineId, WorldlineTick, WriterHeadKey};
macro_rules! assert_logical_counter_boundaries {
($ty:ty) => {{
assert_eq!(<$ty>::ZERO.as_u64(), 0);
assert_eq!(<$ty>::MAX.as_u64(), u64::MAX);
assert_eq!(<$ty>::from_raw(41).checked_add(1).unwrap().as_u64(), 42);
assert_eq!(<$ty>::MAX.checked_add(1), None);
assert_eq!(<$ty>::from_raw(42).checked_sub(1).unwrap().as_u64(), 41);
assert_eq!(<$ty>::ZERO.checked_sub(1), None);
assert_eq!(<$ty>::from_raw(7).checked_increment().unwrap().as_u64(), 8);
assert_eq!(<$ty>::MAX.checked_increment(), None);
}};
}
#[test]
fn worldline_tick_checked_arithmetic_boundaries() {
assert_logical_counter_boundaries!(WorldlineTick);
}
#[test]
fn global_tick_checked_arithmetic_boundaries() {
assert_logical_counter_boundaries!(GlobalTick);
}
#[test]
fn run_id_checked_arithmetic_boundaries() {
assert_logical_counter_boundaries!(RunId);
}
#[test]
fn opaque_ids_round_trip_bytes() {
let worldline = WorldlineId::from_bytes([3u8; 32]);
let head = HeadId::from_bytes([7u8; 32]);
assert_eq!(*worldline.as_bytes(), [3u8; 32]);
assert_eq!(*head.as_bytes(), [7u8; 32]);
}
#[test]
fn writer_head_key_preserves_typed_components() {
let key = WriterHeadKey {
worldline_id: WorldlineId::from_bytes([1u8; 32]),
head_id: HeadId::from_bytes([2u8; 32]),
};
assert_eq!(*key.worldline_id.as_bytes(), [1u8; 32]);
assert_eq!(*key.head_id.as_bytes(), [2u8; 32]);
}
}