summaryrefslogtreecommitdiff
path: root/src/codec/decode.rs
blob: 31ed72541d5b4e3eac203a79eb1f39b814a54c19 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::{io::Read, sync::Arc};

use crate::{DEFAULT_BUFFER_LEN, codec::error::Result};

macro_rules! impl_decode {
    ($type: ty) => {
        impl<Ctx> Decode<Ctx> for $type {
            fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self>
            where
                Self: Sized,
            {
                const BYTES: usize = (<$type>::BITS / 8) as usize;
                let mut bytes = [0; BYTES];
                reader.read_exact(&mut bytes)?;
                Ok(<$type>::from_be_bytes(bytes))
            }
        }
    };
}

pub trait Decode<Ctx> {
    fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self>
    where
        Self: Sized;
}

impl<Ctx> Decode<Ctx> for bool {
    fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self>
    where
        Self: Sized,
    {
        let mut byte = [0_u8];
        reader.read_exact(&mut byte)?;
        Ok(byte[0] != 0)
    }
}

impl<Ctx, T: Decode<Ctx>> Decode<Ctx> for Option<T> {
    fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self>
    where
        Self: Sized,
    {
        let tag = u8::decode(reader, ctx)?;
        if tag == 0 {
            Ok(None)
        } else {
            Ok(Some(T::decode(reader, ctx)?))
        }
    }
}

impl<Ctx, T: Decode<Ctx>> Decode<Ctx> for Vec<T> {
    fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self>
    where
        Self: Sized,
    {
        let len = usize::decode(reader, ctx)?;
        let mut vec = Vec::with_capacity(len);
        for _ in 0..len {
            vec.push(T::decode(reader, ctx)?);
        }
        Ok(vec)
    }
}

impl<Ctx> Decode<Ctx> for String {
    fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self>
    where
        Self: Sized,
    {
        let mut bytes: Vec<u8> = Vec::new();
        reader.read_to_end(&mut bytes)?;
        Ok(String::from_utf8_lossy(&bytes).to_string())
    }
}

impl<Ctx> Decode<Ctx> for Arc<[u8]> {
    fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self> {
        let mut buf = Vec::with_capacity(DEFAULT_BUFFER_LEN);
        reader.read_to_end(&mut buf)?;
        Ok(Arc::<[u8]>::from(buf.into_boxed_slice()))
    }
}

impl<Ctx, const S: usize> Decode<Ctx> for [u8; S] {
    fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self>
    where
        Self: Sized,
    {
        let mut slice = [0_u8; S];
        reader.read_exact(&mut slice)?;
        Ok(slice)
    }
}

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

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

impl<Ctx> Decode<Ctx> for f64 {
    fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self>
    where
        Self: Sized,
    {
        let bits = u64::decode(reader, ctx)?;
        let value = f64::from_bits(bits);
        Ok(value)
    }
}

impl<Ctx> Decode<Ctx> for f32 {
    fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self>
    where
        Self: Sized,
    {
        let bits = u32::decode(reader, ctx)?;
        let value = f32::from_bits(bits);
        Ok(value)
    }
}