zenoh_protocol/zenoh/
err.rs1use alloc::vec::Vec;
15
16use zenoh_buffers::ZBuf;
17
18use crate::{common::ZExtUnknown, core::Encoding};
19
20pub mod flag {
40 pub const E: u8 = 1 << 6; pub const Z: u8 = 1 << 7; }
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct Err {
47 pub encoding: Encoding,
48 pub ext_sinfo: Option<ext::SourceInfoType>,
49 #[cfg(feature = "shared-memory")]
50 pub ext_shm: Option<ext::ShmType>,
51 pub ext_unknown: Vec<ZExtUnknown>,
52 pub payload: ZBuf,
53}
54
55pub mod ext {
56 #[cfg(feature = "shared-memory")]
57 use crate::{common::ZExtUnit, zextunit};
58 use crate::{common::ZExtZBuf, zextzbuf};
59
60 pub type SourceInfo = zextzbuf!(0x1, false);
63 pub type SourceInfoType = crate::zenoh::ext::SourceInfoType<{ SourceInfo::ID }>;
64
65 #[cfg(feature = "shared-memory")]
68 pub type Shm = zextunit!(0x2, true);
69 #[cfg(feature = "shared-memory")]
70 pub type ShmType = crate::zenoh::ext::ShmType<{ Shm::ID }>;
71}
72
73impl Err {
74 #[cfg(feature = "test")]
75 #[doc(hidden)]
76 pub fn rand() -> Self {
77 use rand::Rng;
78
79 use crate::common::iext;
80 let mut rng = rand::thread_rng();
81
82 let encoding = Encoding::rand();
83 let ext_sinfo = rng.gen_bool(0.5).then_some(ext::SourceInfoType::rand());
84 #[cfg(feature = "shared-memory")]
85 let ext_shm = rng.gen_bool(0.5).then_some(ext::ShmType::rand());
86 let mut ext_unknown = Vec::new();
87 for _ in 0..rng.gen_range(0..4) {
88 ext_unknown.push(ZExtUnknown::rand2(
89 iext::mid(ext::SourceInfo::ID) + 1,
90 false,
91 ));
92 }
93 let payload = ZBuf::rand(rng.gen_range(0..=64));
94
95 Self {
96 encoding,
97 ext_sinfo,
98 #[cfg(feature = "shared-memory")]
99 ext_shm,
100 ext_unknown,
101 payload,
102 }
103 }
104}