zenoh_protocol/network/
push.rs1use zenoh_buffers::buffer::Buffer;
2
3#[cfg(feature = "test")]
17use crate::zenoh::Put;
18use crate::{core::WireExpr, zenoh::PushBody};
19
20pub mod flag {
21 pub const N: u8 = 1 << 5; pub const M: u8 = 1 << 6; pub const Z: u8 = 1 << 7; }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct Push {
47 pub wire_expr: WireExpr<'static>,
48 pub ext_qos: ext::QoSType,
49 pub ext_tstamp: Option<ext::TimestampType>,
50 pub ext_nodeid: ext::NodeIdType,
51 pub ext_ts_stack: Option<ext::TsStackType>,
52 pub payload: PushBody,
53}
54
55pub mod ext {
56 use crate::{zextz64, zextzbuf};
57
58 pub type QoS = zextz64!(0x1, false);
59 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
60
61 pub type Timestamp = zextzbuf!(0x2, false);
62 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
63
64 pub type NodeId = zextz64!(0x3, true);
65 pub type NodeIdType = crate::network::ext::NodeIdType<{ NodeId::ID }>;
66
67 pub type TsStack = zextzbuf!(0x7, false);
68 pub type TsStackType = crate::network::timestamp_stack::TsStackType<{ TsStack::ID }>;
69}
70
71impl Push {
72 pub fn payload_size(&self) -> usize {
73 match &self.payload {
74 PushBody::Put(p) => {
75 p.payload.len() + p.ext_attachment.as_ref().map_or(0, |a| a.buffer.len())
76 }
77 PushBody::Del(d) => d.ext_attachment.as_ref().map_or(0, |a| a.buffer.len()),
78 }
79 }
80
81 #[cfg(feature = "test")]
82 #[doc(hidden)]
83 pub fn rand() -> Self {
84 use rand::Rng;
85
86 let mut rng = rand::thread_rng();
87 let wire_expr = WireExpr::rand();
88 let payload = PushBody::rand();
89 let ext_qos = ext::QoSType::rand();
90 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
91 let ext_nodeid = ext::NodeIdType::rand();
92 let ext_ts_stack = rng.gen_bool(0.5).then(ext::TsStackType::rand);
93
94 Self {
95 wire_expr,
96 payload,
97 ext_tstamp,
98 ext_qos,
99 ext_nodeid,
100 ext_ts_stack,
101 }
102 }
103}
104
105impl From<PushBody> for Push {
106 fn from(value: PushBody) -> Self {
107 Self {
108 wire_expr: WireExpr::empty(),
109 ext_qos: ext::QoSType::DEFAULT,
110 ext_tstamp: None,
111 ext_nodeid: ext::NodeIdType::DEFAULT,
112 ext_ts_stack: None,
113 payload: value,
114 }
115 }
116}
117
118#[cfg(feature = "test")]
119impl From<Put> for Push {
120 fn from(value: Put) -> Self {
121 PushBody::from(value).into()
122 }
123}
124
125#[cfg(feature = "test")]
126impl From<Vec<u8>> for Push {
127 fn from(value: Vec<u8>) -> Self {
128 Self::from(Put {
129 payload: value.into(),
130 ..Put::default()
131 })
132 }
133}