Skip to main content

sectorsync_core/
ids.rs

1//! Strongly typed identifiers used across `SectorSync`.
2
3macro_rules! id_type {
4    ($(#[$meta:meta])* $name:ident, $inner:ty) => {
5        $(#[$meta])*
6        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
7        pub struct $name($inner);
8
9        impl $name {
10            /// Creates a new identifier from its raw representation.
11            pub const fn new(value: $inner) -> Self {
12                Self(value)
13            }
14
15            /// Returns the raw identifier value.
16            pub const fn get(self) -> $inner {
17                self.0
18            }
19        }
20
21        impl From<$inner> for $name {
22            fn from(value: $inner) -> Self {
23                Self::new(value)
24            }
25        }
26    };
27}
28
29id_type!(/// Stable entity identifier visible to embedders.
30EntityId, u64);
31id_type!(/// Connected or simulated client identifier.
32ClientId, u64);
33id_type!(/// Client command identifier.
34CommandId, u64);
35id_type!(/// Station identifier selected by the embedding application.
36StationId, u32);
37id_type!(/// Logical node identifier selected by the embedding application.
38NodeId, u32);
39id_type!(/// World instance identifier.
40InstanceId, u64);
41id_type!(/// Small compiled synchronization policy identifier.
42PolicyId, u16);
43id_type!(/// Registered component identifier.
44ComponentId, u16);
45id_type!(/// Cross-station event identifier used for idempotency.
46EventId, u64);
47id_type!(/// Runtime barrier identifier.
48BarrierId, u64);
49id_type!(/// Fixed simulation tick number.
50Tick, u64);
51id_type!(/// Entity ownership epoch used during handoff.
52OwnerEpoch, u64);
53
54/// Dense, generation-checked handle for station-local entity storage.
55#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
56pub struct EntityHandle {
57    index: u32,
58    generation: u32,
59}
60
61impl EntityHandle {
62    /// Creates a station-local handle.
63    pub const fn new(index: u32, generation: u32) -> Self {
64        Self { index, generation }
65    }
66
67    /// Dense storage index.
68    pub const fn index(self) -> u32 {
69        self.index
70    }
71
72    /// Generation used to detect stale handles.
73    pub const fn generation(self) -> u32 {
74        self.generation
75    }
76}