zenoh_protocol/zenoh/
put.rs1use alloc::vec::Vec;
15
16use uhlc::Timestamp;
17use zenoh_buffers::ZBuf;
18
19use crate::{common::ZExtUnknown, core::Encoding};
20
21pub mod flag {
43 pub const T: u8 = 1 << 5; pub const E: u8 = 1 << 6; pub const Z: u8 = 1 << 7; }
47
48#[derive(Debug, Default, Clone, PartialEq, Eq)]
49pub struct Put {
50 pub timestamp: Option<Timestamp>,
51 pub encoding: Encoding,
52 pub ext_sinfo: Option<ext::SourceInfoType>,
53 pub ext_attachment: Option<ext::AttachmentType>,
54 #[cfg(feature = "shared-memory")]
55 pub ext_shm: Option<ext::ShmType>,
56 pub ext_unknown: Vec<ZExtUnknown>,
57 pub payload: ZBuf,
58}
59
60pub mod ext {
61 #[cfg(feature = "shared-memory")]
62 use crate::{common::ZExtUnit, zextunit};
63 use crate::{common::ZExtZBuf, zextzbuf};
64
65 pub type SourceInfo = zextzbuf!(0x1, false);
68 pub type SourceInfoType = crate::zenoh::ext::SourceInfoType<{ SourceInfo::ID }>;
69
70 #[cfg(feature = "shared-memory")]
73 pub type Shm = zextunit!(0x2, true);
74 #[cfg(feature = "shared-memory")]
75 pub type ShmType = crate::zenoh::ext::ShmType<{ Shm::ID }>;
76
77 pub type Attachment = zextzbuf!(0x3, false);
79 pub type AttachmentType = crate::zenoh::ext::AttachmentType<{ Attachment::ID }>;
80}
81
82impl Put {
83 #[cfg(feature = "test")]
84 #[doc(hidden)]
85 pub fn rand() -> Self {
86 use rand::Rng;
87
88 use crate::{common::iext, core::ZenohIdProto};
89 let mut rng = rand::thread_rng();
90
91 let timestamp = rng.gen_bool(0.5).then_some({
92 let time = uhlc::NTP64(rng.gen());
93 let id = uhlc::ID::try_from(ZenohIdProto::rand().to_le_bytes()).unwrap();
94 Timestamp::new(time, id)
95 });
96 let encoding = Encoding::rand();
97 let ext_sinfo = rng.gen_bool(0.5).then_some(ext::SourceInfoType::rand());
98 #[cfg(feature = "shared-memory")]
99 let ext_shm = rng.gen_bool(0.5).then_some(ext::ShmType::rand());
100 let ext_attachment = rng.gen_bool(0.5).then_some(ext::AttachmentType::rand());
101 let mut ext_unknown = Vec::new();
102 for _ in 0..rng.gen_range(0..4) {
103 ext_unknown.push(ZExtUnknown::rand2(
104 iext::mid(ext::Attachment::ID) + 1,
105 false,
106 ));
107 }
108 let payload = ZBuf::rand(rng.gen_range(1..=64));
109
110 Self {
111 timestamp,
112 encoding,
113 ext_sinfo,
114 #[cfg(feature = "shared-memory")]
115 ext_shm,
116 ext_attachment,
117 ext_unknown,
118 payload,
119 }
120 }
121}