summaryrefslogtreecommitdiff
path: root/src/codec/encode.rs
blob: c8d1c7bf7cbb7281c64f6995fa631133c90d2b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Encoding: writing protocol values into a byte stream.
use crate::codec::error::Error;
use std::{io::Write, sync::Arc};

macro_rules! impl_encode {
    ($t: ty) => {
        impl<Ctx> Encode<Ctx> for $t {
            /// write number using big endian
            fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> {
                let size = buffer.write(&self.to_be_bytes())?;
                Ok(size)
            }
        }
    };
}

/// Write a value into a byte stream
pub trait Encode<Ctx> {
    fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error>;
}

impl<Ctx> Encode<Ctx> for bool {
    fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> {
        let size = buffer.write(&[*self as u8])?;
        Ok(size)
    }
}

impl<Ctx, T: Encode<Ctx>> Encode<Ctx> for Option<T> {
    /// Encode `T` if Some(T) or do nothing if None
    fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error> {
        match self {
            Some(val) => {
                let size = val.encode(buffer, ctx)?;
                Ok(size)
            }
            None => Ok(0),
        }
    }
}

impl<Ctx, T: Encode<Ctx>> Encode<Ctx> for Vec<T> {
    /// Encode each element of vector
    /// use `CountPrefix` to prefix the vector with number of elements
    /// use `LenPrefix` to prefix the vector with encoded byte size
    fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error> {
        let mut size = 0;
        for item in self {
            size += item.encode(buffer, ctx)?;
        }
        Ok(size)
    }
}

impl<Ctx> Encode<Ctx> for String {
    /// Encode the string using utf8
    /// use `CountPrefix` to prefix the string with char length
    /// use `LenPrefix` to prefix the string with utf8 bytes length
    fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> {
        let utf8 = self.as_bytes();
        buffer.write_all(utf8)?;
        Ok(utf8.len())
    }
}

impl<Ctx> Encode<Ctx> for Arc<[u8]> {
    /// write raw slice into the buffer
    /// use `LenPrefix` to prefix with byte length
    fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> {
        let len = buffer.write(self)?;
        Ok(len)
    }
}

impl<Ctx, const S: usize> Encode<Ctx> for [u8; S] {
    /// write raw slice into the buffer
    /// use `LenPrefix` to prefix with byte length
    fn encode(&self, buffer: &mut dyn Write, _ctx: &mut Ctx) -> Result<usize, Error> {
        let len = buffer.write(self)?;
        Ok(len)
    }
}

impl_encode!(u8);
impl_encode!(u16);
impl_encode!(u32);
impl_encode!(u64);
impl_encode!(u128);
impl_encode!(usize);

impl_encode!(i8);
impl_encode!(i16);
impl_encode!(i32);
impl_encode!(i64);
impl_encode!(i128);
impl_encode!(isize);

impl_encode!(f32);
impl_encode!(f64);