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
use std::fmt::{Display, Formatter, Result};
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NodeId(u8);
impl Display for NodeId {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "NID({})", self.0)
}
}
impl NodeId {
#[inline]
pub fn from_u8(id: u8) -> Self {
Self(id)
}
#[inline]
pub fn to_u8(&self) -> u8 {
self.0
}
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct ServiceId(u32);
impl Display for ServiceId {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "SID({}:{})", self.node_id().0, self.local_service_id().0)
}
}
impl ServiceId {
#[inline]
pub fn from_u32(id: u32) -> Self {
Self(id)
}
#[inline]
pub fn to_u32(&self) -> u32 {
self.0
}
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct LocalServiceId(u32);
impl LocalServiceId {
#[inline]
pub fn from_u32(id: u32) -> Self {
Self(id)
}
#[inline]
pub fn to_u32(&self) -> u32 {
self.0
}
#[inline]
pub fn to_global(self, node_id: NodeId) -> ServiceId {
ServiceId((node_id.0 as u32) << 24 | (self.0) as u32)
}
}
impl Display for LocalServiceId {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "LID({})", self.0)
}
}
impl ServiceId {
#[inline]
pub fn node_id(&self) -> NodeId {
NodeId((self.0 >> 24) as u8)
}
#[inline]
pub fn local_service_id(&self) -> LocalServiceId {
LocalServiceId(self.0 & 0xffffff)
}
}