aldrin_core/
ids.rs

1use crate::deserialize_key::DeserializeKey;
2use crate::error::{DeserializeError, SerializeError};
3#[cfg(feature = "introspection")]
4use crate::introspection::{
5    BuiltInType, Introspectable, KeyType, KeyTypeOf, Layout, LexicalId, References,
6};
7use crate::serialize_key::SerializeKey;
8use crate::value_deserializer::{Deserialize, Deserializer};
9use crate::value_serializer::{AsSerializeArg, Serialize, Serializer};
10use std::fmt;
11use std::str::FromStr;
12use uuid::{Error as UuidError, Uuid};
13
14/// Id of an object.
15///
16/// [`ObjectId`s][Self] consist of two parts:
17/// - An [`ObjectUuid`], identifying the object on the bus
18/// - An [`ObjectCookie`], a random UUID chosen by the broker
19///
20/// It is important to point out, that when an object is destroyed and later created again with the
21/// same [`ObjectUuid`], then the [`ObjectCookie`] and consequently the [`ObjectId`] will be
22/// different.
23#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
24#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
25#[cfg_attr(
26    feature = "serde",
27    derive(serde::Serialize, serde::Deserialize),
28    serde(rename_all = "kebab-case")
29)]
30pub struct ObjectId {
31    /// UUID of the object.
32    pub uuid: ObjectUuid,
33
34    /// Cookie of the object.
35    pub cookie: ObjectCookie,
36}
37
38impl ObjectId {
39    /// Nil `ObjectId` (all zeros).
40    pub const NIL: Self = Self::new(ObjectUuid::NIL, ObjectCookie::NIL);
41
42    /// Creates a new [`ObjectId`] from an [`ObjectUuid`] and [`ObjectCookie`].
43    pub const fn new(uuid: ObjectUuid, cookie: ObjectCookie) -> Self {
44        Self { uuid, cookie }
45    }
46
47    /// Checks if the id is nil (all zeros).
48    pub const fn is_nil(self) -> bool {
49        self.uuid.is_nil() && self.cookie.is_nil()
50    }
51}
52
53impl Serialize for ObjectId {
54    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
55        serializer.serialize_object_id(*self);
56        Ok(())
57    }
58}
59
60impl Deserialize for ObjectId {
61    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
62        deserializer.deserialize_object_id()
63    }
64}
65
66impl AsSerializeArg for ObjectId {
67    type SerializeArg<'a> = Self;
68
69    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
70    where
71        Self: 'a,
72    {
73        *self
74    }
75}
76
77#[cfg(feature = "introspection")]
78impl Introspectable for ObjectId {
79    fn layout() -> Layout {
80        BuiltInType::ObjectId.into()
81    }
82
83    fn lexical_id() -> LexicalId {
84        LexicalId::OBJECT_ID
85    }
86
87    fn add_references(_references: &mut References) {}
88}
89
90/// UUID of an object.
91///
92/// [`ObjectUuid`s](Self) are chosen by the user when creating an object and must be unique among
93/// all objects on the bus.
94#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
95#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
96#[cfg_attr(
97    feature = "serde",
98    derive(serde::Serialize, serde::Deserialize),
99    serde(transparent)
100)]
101#[repr(transparent)]
102pub struct ObjectUuid(pub Uuid);
103
104impl ObjectUuid {
105    /// Nil `ObjectUuid` (all zeros).
106    pub const NIL: Self = Self(Uuid::nil());
107
108    /// Creates an [`ObjectUuid`] with a random v4 UUID.
109    ///
110    /// # Examples
111    ///
112    /// ```
113    /// # use aldrin_core::ObjectUuid;
114    /// let object_uuid = ObjectUuid::new_v4();
115    /// ```
116    #[cfg(feature = "new-v4-ids")]
117    pub fn new_v4() -> Self {
118        Self(Uuid::new_v4())
119    }
120
121    /// Checks if the id is nil (all zeros).
122    pub const fn is_nil(self) -> bool {
123        self.0.is_nil()
124    }
125}
126
127impl Serialize for ObjectUuid {
128    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
129        serializer.serialize_uuid(self.0);
130        Ok(())
131    }
132}
133
134impl Deserialize for ObjectUuid {
135    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
136        deserializer.deserialize_uuid().map(Self)
137    }
138}
139
140impl AsSerializeArg for ObjectUuid {
141    type SerializeArg<'a> = Self;
142
143    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
144    where
145        Self: 'a,
146    {
147        *self
148    }
149}
150
151#[cfg(feature = "introspection")]
152impl Introspectable for ObjectUuid {
153    fn layout() -> Layout {
154        BuiltInType::Uuid.into()
155    }
156
157    fn lexical_id() -> LexicalId {
158        LexicalId::UUID
159    }
160
161    fn add_references(_references: &mut References) {}
162}
163
164impl SerializeKey for ObjectUuid {
165    type Impl<'a> = Uuid;
166
167    fn as_impl(&self) -> Self::Impl<'_> {
168        self.0
169    }
170}
171
172impl DeserializeKey for ObjectUuid {
173    type Impl = Uuid;
174
175    fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
176        Ok(Self(key))
177    }
178}
179
180#[cfg(feature = "introspection")]
181impl KeyTypeOf for ObjectUuid {
182    const KEY_TYPE: KeyType = KeyType::Uuid;
183}
184
185impl From<Uuid> for ObjectUuid {
186    fn from(uuid: Uuid) -> Self {
187        Self(uuid)
188    }
189}
190
191impl From<ObjectUuid> for Uuid {
192    fn from(uuid: ObjectUuid) -> Self {
193        uuid.0
194    }
195}
196
197impl fmt::Display for ObjectUuid {
198    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199        self.0.fmt(f)
200    }
201}
202
203impl FromStr for ObjectUuid {
204    type Err = UuidError;
205
206    fn from_str(s: &str) -> Result<Self, UuidError> {
207        s.parse().map(Self)
208    }
209}
210
211/// Cookie of an object.
212///
213/// [`ObjectCookie`s](Self) are chosen by the broker when creating an object. They ensure that
214/// objects, created and destroyed over time with the same [`ObjectUuid`], can still be
215/// distinguished.
216#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
217#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
218#[cfg_attr(
219    feature = "serde",
220    derive(serde::Serialize, serde::Deserialize),
221    serde(transparent)
222)]
223#[repr(transparent)]
224pub struct ObjectCookie(pub Uuid);
225
226impl ObjectCookie {
227    /// Nil `ObjectCookie` (all zeros).
228    pub const NIL: Self = Self(Uuid::nil());
229
230    /// Creates an [`ObjectCookie`] with a random v4 UUID.
231    ///
232    /// # Examples
233    ///
234    /// ```
235    /// # use aldrin_core::ObjectCookie;
236    /// let object_cookie = ObjectCookie::new_v4();
237    /// ```
238    #[cfg(feature = "new-v4-ids")]
239    pub fn new_v4() -> Self {
240        Self(Uuid::new_v4())
241    }
242
243    /// Checks if the id is nil (all zeros).
244    pub const fn is_nil(self) -> bool {
245        self.0.is_nil()
246    }
247}
248
249impl Serialize for ObjectCookie {
250    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
251        serializer.serialize_uuid(self.0);
252        Ok(())
253    }
254}
255
256impl Deserialize for ObjectCookie {
257    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
258        deserializer.deserialize_uuid().map(Self)
259    }
260}
261
262impl AsSerializeArg for ObjectCookie {
263    type SerializeArg<'a> = Self;
264
265    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
266    where
267        Self: 'a,
268    {
269        *self
270    }
271}
272
273#[cfg(feature = "introspection")]
274impl Introspectable for ObjectCookie {
275    fn layout() -> Layout {
276        BuiltInType::Uuid.into()
277    }
278
279    fn lexical_id() -> LexicalId {
280        LexicalId::UUID
281    }
282
283    fn add_references(_references: &mut References) {}
284}
285
286impl SerializeKey for ObjectCookie {
287    type Impl<'a> = Uuid;
288
289    fn as_impl(&self) -> Self::Impl<'_> {
290        self.0
291    }
292}
293
294impl DeserializeKey for ObjectCookie {
295    type Impl = Uuid;
296
297    fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
298        Ok(Self(key))
299    }
300}
301
302#[cfg(feature = "introspection")]
303impl KeyTypeOf for ObjectCookie {
304    const KEY_TYPE: KeyType = KeyType::Uuid;
305}
306
307impl From<Uuid> for ObjectCookie {
308    fn from(cookie: Uuid) -> Self {
309        Self(cookie)
310    }
311}
312
313impl From<ObjectCookie> for Uuid {
314    fn from(cookie: ObjectCookie) -> Self {
315        cookie.0
316    }
317}
318
319impl fmt::Display for ObjectCookie {
320    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
321        self.0.fmt(f)
322    }
323}
324
325/// Id of a service.
326///
327/// A [`ServiceId`] consists of three parts:
328/// - An [`ObjectId`], identifying the associated object on the bus
329/// - A [`ServiceUuid`], identifying the service of the object
330/// - A [`ServiceCookie`], a random UUID chosen by the broker
331///
332/// It is important to point out, that when a service is destroyed and later created again with the
333/// same [`ServiceUuid`], then the [`ServiceCookie`] and consequently the [`ServiceId`] will be
334/// different.
335#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
336#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
337#[cfg_attr(
338    feature = "serde",
339    derive(serde::Serialize, serde::Deserialize),
340    serde(rename_all = "kebab-case")
341)]
342pub struct ServiceId {
343    /// Id of the associated object.
344    pub object_id: ObjectId,
345
346    /// UUID of the service.
347    pub uuid: ServiceUuid,
348
349    /// Cookie of the service.
350    pub cookie: ServiceCookie,
351}
352
353impl ServiceId {
354    /// Nil `ServiceId` (all zeros).
355    pub const NIL: Self = Self::new(ObjectId::NIL, ServiceUuid::NIL, ServiceCookie::NIL);
356
357    /// Creates a new [`ServiceId`] from an [`ObjectId`], a [`ServiceUuid`] and a [`ServiceCookie`].
358    pub const fn new(object_id: ObjectId, uuid: ServiceUuid, cookie: ServiceCookie) -> Self {
359        Self {
360            object_id,
361            uuid,
362            cookie,
363        }
364    }
365
366    /// Checks if the id is nil (all zeros).
367    pub const fn is_nil(self) -> bool {
368        self.object_id.is_nil() && self.uuid.is_nil() && self.cookie.is_nil()
369    }
370}
371
372impl Serialize for ServiceId {
373    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
374        serializer.serialize_service_id(*self);
375        Ok(())
376    }
377}
378
379impl Deserialize for ServiceId {
380    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
381        deserializer.deserialize_service_id()
382    }
383}
384
385impl AsSerializeArg for ServiceId {
386    type SerializeArg<'a> = Self;
387
388    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
389    where
390        Self: 'a,
391    {
392        *self
393    }
394}
395
396#[cfg(feature = "introspection")]
397impl Introspectable for ServiceId {
398    fn layout() -> Layout {
399        BuiltInType::ServiceId.into()
400    }
401
402    fn lexical_id() -> LexicalId {
403        LexicalId::SERVICE_ID
404    }
405
406    fn add_references(_references: &mut References) {}
407}
408
409/// UUID of a service.
410///
411/// [`ServiceUuid`s](Self) are chosen by the user when creating a service and must be unique among
412/// all services of an object.
413#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
414#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
415#[cfg_attr(
416    feature = "serde",
417    derive(serde::Serialize, serde::Deserialize),
418    serde(transparent)
419)]
420#[repr(transparent)]
421pub struct ServiceUuid(pub Uuid);
422
423impl ServiceUuid {
424    /// Nil `ServiceUuid` (all zeros).
425    pub const NIL: Self = Self(Uuid::nil());
426
427    /// Creates a [`ServiceUuid`] with a random v4 UUID.
428    ///
429    /// # Examples
430    ///
431    /// ```
432    /// # use aldrin_core::ServiceUuid;
433    /// let service_uuid = ServiceUuid::new_v4();
434    /// ```
435    #[cfg(feature = "new-v4-ids")]
436    pub fn new_v4() -> Self {
437        Self(Uuid::new_v4())
438    }
439
440    /// Checks if the id is nil (all zeros).
441    pub const fn is_nil(self) -> bool {
442        self.0.is_nil()
443    }
444}
445
446impl Serialize for ServiceUuid {
447    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
448        serializer.serialize_uuid(self.0);
449        Ok(())
450    }
451}
452
453impl Deserialize for ServiceUuid {
454    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
455        deserializer.deserialize_uuid().map(Self)
456    }
457}
458
459impl AsSerializeArg for ServiceUuid {
460    type SerializeArg<'a> = Self;
461
462    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
463    where
464        Self: 'a,
465    {
466        *self
467    }
468}
469
470#[cfg(feature = "introspection")]
471impl Introspectable for ServiceUuid {
472    fn layout() -> Layout {
473        BuiltInType::Uuid.into()
474    }
475
476    fn lexical_id() -> LexicalId {
477        LexicalId::UUID
478    }
479
480    fn add_references(_references: &mut References) {}
481}
482
483impl SerializeKey for ServiceUuid {
484    type Impl<'a> = Uuid;
485
486    fn as_impl(&self) -> Self::Impl<'_> {
487        self.0
488    }
489}
490
491impl DeserializeKey for ServiceUuid {
492    type Impl = Uuid;
493
494    fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
495        Ok(Self(key))
496    }
497}
498
499#[cfg(feature = "introspection")]
500impl KeyTypeOf for ServiceUuid {
501    const KEY_TYPE: KeyType = KeyType::Uuid;
502}
503
504impl From<Uuid> for ServiceUuid {
505    fn from(uuid: Uuid) -> Self {
506        Self(uuid)
507    }
508}
509
510impl From<ServiceUuid> for Uuid {
511    fn from(uuid: ServiceUuid) -> Self {
512        uuid.0
513    }
514}
515
516impl fmt::Display for ServiceUuid {
517    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
518        self.0.fmt(f)
519    }
520}
521
522impl FromStr for ServiceUuid {
523    type Err = UuidError;
524
525    fn from_str(s: &str) -> Result<Self, UuidError> {
526        s.parse().map(Self)
527    }
528}
529
530/// Cookie of a service.
531///
532/// [`ServiceCookie`s](Self) are chosen by the broker when creating a service. They ensure that
533/// services, created and destroyed over time with the same [`ServiceUuid`] and on the same object,
534/// can still be distinguished.
535#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
536#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
537#[cfg_attr(
538    feature = "serde",
539    derive(serde::Serialize, serde::Deserialize),
540    serde(transparent)
541)]
542#[repr(transparent)]
543pub struct ServiceCookie(pub Uuid);
544
545impl ServiceCookie {
546    /// Nil `ServiceCookie` (all zeros).
547    pub const NIL: Self = Self(Uuid::nil());
548
549    /// Creates a [`ServiceCookie`] with a random v4 UUID.
550    ///
551    /// # Examples
552    ///
553    /// ```
554    /// # use aldrin_core::ServiceCookie;
555    /// let service_cookie = ServiceCookie::new_v4();
556    /// ```
557    #[cfg(feature = "new-v4-ids")]
558    pub fn new_v4() -> Self {
559        Self(Uuid::new_v4())
560    }
561
562    /// Checks if the id is nil (all zeros).
563    pub const fn is_nil(self) -> bool {
564        self.0.is_nil()
565    }
566}
567
568impl Serialize for ServiceCookie {
569    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
570        serializer.serialize_uuid(self.0);
571        Ok(())
572    }
573}
574
575impl Deserialize for ServiceCookie {
576    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
577        deserializer.deserialize_uuid().map(Self)
578    }
579}
580
581impl AsSerializeArg for ServiceCookie {
582    type SerializeArg<'a> = Self;
583
584    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
585    where
586        Self: 'a,
587    {
588        *self
589    }
590}
591
592#[cfg(feature = "introspection")]
593impl Introspectable for ServiceCookie {
594    fn layout() -> Layout {
595        BuiltInType::Uuid.into()
596    }
597
598    fn lexical_id() -> LexicalId {
599        LexicalId::UUID
600    }
601
602    fn add_references(_references: &mut References) {}
603}
604
605impl SerializeKey for ServiceCookie {
606    type Impl<'a> = Uuid;
607
608    fn as_impl(&self) -> Self::Impl<'_> {
609        self.0
610    }
611}
612
613impl DeserializeKey for ServiceCookie {
614    type Impl = Uuid;
615
616    fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
617        Ok(Self(key))
618    }
619}
620
621#[cfg(feature = "introspection")]
622impl KeyTypeOf for ServiceCookie {
623    const KEY_TYPE: KeyType = KeyType::Uuid;
624}
625
626impl From<Uuid> for ServiceCookie {
627    fn from(cookie: Uuid) -> Self {
628        Self(cookie)
629    }
630}
631
632impl From<ServiceCookie> for Uuid {
633    fn from(cookie: ServiceCookie) -> Self {
634        cookie.0
635    }
636}
637
638impl fmt::Display for ServiceCookie {
639    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
640        self.0.fmt(f)
641    }
642}
643
644/// Cookie of a channel.
645///
646/// [`ChannelCookie`s](Self) are chosen by the broker when creating a channel.
647#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
648#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
649#[cfg_attr(
650    feature = "serde",
651    derive(serde::Serialize, serde::Deserialize),
652    serde(transparent)
653)]
654#[repr(transparent)]
655pub struct ChannelCookie(pub Uuid);
656
657impl ChannelCookie {
658    /// Nil `ChannelCookie` (all zeros).
659    pub const NIL: Self = Self(Uuid::nil());
660
661    /// Creates a [`ChannelCookie`] with a random v4 UUID.
662    ///
663    /// # Examples
664    ///
665    /// ```
666    /// # use aldrin_core::ChannelCookie;
667    /// let channel_cookie = ChannelCookie::new_v4();
668    /// ```
669    #[cfg(feature = "new-v4-ids")]
670    pub fn new_v4() -> Self {
671        Self(Uuid::new_v4())
672    }
673
674    /// Checks if the id is nil (all zeros).
675    pub const fn is_nil(self) -> bool {
676        self.0.is_nil()
677    }
678}
679
680impl Serialize for ChannelCookie {
681    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
682        serializer.serialize_uuid(self.0);
683        Ok(())
684    }
685}
686
687impl Deserialize for ChannelCookie {
688    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
689        deserializer.deserialize_uuid().map(Self)
690    }
691}
692
693impl AsSerializeArg for ChannelCookie {
694    type SerializeArg<'a> = Self;
695
696    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
697    where
698        Self: 'a,
699    {
700        *self
701    }
702}
703
704#[cfg(feature = "introspection")]
705impl Introspectable for ChannelCookie {
706    fn layout() -> Layout {
707        BuiltInType::Uuid.into()
708    }
709
710    fn lexical_id() -> LexicalId {
711        LexicalId::UUID
712    }
713
714    fn add_references(_references: &mut References) {}
715}
716
717impl SerializeKey for ChannelCookie {
718    type Impl<'a> = Uuid;
719
720    fn as_impl(&self) -> Self::Impl<'_> {
721        self.0
722    }
723}
724
725impl DeserializeKey for ChannelCookie {
726    type Impl = Uuid;
727
728    fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
729        Ok(Self(key))
730    }
731}
732
733#[cfg(feature = "introspection")]
734impl KeyTypeOf for ChannelCookie {
735    const KEY_TYPE: KeyType = KeyType::Uuid;
736}
737
738impl From<Uuid> for ChannelCookie {
739    fn from(cookie: Uuid) -> Self {
740        Self(cookie)
741    }
742}
743
744impl From<ChannelCookie> for Uuid {
745    fn from(cookie: ChannelCookie) -> Self {
746        cookie.0
747    }
748}
749
750impl fmt::Display for ChannelCookie {
751    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
752        self.0.fmt(f)
753    }
754}
755
756/// Cookie of a bus listener.
757///
758/// [`BusListenerCookie`s](Self) are chosen by the broker when creating a bus listener.
759#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
760#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
761#[cfg_attr(
762    feature = "serde",
763    derive(serde::Serialize, serde::Deserialize),
764    serde(transparent)
765)]
766#[repr(transparent)]
767pub struct BusListenerCookie(pub Uuid);
768
769impl BusListenerCookie {
770    /// Nil `BusListenerCookie` (all zeros).
771    pub const NIL: Self = Self(Uuid::nil());
772
773    /// Creates a [`BusListenerCookie`] with a random v4 UUID.
774    ///
775    /// # Examples
776    ///
777    /// ```
778    /// # use aldrin_core::BusListenerCookie;
779    /// let bus_listener_cookie = BusListenerCookie::new_v4();
780    /// ```
781    #[cfg(feature = "new-v4-ids")]
782    pub fn new_v4() -> Self {
783        Self(Uuid::new_v4())
784    }
785
786    /// Checks if the id is nil (all zeros).
787    pub const fn is_nil(self) -> bool {
788        self.0.is_nil()
789    }
790}
791
792impl From<Uuid> for BusListenerCookie {
793    fn from(cookie: Uuid) -> Self {
794        Self(cookie)
795    }
796}
797
798impl From<BusListenerCookie> for Uuid {
799    fn from(cookie: BusListenerCookie) -> Self {
800        cookie.0
801    }
802}
803
804impl fmt::Display for BusListenerCookie {
805    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
806        self.0.fmt(f)
807    }
808}
809
810/// Introspection type id of a service, struct or enum.
811#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
812#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
813#[cfg_attr(
814    feature = "serde",
815    derive(serde::Serialize, serde::Deserialize),
816    serde(transparent)
817)]
818#[repr(transparent)]
819pub struct TypeId(pub Uuid);
820
821impl TypeId {
822    /// Nil `TypeId` (all zeros).
823    pub const NIL: Self = Self(Uuid::nil());
824
825    /// Checks if the id is nil (all zeros).
826    pub const fn is_nil(self) -> bool {
827        self.0.is_nil()
828    }
829}
830
831impl Serialize for TypeId {
832    fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
833        serializer.serialize_uuid(self.0);
834        Ok(())
835    }
836}
837
838impl Deserialize for TypeId {
839    fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
840        deserializer.deserialize_uuid().map(Self)
841    }
842}
843
844impl AsSerializeArg for TypeId {
845    type SerializeArg<'a> = Self;
846
847    fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
848    where
849        Self: 'a,
850    {
851        *self
852    }
853}
854
855#[cfg(feature = "introspection")]
856impl Introspectable for TypeId {
857    fn layout() -> Layout {
858        BuiltInType::Uuid.into()
859    }
860
861    fn lexical_id() -> LexicalId {
862        LexicalId::UUID
863    }
864
865    fn add_references(_references: &mut References) {}
866}
867
868impl SerializeKey for TypeId {
869    type Impl<'a> = Uuid;
870
871    fn as_impl(&self) -> Self::Impl<'_> {
872        self.0
873    }
874}
875
876impl DeserializeKey for TypeId {
877    type Impl = Uuid;
878
879    fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
880        Ok(Self(key))
881    }
882}
883
884#[cfg(feature = "introspection")]
885impl KeyTypeOf for TypeId {
886    const KEY_TYPE: KeyType = KeyType::Uuid;
887}
888
889impl From<Uuid> for TypeId {
890    fn from(uuid: Uuid) -> Self {
891        Self(uuid)
892    }
893}
894
895impl From<TypeId> for Uuid {
896    fn from(id: TypeId) -> Self {
897        id.0
898    }
899}
900
901impl fmt::Display for TypeId {
902    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
903        self.0.fmt(f)
904    }
905}
906
907impl FromStr for TypeId {
908    type Err = UuidError;
909
910    fn from_str(s: &str) -> Result<Self, UuidError> {
911        s.parse().map(Self)
912    }
913}