1mod encoding;
15mod locator;
16#[cfg(feature = "shared-memory")]
17mod shm;
18mod timestamp;
19mod wire_expr;
20mod zbuf;
21mod zenohid;
22mod zint;
23mod zslice;
24
25use alloc::{string::String, vec::Vec};
26
27use zenoh_buffers::{
28 reader::{DidntRead, Reader},
29 writer::{DidntWrite, Writer},
30};
31
32use crate::{LCodec, RCodec, WCodec, Zenoh080, Zenoh080Bounded};
33
34macro_rules! array_impl {
36 ($n:expr) => {
37 impl<W> WCodec<[u8; $n], &mut W> for Zenoh080
38 where
39 W: Writer,
40 {
41 type Output = Result<(), DidntWrite>;
42
43 fn write(self, writer: &mut W, x: [u8; $n]) -> Self::Output {
44 writer.write_exact(x.as_slice())
45 }
46 }
47
48 impl<W> WCodec<&[u8; $n], &mut W> for Zenoh080
49 where
50 W: Writer,
51 {
52 type Output = Result<(), DidntWrite>;
53
54 fn write(self, writer: &mut W, x: &[u8; $n]) -> Self::Output {
55 self.write(writer, *x)
56 }
57 }
58
59 impl<R> RCodec<[u8; $n], &mut R> for Zenoh080
60 where
61 R: Reader,
62 {
63 type Error = DidntRead;
64
65 fn read(self, reader: &mut R) -> Result<[u8; $n], Self::Error> {
66 let mut x = [0u8; $n];
67 reader.read_exact(&mut x)?;
68 Ok(x)
69 }
70 }
71
72 impl LCodec<[u8; $n]> for Zenoh080 {
73 fn w_len(self, _: [u8; $n]) -> usize {
74 $n
75 }
76 }
77 };
78}
79
80array_impl!(1);
81array_impl!(2);
82array_impl!(3);
83array_impl!(4);
84array_impl!(5);
85array_impl!(6);
86array_impl!(7);
87array_impl!(8);
88array_impl!(9);
89array_impl!(10);
90array_impl!(11);
91array_impl!(12);
92array_impl!(13);
93array_impl!(14);
94array_impl!(15);
95array_impl!(16);
96
97macro_rules! vec_impl {
99 ($bound:ty) => {
100 impl<W> WCodec<&[u8], &mut W> for Zenoh080Bounded<$bound>
101 where
102 W: Writer,
103 {
104 type Output = Result<(), DidntWrite>;
105
106 fn write(self, writer: &mut W, x: &[u8]) -> Self::Output {
107 self.write(&mut *writer, x.len())?;
108 if x.is_empty() {
109 Ok(())
110 } else {
111 writer.write_exact(x)
112 }
113 }
114 }
115
116 impl<R> RCodec<Vec<u8>, &mut R> for Zenoh080Bounded<$bound>
117 where
118 R: Reader,
119 {
120 type Error = DidntRead;
121
122 #[allow(clippy::uninit_vec)]
123 fn read(self, reader: &mut R) -> Result<Vec<u8>, Self::Error> {
124 let len: usize = self.read(&mut *reader)?;
125 if reader.remaining() < len {
126 return Err(DidntRead);
127 }
128 let mut buff = zenoh_buffers::vec::uninit(len);
129 if len != 0 {
130 reader.read_exact(&mut buff[..])?;
131 }
132 Ok(buff)
133 }
134 }
135 };
136}
137
138vec_impl!(u8);
139vec_impl!(u16);
140vec_impl!(u32);
141vec_impl!(u64);
142vec_impl!(usize);
143
144impl LCodec<&[u8]> for Zenoh080 {
146 fn w_len(self, x: &[u8]) -> usize {
147 self.w_len(x.len()) + x.len()
148 }
149}
150
151impl<W> WCodec<&[u8], &mut W> for Zenoh080
152where
153 W: Writer,
154{
155 type Output = Result<(), DidntWrite>;
156
157 fn write(self, writer: &mut W, x: &[u8]) -> Self::Output {
158 let zcodec = Zenoh080Bounded::<usize>::new();
159 zcodec.write(&mut *writer, x)
160 }
161}
162
163impl<R> RCodec<Vec<u8>, &mut R> for Zenoh080
164where
165 R: Reader,
166{
167 type Error = DidntRead;
168
169 fn read(self, reader: &mut R) -> Result<Vec<u8>, Self::Error> {
170 let zcodec = Zenoh080Bounded::<usize>::new();
171 zcodec.read(&mut *reader)
172 }
173}
174
175macro_rules! str_impl {
177 ($bound:ty) => {
178 impl<W> WCodec<&str, &mut W> for Zenoh080Bounded<$bound>
179 where
180 W: Writer,
181 {
182 type Output = Result<(), DidntWrite>;
183
184 fn write(self, writer: &mut W, x: &str) -> Self::Output {
185 self.write(&mut *writer, x.as_bytes())
186 }
187 }
188
189 impl<W> WCodec<&String, &mut W> for Zenoh080Bounded<$bound>
190 where
191 W: Writer,
192 {
193 type Output = Result<(), DidntWrite>;
194
195 fn write(self, writer: &mut W, x: &String) -> Self::Output {
196 self.write(&mut *writer, x.as_str())
197 }
198 }
199
200 impl<R> RCodec<String, &mut R> for Zenoh080Bounded<$bound>
201 where
202 R: Reader,
203 {
204 type Error = DidntRead;
205
206 #[allow(clippy::uninit_vec)]
207 fn read(self, reader: &mut R) -> Result<String, Self::Error> {
208 let vec: Vec<u8> = self.read(&mut *reader)?;
209 String::from_utf8(vec).map_err(|_| DidntRead)
210 }
211 }
212 };
213}
214
215str_impl!(u8);
216str_impl!(u16);
217str_impl!(u32);
218str_impl!(u64);
219str_impl!(usize);
220
221impl LCodec<&str> for Zenoh080 {
223 fn w_len(self, x: &str) -> usize {
224 self.w_len(x.as_bytes())
225 }
226}
227
228impl LCodec<&String> for Zenoh080 {
229 fn w_len(self, x: &String) -> usize {
230 self.w_len(x.as_bytes())
231 }
232}
233
234impl<W> WCodec<&str, &mut W> for Zenoh080
235where
236 W: Writer,
237{
238 type Output = Result<(), DidntWrite>;
239
240 fn write(self, writer: &mut W, x: &str) -> Self::Output {
241 let zcodec = Zenoh080Bounded::<usize>::new();
242 zcodec.write(&mut *writer, x)
243 }
244}
245
246impl<W> WCodec<&String, &mut W> for Zenoh080
247where
248 W: Writer,
249{
250 type Output = Result<(), DidntWrite>;
251
252 fn write(self, writer: &mut W, x: &String) -> Self::Output {
253 let zcodec = Zenoh080Bounded::<usize>::new();
254 zcodec.write(&mut *writer, x)
255 }
256}
257
258impl<R> RCodec<String, &mut R> for Zenoh080
259where
260 R: Reader,
261{
262 type Error = DidntRead;
263
264 fn read(self, reader: &mut R) -> Result<String, Self::Error> {
265 let zcodec = Zenoh080Bounded::<usize>::new();
266 zcodec.read(&mut *reader)
267 }
268}