1use crate::{de::DEController, misc::Either};
2
3pub trait Encode<DEC>
5where
6 DEC: DEController,
7{
8 fn encode(&self, ew: &mut DEC::EncodeWrapper<'_, '_, '_>) -> Result<(), DEC::Error>;
10
11 #[inline]
13 fn is_null(&self) -> bool {
14 false
15 }
16}
17
18impl Encode<()> for u32 {
19 #[inline]
20 fn encode(&self, _: &mut ()) -> Result<(), crate::Error> {
21 Ok(())
22 }
23}
24
25impl Encode<()> for &str {
26 #[inline]
27 fn encode(&self, _: &mut ()) -> Result<(), crate::Error> {
28 Ok(())
29 }
30}
31
32impl<DEC, T> Encode<DEC> for &T
33where
34 DEC: DEController,
35 T: Encode<DEC>,
36{
37 #[inline]
38 fn encode(&self, ew: &mut DEC::EncodeWrapper<'_, '_, '_>) -> Result<(), DEC::Error> {
39 (**self).encode(ew)
40 }
41
42 #[inline]
43 fn is_null(&self) -> bool {
44 (**self).is_null()
45 }
46}
47
48impl<DEC, T> Encode<DEC> for &mut T
49where
50 DEC: DEController,
51 T: Encode<DEC>,
52{
53 #[inline]
54 fn encode(&self, ew: &mut DEC::EncodeWrapper<'_, '_, '_>) -> Result<(), DEC::Error> {
55 (**self).encode(ew)
56 }
57
58 #[inline]
59 fn is_null(&self) -> bool {
60 (**self).is_null()
61 }
62}
63
64impl<DEC, L, R> Encode<DEC> for Either<L, R>
65where
66 DEC: DEController,
67 L: Encode<DEC>,
68 R: Encode<DEC>,
69{
70 #[inline]
71 fn encode(&self, ew: &mut DEC::EncodeWrapper<'_, '_, '_>) -> Result<(), DEC::Error> {
72 match self {
73 Self::Left(left) => left.encode(ew),
74 Self::Right(right) => right.encode(ew),
75 }
76 }
77
78 #[inline]
79 fn is_null(&self) -> bool {
80 match self {
81 Self::Left(left) => left.is_null(),
82 Self::Right(right) => right.is_null(),
83 }
84 }
85}
86
87impl<DEC> Encode<DEC> for &dyn Encode<DEC>
88where
89 DEC: DEController,
90{
91 #[inline]
92 fn encode(&self, ew: &mut DEC::EncodeWrapper<'_, '_, '_>) -> Result<(), DEC::Error> {
93 (**self).encode(ew)
94 }
95
96 #[inline]
97 fn is_null(&self) -> bool {
98 (**self).is_null()
99 }
100}
101
102impl<DEC, T> Encode<DEC> for Option<T>
103where
104 DEC: DEController,
105 T: Encode<DEC>,
106{
107 #[inline]
108 fn encode(&self, ew: &mut DEC::EncodeWrapper<'_, '_, '_>) -> Result<(), DEC::Error> {
109 match self {
110 None => Ok(()),
111 Some(elem) => elem.encode(ew),
112 }
113 }
114
115 #[inline]
116 fn is_null(&self) -> bool {
117 self.is_none()
118 }
119}