zenoh_codec/core/
wire_expr.rs1use alloc::string::String;
15
16use zenoh_buffers::{
17 reader::{DidntRead, Reader},
18 writer::{DidntWrite, Writer},
19};
20use zenoh_protocol::{
21 core::{ExprId, ExprLen, WireExpr},
22 network::Mapping,
23};
24
25use crate::{core::Zenoh080Bounded, RCodec, WCodec, Zenoh080, Zenoh080Condition};
26
27impl<W> WCodec<&WireExpr<'_>, &mut W> for Zenoh080
28where
29 W: Writer,
30{
31 type Output = Result<(), DidntWrite>;
32
33 #[inline(always)]
34 fn write(self, writer: &mut W, x: &WireExpr<'_>) -> Self::Output {
35 let WireExpr {
36 scope,
37 suffix,
38 mapping: _,
39 } = x;
40
41 let zodec = Zenoh080Bounded::<ExprId>::new();
42 zodec.write(&mut *writer, *scope)?;
43
44 if x.has_suffix() {
45 #[cold]
46 fn write_suffix<W: Writer>(writer: &mut W, suffix: &str) -> Result<(), DidntWrite> {
47 let zodec = Zenoh080Bounded::<ExprLen>::new();
48 zodec.write(&mut *writer, suffix)
49 }
50 write_suffix(writer, suffix)?;
51 }
52 Ok(())
53 }
54}
55
56impl<R> RCodec<WireExpr<'static>, &mut R> for Zenoh080Condition
57where
58 R: Reader,
59{
60 type Error = DidntRead;
61
62 #[inline(always)]
63 fn read(self, reader: &mut R) -> Result<WireExpr<'static>, Self::Error> {
64 let zodec = Zenoh080Bounded::<ExprId>::new();
65 let scope: ExprId = zodec.read(&mut *reader)?;
66
67 let suffix: String = if self.condition {
68 #[cold]
69 fn read_suffix<R: Reader>(reader: &mut R) -> Result<String, DidntRead> {
70 let zodec = Zenoh080Bounded::<ExprLen>::new();
71 zodec.read(&mut *reader)
72 }
73 read_suffix(reader)?
74 } else {
75 String::new()
76 };
77 Ok(WireExpr {
78 scope,
79 suffix: suffix.into(),
80 mapping: Mapping::DEFAULT,
81 })
82 }
83}