Skip to main content

elevator_core/
entity.rs

1//! Entity identity and allocation via generational keys.
2
3use serde::{Deserialize, Serialize};
4
5slotmap::new_key_type! {
6    /// Universal entity identifier used across all component storages.
7    /// Serialize/Deserialize provided by slotmap's `serde` feature.
8    pub struct EntityId;
9}
10
11/// Generates a typed newtype wrapper around [`EntityId`].
12///
13/// Each wrapper is `#[repr(transparent)]` with a public inner field for
14/// convenient internal access via `.0`, and delegates `Display` to
15/// `EntityId`'s `Debug` (since slotmap keys do not implement `Display`).
16macro_rules! typed_entity_id {
17    ($(#[$meta:meta])* $name:ident) => {
18        $(#[$meta])*
19        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
20        #[serde(transparent)]
21        #[repr(transparent)]
22        pub struct $name(pub EntityId);
23
24        impl $name {
25            /// Returns the inner [`EntityId`].
26            #[inline]
27            pub const fn entity(self) -> EntityId {
28                self.0
29            }
30        }
31
32        impl std::fmt::Display for $name {
33            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34                write!(f, "{}({:?})", stringify!($name), self.0)
35            }
36        }
37
38        impl From<EntityId> for $name {
39            #[inline]
40            fn from(id: EntityId) -> Self {
41                Self(id)
42            }
43        }
44
45        impl Default for $name {
46            fn default() -> Self {
47                Self(EntityId::default())
48            }
49        }
50    };
51}
52
53typed_entity_id! {
54    /// Typed wrapper around [`EntityId`] for elevator entities.
55    ElevatorId
56}
57
58typed_entity_id! {
59    /// Typed wrapper around [`EntityId`] for rider entities.
60    RiderId
61}