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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use naia_serde::{BitReader, BitWrite, ConstBitLength, Serde, SerdeErr, UnsignedVariableInteger};
use crate::{EntityDoesNotExistError, GlobalEntity, LocalEntityAndGlobalEntityConverter};
/// A connection-local entity ID that records whether the entity is host-owned or remote-owned.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum OwnedLocalEntity {
/// Entity whose authoritative state originates on this side of the connection.
Host {
/// Wire-level entity ID within the host pool.
id: u16,
/// `true` if this entity belongs to the static pool.
is_static: bool,
},
/// Entity whose authoritative state originates on the far side of the connection.
Remote {
/// Wire-level entity ID within the remote pool.
id: u16,
/// `true` if this entity belongs to the static pool.
is_static: bool,
},
}
impl OwnedLocalEntity {
/// Creates a dynamic `Host` variant from a [`HostEntity`].
pub fn new_host(id: HostEntity) -> Self {
Self::Host { id: id.value(), is_static: false }
}
/// Creates a dynamic `Host` variant from a raw `u16` ID.
pub fn new_host_dynamic(id: u16) -> Self {
Self::Host { id, is_static: false }
}
/// Creates a static `Host` variant from a raw `u16` ID.
pub fn new_host_static(id: u16) -> Self {
Self::Host { id, is_static: true }
}
/// Creates a `Remote` variant from a [`RemoteEntity`], preserving its `is_static` flag.
pub fn new_remote(id: RemoteEntity) -> Self {
Self::Remote { id: id.value(), is_static: id.is_static() }
}
/// Creates a dynamic `Remote` variant from a raw `u16` ID.
pub fn new_remote_dynamic(id: u16) -> Self {
Self::Remote { id, is_static: false }
}
/// Creates a static `Remote` variant from a raw `u16` ID.
pub fn new_remote_static(id: u16) -> Self {
Self::Remote { id, is_static: true }
}
/// Returns `true` if this is a `Host` variant.
pub fn is_host(&self) -> bool {
match self {
Self::Host { .. } => true,
Self::Remote { .. } => false,
}
}
/// Returns `true` if this is a `Remote` variant.
pub fn is_remote(&self) -> bool {
!self.is_host()
}
/// Returns `true` if this entity belongs to the static pool.
pub fn is_static(&self) -> bool {
match self {
Self::Host { is_static, .. } => *is_static,
Self::Remote { is_static, .. } => *is_static,
}
}
/// Returns the raw `u16` wire ID for this entity, regardless of variant.
pub fn id(&self) -> u16 {
match self {
Self::Host { id, .. } | Self::Remote { id, .. } => *id,
}
}
/// Serializes this entity into the bit stream, writing host/remote flag, static flag, and ID.
pub fn ser(&self, writer: &mut dyn BitWrite) {
match self {
Self::Host { id, is_static } => {
true.ser(writer);
is_static.ser(writer);
UnsignedVariableInteger::<7>::new(*id).ser(writer);
}
Self::Remote { id, is_static } => {
false.ser(writer);
is_static.ser(writer);
UnsignedVariableInteger::<7>::new(*id).ser(writer);
}
}
}
/// Deserializes an `OwnedLocalEntity` from the bit stream.
pub fn de(reader: &mut BitReader) -> Result<Self, SerdeErr> {
let is_host = bool::de(reader)?;
let is_static = bool::de(reader)?;
let id = UnsignedVariableInteger::<7>::de(reader)?.get() as u16;
if is_host {
Ok(Self::Host { id, is_static })
} else {
Ok(Self::Remote { id, is_static })
}
}
/// Returns the encoded bit length of this entity.
pub fn bit_length(&self) -> u32 {
match self {
Self::Host { id, .. } | Self::Remote { id, .. } => {
bool::const_bit_length() // is_host
+ bool::const_bit_length() // is_static
+ UnsignedVariableInteger::<7>::new(*id).bit_length()
}
}
}
pub(crate) fn convert_to_global(
&self,
converter: &dyn LocalEntityAndGlobalEntityConverter,
) -> Result<GlobalEntity, EntityDoesNotExistError> {
match self {
OwnedLocalEntity::Host { id, is_static: true } => {
converter.static_host_entity_to_global_entity(&HostEntity::new(*id))
}
OwnedLocalEntity::Host { id, is_static: false } => {
converter.host_entity_to_global_entity(&HostEntity::new(*id))
}
OwnedLocalEntity::Remote { id, is_static } => {
let remote = if *is_static {
RemoteEntity::new_static(*id)
} else {
RemoteEntity::new(*id)
};
converter.remote_entity_to_global_entity(&remote)
}
}
}
pub(crate) fn take_remote(&self) -> RemoteEntity {
let OwnedLocalEntity::Remote { id, is_static } = self else {
panic!("Expected RemoteEntity")
};
if *is_static { RemoteEntity::new_static(*id) } else { RemoteEntity::new(*id) }
}
pub(crate) fn to_reversed(self) -> OwnedLocalEntity {
match self {
OwnedLocalEntity::Host { id, is_static } => OwnedLocalEntity::Remote { id, is_static },
OwnedLocalEntity::Remote { id, is_static } => OwnedLocalEntity::Host { id, is_static },
}
}
/// Extracts the inner [`HostEntity`], panicking if this is a `Remote` variant.
pub fn host(&self) -> HostEntity {
match self {
OwnedLocalEntity::Host { id, is_static } => {
if *is_static { HostEntity::new_static(*id) } else { HostEntity::new(*id) }
}
OwnedLocalEntity::Remote { .. } => panic!("Expected OwnedLocalEntity::Host, found OwnedLocalEntity::Remote"),
}
}
/// Extracts the inner [`RemoteEntity`], panicking if this is a `Host` variant.
pub fn remote(&self) -> RemoteEntity {
match self {
OwnedLocalEntity::Remote { id, is_static } => {
if *is_static { RemoteEntity::new_static(*id) } else { RemoteEntity::new(*id) }
}
OwnedLocalEntity::Host { .. } => panic!("Expected OwnedLocalEntity::Remote, found OwnedLocalEntity::Host"),
}
}
}
/// A host-assigned entity ID. Carries `is_static` so that static and dynamic
/// entities from pools that both start at 0 remain distinct as hash map keys.
#[derive(Copy, Eq, Hash, Clone, PartialEq, Debug)]
pub struct HostEntity {
id: u16,
is_static: bool,
}
impl HostEntity {
/// Creates a dynamic host entity with the given `id`.
pub fn new(id: u16) -> Self {
Self { id, is_static: false }
}
/// Creates a static host entity with the given `id`.
pub fn new_static(id: u16) -> Self {
Self { id, is_static: true }
}
/// Returns the raw `u16` wire ID.
pub fn value(&self) -> u16 {
self.id
}
/// Returns `true` if this entity is from the static pool.
pub fn is_static(&self) -> bool {
self.is_static
}
/// Converts this host entity into the equivalent [`RemoteEntity`] with the same ID and static flag.
pub fn to_remote(self) -> RemoteEntity {
if self.is_static { RemoteEntity::new_static(self.id) } else { RemoteEntity::new(self.id) }
}
/// Serializes the entity ID into the bit stream (ID only; authority messages use dynamic entities).
pub fn ser(&self, writer: &mut dyn BitWrite) {
UnsignedVariableInteger::<7>::new(self.value()).ser(writer);
}
/// Deserializes a dynamic host entity from the bit stream.
pub fn de(reader: &mut BitReader) -> Result<Self, SerdeErr> {
let value = UnsignedVariableInteger::<7>::de(reader)?.get();
Ok(Self { id: value as u16, is_static: false }) // authority messages only use dynamic entities
}
/// Returns the encoded bit length of this entity's ID.
pub fn bit_length(&self) -> u32 {
UnsignedVariableInteger::<7>::new(self.value()).bit_length()
}
/// Wraps this entity as an `OwnedLocalEntity::Host`, preserving the `is_static` flag.
pub fn copy_to_owned(&self) -> OwnedLocalEntity {
OwnedLocalEntity::Host { id: self.value(), is_static: self.is_static }
}
/// Wraps this entity as a dynamic `OwnedLocalEntity::Host` (forcing `is_static = false`).
pub fn copy_to_owned_dynamic(&self) -> OwnedLocalEntity {
OwnedLocalEntity::Host { id: self.value(), is_static: false }
}
/// Wraps this entity as a static `OwnedLocalEntity::Host` (forcing `is_static = true`).
pub fn copy_to_owned_static(&self) -> OwnedLocalEntity {
OwnedLocalEntity::Host { id: self.value(), is_static: true }
}
}
/// A connection-local entity ID assigned by the remote peer, used on the receiving side of replication.
#[derive(Copy, Eq, Hash, Clone, PartialEq, Debug)]
pub struct RemoteEntity {
id: u16,
is_static: bool,
}
impl RemoteEntity {
/// Creates a dynamic remote entity with the given `id`.
pub fn new(id: u16) -> Self {
Self { id, is_static: false }
}
/// Creates a static remote entity with the given `id`.
pub fn new_static(id: u16) -> Self {
Self { id, is_static: true }
}
/// Returns the raw `u16` wire ID.
pub fn value(&self) -> u16 {
self.id
}
/// Returns `true` if this entity is from the static pool.
pub fn is_static(&self) -> bool {
self.is_static
}
/// Converts this remote entity into the equivalent [`HostEntity`] with the same ID and static flag.
pub fn to_host(self) -> HostEntity {
if self.is_static { HostEntity::new_static(self.id) } else { HostEntity::new(self.id) }
}
/// Serializes only the entity ID into the bit stream (authority messages always use dynamic entities).
pub fn ser(&self, writer: &mut dyn BitWrite) {
UnsignedVariableInteger::<7>::new(self.value()).ser(writer);
}
/// Deserializes a dynamic remote entity from the bit stream.
pub fn de(reader: &mut BitReader) -> Result<Self, SerdeErr> {
let value = UnsignedVariableInteger::<7>::de(reader)?.get();
Ok(Self { id: value as u16, is_static: false })
}
/// Wraps this entity as an `OwnedLocalEntity::Remote`, preserving the `is_static` flag.
pub fn copy_to_owned(&self) -> OwnedLocalEntity {
OwnedLocalEntity::Remote { id: self.id, is_static: self.is_static }
}
}