-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.rs
342 lines (303 loc) · 7.17 KB
/
types.rs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::{
Pointer, PointerType, Position, Read, ReadType, StaticSize, VecReader, Write, WriteType, Writer,
};
use num::ToPrimitive;
use std::{convert::TryInto, mem::size_of};
impl<'a> ReadType<'a> for () {
type ReadType = ();
}
impl WriteType for () {
type WriteType = ();
}
impl StaticSize for () {
const STATIC_SIZE: PointerType = 0;
}
impl<'a> Read<'a> for () {
type Output = Self;
fn read(_bytes: &'a [u8], _position: Position<Self>) -> Self::Output {}
}
impl Write for () {
type Output = Self;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.position()
}
}
impl<'a> ReadType<'a> for bool {
type ReadType = bool;
}
impl WriteType for bool {
type WriteType = bool;
}
impl StaticSize for bool {
const STATIC_SIZE: PointerType = 1;
}
impl<'a> Read<'a> for bool {
type Output = Self;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
let position = position.cast();
<u8>::read(bytes, position) != 0
}
}
impl Write for bool {
type Output = Self;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
let value = *self as u8;
writer.write_raw::<bool>(&value.to_le_bytes())
}
}
macro_rules! impl_read_write_for_number_type {
($ty:ty) => {
impl<'a> ReadType<'a> for $ty {
type ReadType = $ty;
}
impl WriteType for $ty {
type WriteType = $ty;
}
impl StaticSize for $ty {
const STATIC_SIZE: PointerType = size_of::<$ty>() as PointerType;
}
impl<'a> Read<'a> for $ty {
type Output = Self;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
let position = position.to_usize().unwrap();
let bytes = &bytes[position..position + size_of::<$ty>()];
let bytes = bytes.try_into().unwrap();
<$ty>::from_le_bytes(bytes)
}
}
impl Write for $ty {
type Output = $ty;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.write_raw::<$ty>(&self.to_le_bytes())
}
}
};
}
impl_read_write_for_number_type!(u8);
impl_read_write_for_number_type!(u16);
impl_read_write_for_number_type!(u32);
impl_read_write_for_number_type!(u64);
impl_read_write_for_number_type!(i8);
impl_read_write_for_number_type!(i16);
impl_read_write_for_number_type!(i32);
impl_read_write_for_number_type!(i64);
impl_read_write_for_number_type!(f32);
impl_read_write_for_number_type!(f64);
impl<'a> ReadType<'a> for char {
type ReadType = char;
}
impl WriteType for char {
type WriteType = char;
}
impl StaticSize for char {
const STATIC_SIZE: PointerType = 4;
}
impl<'a> Read<'a> for char {
type Output = Self;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
<u32>::read(bytes, position.cast()).try_into().unwrap()
}
}
impl Write for char {
type Output = Self;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.write(&(*self as u32)).cast()
}
}
impl<'a, T> ReadType<'a> for Option<T>
where
T: ReadType<'a>,
T::ReadType: StaticSize,
{
type ReadType = Option<T::ReadType>;
}
impl<T> WriteType for Option<T>
where
T: WriteType,
{
type WriteType = Option<T::WriteType>;
}
impl<T> StaticSize for Option<T>
where
T: StaticSize,
{
const STATIC_SIZE: PointerType = 1 + T::STATIC_SIZE;
}
impl<'a, T> Read<'a> for Option<T>
where
T: StaticSize + Read<'a>,
{
type Output = Option<T::Output>;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
let is_some = bool::read(bytes, position.offset(0));
if is_some {
Some(T::read(bytes, position.offset(1)))
} else {
None
}
}
}
impl<T> Write for Option<T>
where
T: StaticSize + Write,
T::Output: Sized,
{
type Output = Option<T::Output>;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
let position = writer.position();
match self {
None => {
writer.write(&0u8);
for _ in 0..T::STATIC_SIZE {
writer.write(&0u8);
}
}
Some(value) => {
writer.write(&1u8);
writer.write(value);
}
}
position
}
}
impl<'a, T1, T2> ReadType<'a> for (T1, T2)
where
T1: ReadType<'a>,
T1::ReadType: StaticSize,
T2: ReadType<'a>,
T2::ReadType: StaticSize,
{
type ReadType = (T1::ReadType, T2::ReadType);
}
impl<T1, T2> WriteType for (T1, T2)
where
T1: WriteType,
T2: WriteType,
{
type WriteType = (T1::WriteType, T2::WriteType);
}
impl<'a, T1, T2> StaticSize for (T1, T2)
where
T1: Read<'a> + StaticSize,
T2: Read<'a> + StaticSize,
{
const STATIC_SIZE: PointerType = T1::STATIC_SIZE + T2::STATIC_SIZE;
}
impl<'a, T1, T2> Read<'a> for (T1, T2)
where
T1: Read<'a> + StaticSize,
T2: Read<'a> + StaticSize,
{
type Output = (T1::Output, T2::Output);
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
(
T1::read(bytes, position.offset(0)),
T2::read(bytes, position.offset(T1::STATIC_SIZE)),
)
}
}
impl<T1, T2> Write for (T1, T2)
where
T1: Write + StaticSize,
T2: Write + StaticSize,
T1::Output: Sized,
T2::Output: Sized,
{
type Output = (T1::Output, T2::Output);
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
let position = writer.position();
self.0.write(writer);
self.1.write(writer);
position
}
}
impl<'a, T> ReadType<'a> for Vec<T>
where
T: 'a + ReadType<'a>,
T::ReadType: StaticSize,
{
type ReadType = Pointer<VecReader<'a, T::ReadType>>;
}
impl<T> WriteType for Vec<T>
where
T: WriteType,
{
type WriteType = Position<[T::WriteType]>;
}
impl<T> Write for Vec<T>
where
T: Write,
T::Output: Sized,
{
type Output = [T::Output];
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.write(self.as_slice())
}
}
impl<T> Write for [T]
where
T: Write,
T::Output: Sized,
{
type Output = [T::Output];
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
let position = writer.position();
let len = self.len() as PointerType;
writer.write(&len);
for value in self.iter() {
writer.write(value);
}
position
}
}
impl<'a> ReadType<'a> for String {
type ReadType = Pointer<&'a str>;
}
impl WriteType for String {
type WriteType = Position<str>;
}
impl<'a> StaticSize for &'a str {
const STATIC_SIZE: PointerType = size_of::<PointerType>() as PointerType;
}
impl<'a> Read<'a> for &'a str {
type Output = Self;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
let len = PointerType::read(bytes, position.cast());
let len = len.to_usize().unwrap();
let position = position.offset::<str>(PointerType::STATIC_SIZE);
let position = *position as usize;
std::str::from_utf8(&bytes[position..position + len]).unwrap()
}
}
impl Write for str {
type Output = str;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
let position = writer.position();
let len: PointerType = self.len().try_into().unwrap();
writer.write(&len);
writer.write_raw::<str>(self.as_bytes());
position
}
}
impl Write for String {
type Output = str;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.write(self.as_str())
}
}
impl<'a, K, V> ReadType<'a> for std::collections::HashMap<K, V>
where
K: 'a + ReadType<'a>,
K::ReadType: StaticSize,
V: 'a + ReadType<'a>,
V::ReadType: StaticSize,
{
type ReadType = Pointer<VecReader<'a, (K::ReadType, V::ReadType)>>;
}
impl<K, V> WriteType for std::collections::HashMap<K, V>
where
K: WriteType,
V: WriteType,
{
type WriteType = Position<[(K::WriteType, V::WriteType)]>;
}