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
131
|
use std::{io::Read, sync::Arc};
use crate::{DEFAULT_BUFFER_LEN, context::Context};
macro_rules! impl_decode {
($type: ty) => {
impl Decode for $type {
fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::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 {
fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result<Self>
where
Self: Sized;
}
impl Decode for bool {
fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result<Self>
where
Self: Sized,
{
let mut byte = [0_u8];
reader.read_exact(&mut byte)?;
Ok(byte[0] != 0)
}
}
impl<T: Decode> Decode for Option<T> {
fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result<Self>
where
Self: Sized,
{
let tag = u8::decode(reader, ctx)?;
if tag == 0 {
Ok(None)
} else {
Ok(Some(T::decode(reader, ctx)?))
}
}
}
impl<T: Decode> Decode for Vec<T> {
fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::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 Decode for String {
fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result<Self>
where
Self: Sized,
{
// assume reader is limited
let mut bytes: Vec<u8> = Vec::new();
reader.read_to_end(&mut bytes)?;
Ok(String::from_utf8_lossy(&bytes).to_string())
}
}
impl Decode for Arc<[u8]> {
fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::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<const S: usize> Decode for [u8; S] {
fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::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 Decode for f64 {
fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result<Self>
where
Self: Sized,
{
let bits = u64::decode(reader, ctx)?;
let value = f64::from_bits(bits);
Ok(value)
}
}
impl Decode for f32 {
fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result<Self>
where
Self: Sized,
{
let bits = u32::decode(reader, ctx)?;
let value = f32::from_bits(bits);
Ok(value)
}
}
|