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
use super::type_registry::ShortTypeId;
#[cfg_attr(feature = "serde-serialization", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Debug)]
pub struct MachineID(pub u8);
#[cfg_attr(feature = "serde-serialization", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct RawID {
pub instance_id: u32,
pub type_id: ShortTypeId,
pub machine: MachineID,
pub version: u8,
}
pub fn broadcast_instance_id() -> u32 {
u32::max_value()
}
pub fn broadcast_machine_id() -> MachineID {
MachineID(u8::max_value())
}
impl RawID {
pub fn new(type_id: ShortTypeId, instance_id: u32, machine: MachineID, version: u8) -> Self {
RawID {
type_id,
machine,
version,
instance_id,
}
}
pub fn local_broadcast(&self) -> RawID {
RawID {
instance_id: broadcast_instance_id(),
..*self
}
}
pub fn global_broadcast(&self) -> RawID {
RawID {
machine: broadcast_machine_id(),
..self.local_broadcast()
}
}
pub fn is_broadcast(&self) -> bool {
self.instance_id == broadcast_instance_id()
}
pub fn is_global_broadcast(&self) -> bool {
self.machine == broadcast_machine_id()
}
}
impl ::std::fmt::Debug for RawID {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(
f,
"{}_{}.{}@{}",
u16::from(self.type_id),
self.instance_id,
self.version,
self.machine.0,
)
}
}
pub trait TypedID: Copy + Clone + Sized + ::std::fmt::Debug + ::std::hash::Hash {
fn as_raw(&self) -> RawID;
unsafe fn from_raw(raw: RawID) -> Self;
}