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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use super::*;
use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
pub trait LenType: TryFrom<usize> + Encoder + for<'de> Decoder<'de> {}
impl LenType for u8 {}
impl LenType for u16 {}
impl LenType for u32 {}
impl LenType for u64 {}
impl LenType for usize {}
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Record<L: LenType, T> {
pub data: T,
_marker: PhantomData<L>,
}
macro_rules! impls {
[Encoder for $($ty:ty),*] => {$(
impl<L: LenType> Encoder for Record<L, $ty>
where
L::Error: fmt::Debug,
{
#[inline]
fn size_hint(&self) -> usize {
let bytes: &[u8] = self.as_ref();
L::SIZE + bytes.len()
}
#[inline]
fn encoder(&self, c: &mut impl Array<u8>) {
let len: L = self.data.len().try_into().expect("Invalid length type") ;
len.encoder(c);
c.extend_from_slice(&self.data);
}
}
)*};
}
impls!(Encoder for &[u8], &str, String);
impl<'de, L: LenType> Decoder<'de> for Record<L, &'de [u8]>
where
usize: TryFrom<L>,
{
fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, &'static str> {
let len: usize = L::decoder(c)?
.try_into()
.map_err(|_| "Invalid length type")?;
c.read_slice(len)
.map(Record::new)
.ok_or("Insufficient bytes")
}
}
impl<'de, L: LenType> Decoder<'de> for Record<L, &'de str>
where
usize: TryFrom<L>,
{
fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, &'static str> {
let bytes: Record<L, &[u8]> = Record::decoder(c)?;
core::str::from_utf8(bytes.data)
.map_err(|_| "Invalid UTF-8 slice")
.map(Record::new)
}
}
impl<L: LenType> Decoder<'_> for Record<L, String>
where
usize: TryFrom<L>,
{
fn decoder(c: &mut Cursor<&[u8]>) -> Result<Self, &'static str> {
let bytes: Record<L, &[u8]> = Record::decoder(c)?;
String::from_utf8(bytes.to_vec())
.map_err(|_| "Invalid UTF-8 string")
.map(Record::new)
}
}
impl<L, T> Encoder for Record<L, Vec<T>>
where
L: LenType,
L::Error: fmt::Debug,
T: Encoder,
{
#[inline]
fn size_hint(&self) -> usize {
L::SIZE + self.iter().map(T::size_hint).sum::<usize>()
}
#[inline]
fn encoder(&self, c: &mut impl Array<u8>) {
let len: L = self.data.len().try_into().expect("Invalid length type");
len.encoder(c);
for record in &self.data {
record.encoder(c);
}
}
}
impl<'de, L: LenType, T> Decoder<'de> for Record<L, Vec<T>>
where
T: Decoder<'de>,
usize: TryFrom<L>,
{
#[inline]
fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, &'static str> {
let len: usize = L::decoder(c)?
.try_into()
.map_err(|_| "Invalid length type")?;
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(T::decoder(c)?);
}
Ok(Record::new(vec))
}
}
impl<L: LenType, T> Record<L, T> {
#[inline]
pub fn new(data: T) -> Self {
Self {
data,
_marker: PhantomData,
}
}
}
impl<L: LenType, T: fmt::Debug> fmt::Debug for Record<L, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.data.fmt(f)
}
}
impl<L: LenType, T> From<T> for Record<L, T> {
fn from(data: T) -> Self {
Self::new(data)
}
}
impl<L: LenType, T> Deref for Record<L, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<L: LenType, T> DerefMut for Record<L, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}