zenoh-protocol 1.9.0

Internal crate for zenoh.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
pub mod declare;
pub mod interest;
pub mod oam;
pub mod push;
pub mod request;
pub mod response;

use core::fmt;

pub use declare::{
    Declare, DeclareBody, DeclareFinal, DeclareKeyExpr, DeclareQueryable, DeclareSubscriber,
    DeclareToken, UndeclareKeyExpr, UndeclareQueryable, UndeclareSubscriber, UndeclareToken,
};
pub use interest::Interest;
pub use oam::Oam;
pub use push::Push;
pub use request::{AtomicRequestId, Request, RequestId};
pub use response::{Response, ResponseFinal};

use crate::core::{CongestionControl, Priority, Reliability, WireExpr};
#[cfg(feature = "shared-memory")]
use crate::zenoh::{PushBody, RequestBody, ResponseBody};

pub mod id {
    // WARNING: it's crucial that these IDs do NOT collide with the IDs
    //          defined in `crate::transport::id`.
    pub const OAM: u8 = 0x1f;
    pub const DECLARE: u8 = 0x1e;
    pub const PUSH: u8 = 0x1d;
    pub const REQUEST: u8 = 0x1c;
    pub const RESPONSE: u8 = 0x1b;
    pub const RESPONSE_FINAL: u8 = 0x1a;
    pub const INTEREST: u8 = 0x19;
}

#[repr(u8)]
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Mapping {
    #[default]
    Receiver = 0,
    Sender = 1,
}

impl Mapping {
    pub const DEFAULT: Self = Self::Receiver;

    #[cfg(feature = "test")]
    #[doc(hidden)]
    pub fn rand() -> Self {
        use rand::Rng;

        let mut rng = rand::thread_rng();
        if rng.gen_bool(0.5) {
            Mapping::Sender
        } else {
            Mapping::Receiver
        }
    }
}

// Zenoh messages at zenoh-network level
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NetworkBody {
    Push(Push),
    Request(Request),
    Response(Response),
    ResponseFinal(ResponseFinal),
    Interest(Interest),
    Declare(Declare),
    OAM(Oam),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum NetworkBodyRef<'a> {
    Push(&'a Push),
    Request(&'a Request),
    Response(&'a Response),
    ResponseFinal(&'a ResponseFinal),
    Interest(&'a Interest),
    Declare(&'a Declare),
    OAM(&'a Oam),
}

#[derive(Debug, PartialEq, Eq)]
pub enum NetworkBodyMut<'a> {
    Push(&'a mut Push),
    Request(&'a mut Request),
    Response(&'a mut Response),
    ResponseFinal(&'a mut ResponseFinal),
    Interest(&'a mut Interest),
    Declare(&'a mut Declare),
    OAM(&'a mut Oam),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NetworkMessage {
    pub body: NetworkBody,
    pub reliability: Reliability,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct NetworkMessageRef<'a> {
    pub body: NetworkBodyRef<'a>,
    pub reliability: Reliability,
}

#[derive(Debug, PartialEq, Eq)]
pub struct NetworkMessageMut<'a> {
    pub body: NetworkBodyMut<'a>,
    pub reliability: Reliability,
}

pub trait NetworkMessageExt {
    #[doc(hidden)]
    fn body(&self) -> NetworkBodyRef<'_>;

    #[doc(hidden)]
    fn reliability(&self) -> Reliability;

    #[inline]
    fn is_reliable(&self) -> bool {
        self.reliability() == Reliability::Reliable
    }

    #[inline]
    fn is_express(&self) -> bool {
        match self.body() {
            NetworkBodyRef::Push(msg) => msg.ext_qos.is_express(),
            NetworkBodyRef::Request(msg) => msg.ext_qos.is_express(),
            NetworkBodyRef::Response(msg) => msg.ext_qos.is_express(),
            NetworkBodyRef::ResponseFinal(msg) => msg.ext_qos.is_express(),
            NetworkBodyRef::Interest(msg) => msg.ext_qos.is_express(),
            NetworkBodyRef::Declare(msg) => msg.ext_qos.is_express(),
            NetworkBodyRef::OAM(msg) => msg.ext_qos.is_express(),
        }
    }

    #[inline]
    fn congestion_control(&self) -> CongestionControl {
        match self.body() {
            NetworkBodyRef::Push(msg) => msg.ext_qos.get_congestion_control(),
            NetworkBodyRef::Request(msg) => msg.ext_qos.get_congestion_control(),
            NetworkBodyRef::Response(msg) => msg.ext_qos.get_congestion_control(),
            NetworkBodyRef::ResponseFinal(msg) => msg.ext_qos.get_congestion_control(),
            NetworkBodyRef::Interest(msg) => msg.ext_qos.get_congestion_control(),
            NetworkBodyRef::Declare(msg) => msg.ext_qos.get_congestion_control(),
            NetworkBodyRef::OAM(msg) => msg.ext_qos.get_congestion_control(),
        }
    }

    #[inline]
    #[cfg(feature = "shared-memory")]
    fn is_shm(&self) -> bool {
        match self.body() {
            NetworkBodyRef::Push(Push { payload, .. }) => match payload {
                PushBody::Put(p) => p.ext_shm.is_some(),
                PushBody::Del(_) => false,
            },
            NetworkBodyRef::Request(Request { payload, .. }) => match payload {
                RequestBody::Query(b) => b.ext_body.as_ref().is_some_and(|b| b.ext_shm.is_some()),
            },
            NetworkBodyRef::Response(Response { payload, .. }) => match payload {
                ResponseBody::Reply(b) => match &b.payload {
                    PushBody::Put(p) => p.ext_shm.is_some(),
                    PushBody::Del(_) => false,
                },
                ResponseBody::Err(e) => e.ext_shm.is_some(),
            },
            NetworkBodyRef::ResponseFinal(_)
            | NetworkBodyRef::Interest(_)
            | NetworkBodyRef::Declare(_)
            | NetworkBodyRef::OAM(_) => false,
        }
    }

    #[inline]
    fn is_droppable(&self) -> bool {
        !self.is_reliable() || self.congestion_control() == CongestionControl::Drop
    }

    #[inline]
    fn priority(&self) -> Priority {
        match self.body() {
            NetworkBodyRef::Push(msg) => msg.ext_qos.get_priority(),
            NetworkBodyRef::Request(msg) => msg.ext_qos.get_priority(),
            NetworkBodyRef::Response(msg) => msg.ext_qos.get_priority(),
            NetworkBodyRef::ResponseFinal(msg) => msg.ext_qos.get_priority(),
            NetworkBodyRef::Interest(msg) => msg.ext_qos.get_priority(),
            NetworkBodyRef::Declare(msg) => msg.ext_qos.get_priority(),
            NetworkBodyRef::OAM(msg) => msg.ext_qos.get_priority(),
        }
    }

    #[inline]
    fn wire_expr(&self) -> Option<&WireExpr<'_>> {
        match &self.body() {
            NetworkBodyRef::Push(m) => Some(&m.wire_expr),
            NetworkBodyRef::Request(m) => Some(&m.wire_expr),
            NetworkBodyRef::Response(m) => Some(&m.wire_expr),
            NetworkBodyRef::ResponseFinal(_) => None,
            NetworkBodyRef::Interest(m) => m.wire_expr.as_ref(),
            NetworkBodyRef::Declare(m) => match &m.body {
                DeclareBody::DeclareKeyExpr(m) => Some(&m.wire_expr),
                DeclareBody::UndeclareKeyExpr(_) => None,
                DeclareBody::DeclareSubscriber(m) => Some(&m.wire_expr),
                DeclareBody::UndeclareSubscriber(m) => Some(&m.ext_wire_expr.wire_expr),
                DeclareBody::DeclareQueryable(m) => Some(&m.wire_expr),
                DeclareBody::UndeclareQueryable(m) => Some(&m.ext_wire_expr.wire_expr),
                DeclareBody::DeclareToken(m) => Some(&m.wire_expr),
                DeclareBody::UndeclareToken(m) => Some(&m.ext_wire_expr.wire_expr),
                DeclareBody::DeclareFinal(_) => None,
            },
            NetworkBodyRef::OAM(_) => None,
        }
    }

    #[inline]
    fn payload_size(&self) -> Option<usize> {
        match &self.body() {
            NetworkBodyRef::Push(p) => Some(p.payload_size()),
            NetworkBodyRef::Request(r) => Some(r.payload_size()),
            NetworkBodyRef::Response(r) => Some(r.payload_size()),
            NetworkBodyRef::ResponseFinal(_)
            | NetworkBodyRef::Interest(_)
            | NetworkBodyRef::Declare(_)
            | NetworkBodyRef::OAM(_) => None,
        }
    }

    #[inline]
    fn as_ref(&self) -> NetworkMessageRef<'_> {
        NetworkMessageRef {
            body: self.body(),
            reliability: self.reliability(),
        }
    }

    #[inline]
    fn to_owned(&self) -> NetworkMessage {
        NetworkMessage {
            body: match self.body() {
                NetworkBodyRef::Push(msg) => NetworkBody::Push(msg.clone()),
                NetworkBodyRef::Request(msg) => NetworkBody::Request(msg.clone()),
                NetworkBodyRef::Response(msg) => NetworkBody::Response(msg.clone()),
                NetworkBodyRef::ResponseFinal(msg) => NetworkBody::ResponseFinal(msg.clone()),
                NetworkBodyRef::Interest(msg) => NetworkBody::Interest(msg.clone()),
                NetworkBodyRef::Declare(msg) => NetworkBody::Declare(msg.clone()),
                NetworkBodyRef::OAM(msg) => NetworkBody::OAM(msg.clone()),
            },
            reliability: self.reliability(),
        }
    }
}

impl<M: NetworkMessageExt> NetworkMessageExt for &M {
    fn body(&self) -> NetworkBodyRef<'_> {
        (**self).body()
    }

    fn reliability(&self) -> Reliability {
        (**self).reliability()
    }
}

impl<M: NetworkMessageExt> NetworkMessageExt for &mut M {
    fn body(&self) -> NetworkBodyRef<'_> {
        (**self).body()
    }

    fn reliability(&self) -> Reliability {
        (**self).reliability()
    }
}

impl NetworkMessageExt for NetworkMessage {
    fn body(&self) -> NetworkBodyRef<'_> {
        match &self.body {
            NetworkBody::Push(body) => NetworkBodyRef::Push(body),
            NetworkBody::Request(body) => NetworkBodyRef::Request(body),
            NetworkBody::Response(body) => NetworkBodyRef::Response(body),
            NetworkBody::ResponseFinal(body) => NetworkBodyRef::ResponseFinal(body),
            NetworkBody::Interest(body) => NetworkBodyRef::Interest(body),
            NetworkBody::Declare(body) => NetworkBodyRef::Declare(body),
            NetworkBody::OAM(body) => NetworkBodyRef::OAM(body),
        }
    }

    fn reliability(&self) -> Reliability {
        self.reliability
    }
}

impl NetworkMessageExt for NetworkMessageRef<'_> {
    fn body(&self) -> NetworkBodyRef<'_> {
        self.body
    }

    fn reliability(&self) -> Reliability {
        self.reliability
    }
}

impl NetworkMessageExt for NetworkMessageMut<'_> {
    fn body(&self) -> NetworkBodyRef<'_> {
        match &self.body {
            NetworkBodyMut::Push(body) => NetworkBodyRef::Push(body),
            NetworkBodyMut::Request(body) => NetworkBodyRef::Request(body),
            NetworkBodyMut::Response(body) => NetworkBodyRef::Response(body),
            NetworkBodyMut::ResponseFinal(body) => NetworkBodyRef::ResponseFinal(body),
            NetworkBodyMut::Interest(body) => NetworkBodyRef::Interest(body),
            NetworkBodyMut::Declare(body) => NetworkBodyRef::Declare(body),
            NetworkBodyMut::OAM(body) => NetworkBodyRef::OAM(body),
        }
    }

    fn reliability(&self) -> Reliability {
        self.reliability
    }
}

impl NetworkMessage {
    #[cfg(feature = "test")]
    #[doc(hidden)]
    pub fn rand() -> Self {
        use rand::Rng;

        let mut rng = rand::thread_rng();

        let body = match rng.gen_range(0..6) {
            0 => NetworkBody::Push(Push::rand()),
            1 => NetworkBody::Request(Request::rand()),
            2 => NetworkBody::Response(Response::rand()),
            3 => NetworkBody::ResponseFinal(ResponseFinal::rand()),
            4 => NetworkBody::Declare(Declare::rand()),
            5 => NetworkBody::OAM(Oam::rand()),
            _ => unreachable!(),
        };

        body.into()
    }

    #[inline]
    pub fn as_mut(&mut self) -> NetworkMessageMut<'_> {
        let body = match &mut self.body {
            NetworkBody::Push(body) => NetworkBodyMut::Push(body),
            NetworkBody::Request(body) => NetworkBodyMut::Request(body),
            NetworkBody::Response(body) => NetworkBodyMut::Response(body),
            NetworkBody::ResponseFinal(body) => NetworkBodyMut::ResponseFinal(body),
            NetworkBody::Interest(body) => NetworkBodyMut::Interest(body),
            NetworkBody::Declare(body) => NetworkBodyMut::Declare(body),
            NetworkBody::OAM(body) => NetworkBodyMut::OAM(body),
        };
        NetworkMessageMut {
            body,
            reliability: self.reliability,
        }
    }
}

impl NetworkMessageMut<'_> {
    #[inline]
    pub fn as_mut(&mut self) -> NetworkMessageMut<'_> {
        let body = match &mut self.body {
            NetworkBodyMut::Push(body) => NetworkBodyMut::Push(body),
            NetworkBodyMut::Request(body) => NetworkBodyMut::Request(body),
            NetworkBodyMut::Response(body) => NetworkBodyMut::Response(body),
            NetworkBodyMut::ResponseFinal(body) => NetworkBodyMut::ResponseFinal(body),
            NetworkBodyMut::Interest(body) => NetworkBodyMut::Interest(body),
            NetworkBodyMut::Declare(body) => NetworkBodyMut::Declare(body),
            NetworkBodyMut::OAM(body) => NetworkBodyMut::OAM(body),
        };
        NetworkMessageMut {
            body,
            reliability: self.reliability,
        }
    }
}

impl fmt::Display for NetworkMessageRef<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.body {
            NetworkBodyRef::OAM(_) => write!(f, "OAM"),
            NetworkBodyRef::Push(_) => write!(f, "Push"),
            NetworkBodyRef::Request(_) => write!(f, "Request"),
            NetworkBodyRef::Response(_) => write!(f, "Response"),
            NetworkBodyRef::ResponseFinal(_) => write!(f, "ResponseFinal"),
            NetworkBodyRef::Interest(_) => write!(f, "Interest"),
            NetworkBodyRef::Declare(_) => write!(f, "Declare"),
        }
    }
}

impl fmt::Display for NetworkMessage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

impl fmt::Display for NetworkMessageMut<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

impl From<NetworkBody> for NetworkMessage {
    #[inline]
    fn from(body: NetworkBody) -> Self {
        Self {
            body,
            reliability: Reliability::DEFAULT,
        }
    }
}

#[cfg(feature = "test")]
impl From<Push> for NetworkMessage {
    fn from(push: Push) -> Self {
        NetworkBody::Push(push).into()
    }
}

// Extensions
pub mod ext {
    use core::fmt;

    use crate::{
        common::{imsg, ZExtZ64},
        core::{CongestionControl, EntityId, Priority, ZenohIdProto},
    };

    /// ```text
    ///  7 6 5 4 3 2 1 0
    /// +-+-+-+-+-+-+-+-+
    /// |Z|0_1|    ID   |
    /// +-+-+-+---------+
    /// %0|r|F|E|D|prio %
    /// +---------------+
    ///
    /// - prio: Priority class
    /// - D:    Don't drop. Don't drop the message for congestion control.
    /// - E:    Express. Don't batch this message.
    /// - F:    Don't drop the first message for congestion control.
    /// - r:  Reserved
    /// ```
    #[repr(transparent)]
    #[derive(Clone, Copy, PartialEq, Eq)]
    pub struct QoSType<const ID: u8> {
        inner: u8,
    }

    impl<const ID: u8> QoSType<{ ID }> {
        const P_MASK: u8 = 0b00000111;
        const D_FLAG: u8 = 0b00001000;
        const E_FLAG: u8 = 0b00010000;
        const F_FLAG: u8 = 0b00100000;

        pub const DEFAULT: Self = Self::new(Priority::DEFAULT, CongestionControl::DEFAULT, false);

        pub const DECLARE: Self =
            Self::new(Priority::Control, CongestionControl::DEFAULT_DECLARE, false);
        pub const INTEREST: Self = Self::new(
            Priority::Control,
            CongestionControl::DEFAULT_INTEREST,
            false,
        );
        pub const PUSH: Self = Self::new(Priority::DEFAULT, CongestionControl::DEFAULT_PUSH, false);
        pub const REQUEST: Self =
            Self::new(Priority::DEFAULT, CongestionControl::DEFAULT_REQUEST, false);
        pub const OAM: Self = Self::new(Priority::Control, CongestionControl::DEFAULT_OAM, false);

        pub const fn new(
            priority: Priority,
            congestion_control: CongestionControl,
            is_express: bool,
        ) -> Self {
            let mut inner = priority as u8;
            match congestion_control {
                CongestionControl::Block => inner |= Self::D_FLAG,
                #[cfg(feature = "unstable")]
                CongestionControl::BlockFirst => inner |= Self::F_FLAG,
                _ => {}
            }
            if is_express {
                inner |= Self::E_FLAG;
            }
            Self { inner }
        }

        pub fn set_priority(&mut self, priority: Priority) {
            self.inner = imsg::set_bitfield(self.inner, priority as u8, Self::P_MASK);
        }

        pub const fn get_priority(&self) -> Priority {
            unsafe { core::mem::transmute(self.inner & Self::P_MASK) }
        }

        pub fn set_congestion_control(&mut self, cctrl: CongestionControl) {
            match cctrl {
                CongestionControl::Block => {
                    self.inner = imsg::set_flag(self.inner, Self::D_FLAG);
                    self.inner = imsg::unset_flag(self.inner, Self::F_FLAG);
                }
                CongestionControl::Drop => {
                    self.inner = imsg::unset_flag(self.inner, Self::D_FLAG);
                    self.inner = imsg::unset_flag(self.inner, Self::F_FLAG);
                }
                #[cfg(feature = "unstable")]
                CongestionControl::BlockFirst => {
                    self.inner = imsg::unset_flag(self.inner, Self::D_FLAG);
                    self.inner = imsg::set_flag(self.inner, Self::F_FLAG);
                }
            }
        }

        pub const fn get_congestion_control(&self) -> CongestionControl {
            match (
                imsg::has_flag(self.inner, Self::D_FLAG),
                imsg::has_flag(self.inner, Self::F_FLAG),
            ) {
                (false, false) => CongestionControl::Drop,
                #[cfg(feature = "unstable")]
                (false, true) => CongestionControl::BlockFirst,
                #[cfg(not(feature = "unstable"))]
                (false, true) => CongestionControl::Drop,
                (true, _) => CongestionControl::Block,
            }
        }

        pub fn set_is_express(&mut self, is_express: bool) {
            match is_express {
                true => self.inner = imsg::set_flag(self.inner, Self::E_FLAG),
                false => self.inner = imsg::unset_flag(self.inner, Self::E_FLAG),
            }
        }

        pub const fn is_express(&self) -> bool {
            imsg::has_flag(self.inner, Self::E_FLAG)
        }

        #[cfg(feature = "test")]
        #[doc(hidden)]
        pub fn rand() -> Self {
            use rand::Rng;
            let mut rng = rand::thread_rng();

            let inner: u8 = rng.gen();
            Self { inner }
        }
    }

    impl<const ID: u8> Default for QoSType<{ ID }> {
        fn default() -> Self {
            Self::new(Priority::DEFAULT, CongestionControl::DEFAULT, false)
        }
    }

    impl<const ID: u8> From<ZExtZ64<{ ID }>> for QoSType<{ ID }> {
        fn from(ext: ZExtZ64<{ ID }>) -> Self {
            Self {
                inner: ext.value as u8,
            }
        }
    }

    impl<const ID: u8> From<QoSType<{ ID }>> for ZExtZ64<{ ID }> {
        fn from(ext: QoSType<{ ID }>) -> Self {
            ZExtZ64::new(ext.inner as u64)
        }
    }

    impl<const ID: u8> fmt::Debug for QoSType<{ ID }> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("QoS")
                .field("priority", &self.get_priority())
                .field("congestion", &self.get_congestion_control())
                .field("express", &self.is_express())
                .finish()
        }
    }

    /// ```text
    ///  7 6 5 4 3 2 1 0
    /// +-+-+-+-+-+-+-+-+
    /// |Z|1_0|    ID   |
    /// +-+-+-+---------+
    /// ~ ts: <u8;z16>  ~
    /// +---------------+
    /// ```
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct TimestampType<const ID: u8> {
        pub timestamp: uhlc::Timestamp,
    }

    impl<const ID: u8> TimestampType<{ ID }> {
        #[cfg(feature = "test")]
        #[doc(hidden)]
        pub fn rand() -> Self {
            use rand::Rng;
            let mut rng = rand::thread_rng();

            let time = uhlc::NTP64(rng.gen());
            let id = uhlc::ID::try_from(ZenohIdProto::rand().to_le_bytes()).unwrap();
            let timestamp = uhlc::Timestamp::new(time, id);
            Self { timestamp }
        }
    }

    /// ```text
    ///  7 6 5 4 3 2 1 0
    /// +-+-+-+-+-+-+-+-+
    /// |Z|0_1|    ID   |
    /// +-+-+-+---------+
    /// %    node_id    %
    /// +---------------+
    /// ```
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct NodeIdType<const ID: u8> {
        pub node_id: u16,
    }

    impl<const ID: u8> NodeIdType<{ ID }> {
        // node_id == 0 means the message has been generated by the node itself
        pub const DEFAULT: Self = Self { node_id: 0 };

        #[cfg(feature = "test")]
        #[doc(hidden)]
        pub fn rand() -> Self {
            use rand::Rng;
            let mut rng = rand::thread_rng();
            let node_id = rng.gen();
            Self { node_id }
        }
    }

    impl<const ID: u8> Default for NodeIdType<{ ID }> {
        fn default() -> Self {
            Self::DEFAULT
        }
    }

    impl<const ID: u8> From<ZExtZ64<{ ID }>> for NodeIdType<{ ID }> {
        fn from(ext: ZExtZ64<{ ID }>) -> Self {
            Self {
                node_id: ext.value as u16,
            }
        }
    }

    impl<const ID: u8> From<NodeIdType<{ ID }>> for ZExtZ64<{ ID }> {
        fn from(ext: NodeIdType<{ ID }>) -> Self {
            ZExtZ64::new(ext.node_id as u64)
        }
    }

    /// ```text
    ///  7 6 5 4 3 2 1 0
    /// +-+-+-+-+-+-+-+-+
    /// |zid_len|X|X|X|X|
    /// +-------+-+-+---+
    /// ~      zid      ~
    /// +---------------+
    /// %      eid      %
    /// +---------------+
    /// ```
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct EntityGlobalIdType<const ID: u8> {
        pub zid: ZenohIdProto,
        pub eid: EntityId,
    }

    impl<const ID: u8> EntityGlobalIdType<{ ID }> {
        #[cfg(feature = "test")]
        #[doc(hidden)]
        pub fn rand() -> Self {
            use rand::Rng;
            let mut rng = rand::thread_rng();

            let zid = ZenohIdProto::rand();
            let eid: EntityId = rng.gen();
            Self { zid, eid }
        }
    }
}