zenoh_codec/core/
mod.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14mod 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
34// [u8; N]
35macro_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
97// &[u8] / Vec<u8> - Bounded
98macro_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                let mut buff = zenoh_buffers::vec::uninit(len);
126                if len != 0 {
127                    reader.read_exact(&mut buff[..])?;
128                }
129                Ok(buff)
130            }
131        }
132    };
133}
134
135vec_impl!(u8);
136vec_impl!(u16);
137vec_impl!(u32);
138vec_impl!(u64);
139vec_impl!(usize);
140
141// &[u8] / Vec<u8>
142impl LCodec<&[u8]> for Zenoh080 {
143    fn w_len(self, x: &[u8]) -> usize {
144        self.w_len(x.len()) + x.len()
145    }
146}
147
148impl<W> WCodec<&[u8], &mut W> for Zenoh080
149where
150    W: Writer,
151{
152    type Output = Result<(), DidntWrite>;
153
154    fn write(self, writer: &mut W, x: &[u8]) -> Self::Output {
155        let zcodec = Zenoh080Bounded::<usize>::new();
156        zcodec.write(&mut *writer, x)
157    }
158}
159
160impl<R> RCodec<Vec<u8>, &mut R> for Zenoh080
161where
162    R: Reader,
163{
164    type Error = DidntRead;
165
166    fn read(self, reader: &mut R) -> Result<Vec<u8>, Self::Error> {
167        let zcodec = Zenoh080Bounded::<usize>::new();
168        zcodec.read(&mut *reader)
169    }
170}
171
172// &str / String - Bounded
173macro_rules! str_impl {
174    ($bound:ty) => {
175        impl<W> WCodec<&str, &mut W> for Zenoh080Bounded<$bound>
176        where
177            W: Writer,
178        {
179            type Output = Result<(), DidntWrite>;
180
181            fn write(self, writer: &mut W, x: &str) -> Self::Output {
182                self.write(&mut *writer, x.as_bytes())
183            }
184        }
185
186        impl<W> WCodec<&String, &mut W> for Zenoh080Bounded<$bound>
187        where
188            W: Writer,
189        {
190            type Output = Result<(), DidntWrite>;
191
192            fn write(self, writer: &mut W, x: &String) -> Self::Output {
193                self.write(&mut *writer, x.as_str())
194            }
195        }
196
197        impl<R> RCodec<String, &mut R> for Zenoh080Bounded<$bound>
198        where
199            R: Reader,
200        {
201            type Error = DidntRead;
202
203            #[allow(clippy::uninit_vec)]
204            fn read(self, reader: &mut R) -> Result<String, Self::Error> {
205                let vec: Vec<u8> = self.read(&mut *reader)?;
206                String::from_utf8(vec).map_err(|_| DidntRead)
207            }
208        }
209    };
210}
211
212str_impl!(u8);
213str_impl!(u16);
214str_impl!(u32);
215str_impl!(u64);
216str_impl!(usize);
217
218// &str / String
219impl LCodec<&str> for Zenoh080 {
220    fn w_len(self, x: &str) -> usize {
221        self.w_len(x.as_bytes())
222    }
223}
224
225impl LCodec<&String> for Zenoh080 {
226    fn w_len(self, x: &String) -> usize {
227        self.w_len(x.as_bytes())
228    }
229}
230
231impl<W> WCodec<&str, &mut W> for Zenoh080
232where
233    W: Writer,
234{
235    type Output = Result<(), DidntWrite>;
236
237    fn write(self, writer: &mut W, x: &str) -> Self::Output {
238        let zcodec = Zenoh080Bounded::<usize>::new();
239        zcodec.write(&mut *writer, x)
240    }
241}
242
243impl<W> WCodec<&String, &mut W> for Zenoh080
244where
245    W: Writer,
246{
247    type Output = Result<(), DidntWrite>;
248
249    fn write(self, writer: &mut W, x: &String) -> Self::Output {
250        let zcodec = Zenoh080Bounded::<usize>::new();
251        zcodec.write(&mut *writer, x)
252    }
253}
254
255impl<R> RCodec<String, &mut R> for Zenoh080
256where
257    R: Reader,
258{
259    type Error = DidntRead;
260
261    fn read(self, reader: &mut R) -> Result<String, Self::Error> {
262        let zcodec = Zenoh080Bounded::<usize>::new();
263        zcodec.read(&mut *reader)
264    }
265}