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
//! Entity identity and allocation via generational keys.
use serde::{Deserialize, Serialize};
slotmap::new_key_type! {
/// Universal entity identifier used across all component storages.
/// Serialize/Deserialize provided by slotmap's `serde` feature.
pub struct EntityId;
}
/// Generates a typed newtype wrapper around [`EntityId`].
///
/// Each wrapper is `#[repr(transparent)]` with a public inner field for
/// convenient internal access via `.0`, and delegates `Display` to
/// `EntityId`'s `Debug` (since slotmap keys do not implement `Display`).
macro_rules! typed_entity_id {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
#[repr(transparent)]
pub struct $name(pub EntityId);
impl $name {
/// Returns the inner [`EntityId`].
#[inline]
pub const fn entity(self) -> EntityId {
self.0
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({:?})", stringify!($name), self.0)
}
}
impl From<EntityId> for $name {
#[inline]
fn from(id: EntityId) -> Self {
Self(id)
}
}
impl Default for $name {
fn default() -> Self {
Self(EntityId::default())
}
}
};
}
typed_entity_id! {
/// Typed wrapper around [`EntityId`] for elevator entities.
ElevatorId
}
typed_entity_id! {
/// Typed wrapper around [`EntityId`] for rider entities.
RiderId
}