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