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#[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 pub uuid: ObjectUuid,
33
34 pub cookie: ObjectCookie,
36}
37
38impl ObjectId {
39 pub const NIL: Self = Self::new(ObjectUuid::NIL, ObjectCookie::NIL);
41
42 pub const fn new(uuid: ObjectUuid, cookie: ObjectCookie) -> Self {
44 Self { uuid, cookie }
45 }
46
47 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#[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 pub const NIL: Self = Self(Uuid::nil());
107
108 #[cfg(feature = "new-v4-ids")]
117 pub fn new_v4() -> Self {
118 Self(Uuid::new_v4())
119 }
120
121 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#[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 pub const NIL: Self = Self(Uuid::nil());
229
230 #[cfg(feature = "new-v4-ids")]
239 pub fn new_v4() -> Self {
240 Self(Uuid::new_v4())
241 }
242
243 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#[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 pub object_id: ObjectId,
345
346 pub uuid: ServiceUuid,
348
349 pub cookie: ServiceCookie,
351}
352
353impl ServiceId {
354 pub const NIL: Self = Self::new(ObjectId::NIL, ServiceUuid::NIL, ServiceCookie::NIL);
356
357 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 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#[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 pub const NIL: Self = Self(Uuid::nil());
426
427 #[cfg(feature = "new-v4-ids")]
436 pub fn new_v4() -> Self {
437 Self(Uuid::new_v4())
438 }
439
440 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#[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 pub const NIL: Self = Self(Uuid::nil());
548
549 #[cfg(feature = "new-v4-ids")]
558 pub fn new_v4() -> Self {
559 Self(Uuid::new_v4())
560 }
561
562 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#[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 pub const NIL: Self = Self(Uuid::nil());
660
661 #[cfg(feature = "new-v4-ids")]
670 pub fn new_v4() -> Self {
671 Self(Uuid::new_v4())
672 }
673
674 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#[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 pub const NIL: Self = Self(Uuid::nil());
772
773 #[cfg(feature = "new-v4-ids")]
782 pub fn new_v4() -> Self {
783 Self(Uuid::new_v4())
784 }
785
786 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#[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 pub const NIL: Self = Self(Uuid::nil());
824
825 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}