aldrin_core/ids/
service_id.rs

1use super::{ObjectId, ServiceCookie, ServiceUuid};
2#[cfg(feature = "introspection")]
3use crate::introspection::{ir, Introspectable, LexicalId, References};
4use crate::tags::{self, PrimaryTag};
5use crate::{Deserialize, DeserializeError, Deserializer, Serialize, SerializeError, Serializer};
6
7/// Id of a service.
8///
9/// A [`ServiceId`] consists of three parts:
10/// - An [`ObjectId`], identifying the associated object on the bus
11/// - A [`ServiceUuid`], identifying the service of the object
12/// - A [`ServiceCookie`], a random UUID chosen by the broker
13///
14/// It is important to point out, that when a service is destroyed and later created again with the
15/// same [`ServiceUuid`], then the [`ServiceCookie`] and consequently the [`ServiceId`] will be
16/// different.
17#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
18#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
19#[cfg_attr(
20    feature = "serde",
21    derive(serde::Serialize, serde::Deserialize),
22    serde(rename_all = "kebab-case")
23)]
24pub struct ServiceId {
25    /// Id of the associated object.
26    pub object_id: ObjectId,
27
28    /// UUID of the service.
29    pub uuid: ServiceUuid,
30
31    /// Cookie of the service.
32    pub cookie: ServiceCookie,
33}
34
35impl ServiceId {
36    /// Nil `ServiceId` (all zeros).
37    pub const NIL: Self = Self::new(ObjectId::NIL, ServiceUuid::NIL, ServiceCookie::NIL);
38
39    /// Creates a new [`ServiceId`] from an [`ObjectId`], a [`ServiceUuid`] and a [`ServiceCookie`].
40    pub const fn new(object_id: ObjectId, uuid: ServiceUuid, cookie: ServiceCookie) -> Self {
41        Self {
42            object_id,
43            uuid,
44            cookie,
45        }
46    }
47
48    /// Checks if the id is nil (all zeros).
49    pub const fn is_nil(self) -> bool {
50        self.object_id.is_nil() && self.uuid.is_nil() && self.cookie.is_nil()
51    }
52}
53
54impl PrimaryTag for ServiceId {
55    type Tag = tags::ServiceId;
56}
57
58impl Serialize<tags::ServiceId> for ServiceId {
59    fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
60        serializer.serialize_service_id(self)
61    }
62}
63
64impl Serialize<tags::ServiceId> for &ServiceId {
65    fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
66        serializer.serialize(*self)
67    }
68}
69
70impl Deserialize<tags::ServiceId> for ServiceId {
71    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
72        deserializer.deserialize_service_id()
73    }
74}
75
76#[cfg(feature = "introspection")]
77impl Introspectable for ServiceId {
78    fn layout() -> ir::LayoutIr {
79        ir::BuiltInTypeIr::ServiceId.into()
80    }
81
82    fn lexical_id() -> LexicalId {
83        LexicalId::SERVICE_ID
84    }
85
86    fn add_references(_references: &mut References) {}
87}