Skip to main content

wyrd/foundation/
ids.rs

1//! Dense runtime identifiers after validate/bind (D-id-space).
2//!
3//! These are not engine `Entity` handles. Author graphs use string knot and
4//! host-path names; bind interns them into the compact ids here so host sample
5//! and apply never look up strings on the hot path.
6
7/// Dense knot index after validate/bind.
8#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
9pub struct KnotId(u16);
10
11/// Port within a knot's closed catalog table (D-port-schema).
12#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
13pub struct PortSlot(u8);
14
15/// Optional diagnostics / tooling id (not used on the settle hot path).
16#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
17pub struct ThreadId(u16);
18
19/// Host-owned PRNG seed for `Random` knots (mixed with weave id at bind).
20#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
21pub struct Seed(pub u64);
22
23/// Core time: discrete tick only (`dt` stays host-side).
24#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
25pub struct HostTime {
26    /// Discrete frame index passed to [`Runtime::begin_frame`](crate::runtime_impl::bind::Runtime::begin_frame).
27    pub tick: u64,
28}
29
30macro_rules! dense_id {
31    ($ty:ident, $raw:ty) => {
32        impl $ty {
33            /// Return the compact numeric representation.
34            pub const fn get(self) -> $raw {
35                self.0
36            }
37        }
38
39        impl TryFrom<usize> for $ty {
40            type Error = core::num::TryFromIntError;
41
42            fn try_from(value: usize) -> Result<Self, Self::Error> {
43                Ok(Self(<$raw>::try_from(value)?))
44            }
45        }
46
47        impl From<$ty> for usize {
48            fn from(value: $ty) -> Self {
49                usize::from(value.0)
50            }
51        }
52    };
53}
54
55dense_id!(KnotId, u16);
56dense_id!(PortSlot, u8);
57dense_id!(ThreadId, u16);
58
59impl PortSlot {
60    /// Construct a slot from a catalog-sized `u8`; the raw field remains private.
61    pub const fn new(value: u8) -> Self {
62        Self(value)
63    }
64}