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 payload: PushBody,
52}
53
54pub mod ext {
55 use crate::{
56 common::{ZExtZ64, ZExtZBuf},
57 zextz64, zextzbuf,
58 };
59
60 pub type QoS = zextz64!(0x1, false);
61 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
62
63 pub type Timestamp = zextzbuf!(0x2, false);
64 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
65
66 pub type NodeId = zextz64!(0x3, true);
67 pub type NodeIdType = crate::network::ext::NodeIdType<{ NodeId::ID }>;
68}
69
70impl Push {
71 pub fn payload_size(&self) -> usize {
72 match &self.payload {
73 PushBody::Put(p) => {
74 p.payload.len() + p.ext_attachment.as_ref().map_or(0, |a| a.buffer.len())
75 }
76 PushBody::Del(d) => d.ext_attachment.as_ref().map_or(0, |a| a.buffer.len()),
77 }
78 }
79
80 #[cfg(feature = "test")]
81 #[doc(hidden)]
82 pub fn rand() -> Self {
83 use rand::Rng;
84
85 let mut rng = rand::thread_rng();
86 let wire_expr = WireExpr::rand();
87 let payload = PushBody::rand();
88 let ext_qos = ext::QoSType::rand();
89 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
90 let ext_nodeid = ext::NodeIdType::rand();
91
92 Self {
93 wire_expr,
94 payload,
95 ext_tstamp,
96 ext_qos,
97 ext_nodeid,
98 }
99 }
100}
101
102impl From<PushBody> for Push {
103 fn from(value: PushBody) -> Self {
104 Self {
105 wire_expr: WireExpr::empty(),
106 ext_qos: ext::QoSType::DEFAULT,
107 ext_tstamp: None,
108 ext_nodeid: ext::NodeIdType::DEFAULT,
109 payload: value,
110 }
111 }
112}
113
114#[cfg(feature = "test")]
115impl From<Put> for Push {
116 fn from(value: Put) -> Self {
117 PushBody::from(value).into()
118 }
119}
120
121#[cfg(feature = "test")]
122impl From<Vec<u8>> for Push {
123 fn from(value: Vec<u8>) -> Self {
124 Self::from(Put {
125 payload: value.into(),
126 ..Put::default()
127 })
128 }
129}