databuf/types/
enumerate.rs1use crate::*;
2
3impl<T> Encode for Option<T>
4where
5 T: Encode,
6{
7 #[inline]
8 fn encode<const CONFIG: u16>(&self, c: &mut (impl Write + ?Sized)) -> io::Result<()> {
9 match self {
10 Some(val) => {
11 c.write_all(&[1])?;
12 val.encode::<CONFIG>(c)
13 }
14 None => c.write_all(&[0]),
15 }
16 }
17}
18
19impl<'de, T: Decode<'de>> Decode<'de> for Option<T> {
20 #[inline]
21 fn decode<const CONFIG: u16>(r: &mut &'de [u8]) -> Result<Self> {
22 Ok(match bool::decode::<CONFIG>(r)? {
23 true => Some(T::decode::<CONFIG>(r)?),
24 false => None,
25 })
26 }
27}
28
29impl<T, E> Encode for std::result::Result<T, E>
30where
31 T: Encode,
32 E: Encode,
33{
34 #[inline]
35 fn encode<const CONFIG: u16>(&self, c: &mut (impl Write + ?Sized)) -> io::Result<()> {
36 match self {
37 Ok(val) => {
38 c.write_all(&[1])?;
39 val.encode::<CONFIG>(c)
40 }
41 Err(err) => {
42 c.write_all(&[0])?;
43 err.encode::<CONFIG>(c)
44 }
45 }
46 }
47}
48
49impl<'de, T, E> Decode<'de> for std::result::Result<T, E>
50where
51 T: Decode<'de>,
52 E: Decode<'de>,
53{
54 #[inline]
55 fn decode<const CONFIG: u16>(c: &mut &'de [u8]) -> Result<Self> {
56 Ok(match bool::decode::<CONFIG>(c)? {
57 true => Ok(T::decode::<CONFIG>(c)?),
58 false => Err(E::decode::<CONFIG>(c)?),
59 })
60 }
61}