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, 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 pub fn rand() -> Self {
85 use rand::Rng;
86
87 use crate::{common::iext, core::ZenohIdProto};
88 let mut rng = rand::thread_rng();
89
90 let timestamp = rng.gen_bool(0.5).then_some({
91 let time = uhlc::NTP64(rng.gen());
92 let id = uhlc::ID::try_from(ZenohIdProto::rand().to_le_bytes()).unwrap();
93 Timestamp::new(time, id)
94 });
95 let encoding = Encoding::rand();
96 let ext_sinfo = rng.gen_bool(0.5).then_some(ext::SourceInfoType::rand());
97 #[cfg(feature = "shared-memory")]
98 let ext_shm = rng.gen_bool(0.5).then_some(ext::ShmType::rand());
99 let ext_attachment = rng.gen_bool(0.5).then_some(ext::AttachmentType::rand());
100 let mut ext_unknown = Vec::new();
101 for _ in 0..rng.gen_range(0..4) {
102 ext_unknown.push(ZExtUnknown::rand2(
103 iext::mid(ext::Attachment::ID) + 1,
104 false,
105 ));
106 }
107 let payload = ZBuf::rand(rng.gen_range(1..=64));
108
109 Self {
110 timestamp,
111 encoding,
112 ext_sinfo,
113 #[cfg(feature = "shared-memory")]
114 ext_shm,
115 ext_attachment,
116 ext_unknown,
117 payload,
118 }
119 }
120}