aldrin_core/ids/
object_id.rs

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