open_dis_rust/common/
entity_id.rs1use super::simulation_address::SimulationAddress;
8use bytes::{Buf, BufMut, BytesMut};
9use serde::{Deserialize, Serialize};
10
11#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
12pub struct EntityId {
14 pub simulation_address: SimulationAddress,
16 pub entity_id: u16,
18}
19
20impl EntityId {
21 #[must_use]
22 pub fn new(site_id: u16, application_id: u16, entity_id: u16) -> Self {
23 EntityId {
24 simulation_address: SimulationAddress::new(site_id, application_id),
25 entity_id,
26 }
27 }
28
29 #[must_use]
30 pub fn default(entity_id: u16) -> Self {
31 EntityId {
32 simulation_address: SimulationAddress::default(),
33 entity_id,
34 }
35 }
36
37 pub fn serialize(&self, buf: &mut BytesMut) {
38 self.simulation_address.serialize(buf);
39 buf.put_u16(self.entity_id);
40 }
41
42 pub fn decode(buf: &mut BytesMut) -> EntityId {
43 EntityId {
44 simulation_address: EntityId::decode_simulation_address(buf),
45 entity_id: buf.get_u16(),
46 }
47 }
48
49 fn decode_simulation_address(buf: &mut BytesMut) -> SimulationAddress {
50 SimulationAddress {
51 site_id: buf.get_u16(),
52 application_id: buf.get_u16(),
53 }
54 }
55}