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
|
// Copyright 2024 Gabriel Bjørnager Jensen.
//
// This file is part of bzipper.
//
// bzipper is free software: you can redistribute
// it and/or modify it under the terms of the GNU
// Lesser General Public License as published by
// the Free Software Foundation, either version 3
// of the License, or (at your option) any later
// version.
//
// bzipper is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Less-
// er General Public License along with bzipper. If
// not, see <https://www.gnu.org/licenses/>.
#[cfg(test)]
mod test;
use crate::SStream;
use std::convert::Infallible;
use std::mem::size_of;
use std::num::NonZero;
/// Denotes a type capable of being serialised.
pub trait Serialise: Sized {
/// Serialises `self` into a byte stream.
///
/// One may assume that the resulting stream has at most the same ammount of bytes as before serialisation.
/// Therefore, not observing this rule is a logic error.
fn serialise(&self, stream: &mut SStream);
}
macro_rules! impl_float {
($type:ty) => {
impl Serialise for $type {
fn serialise(&self, stream: &mut SStream) {
stream.append(&self.to_be_bytes())
}
}
};
}
macro_rules! impl_int {
($type:ty) => {
impl Serialise for $type {
fn serialise(&self, stream: &mut SStream) {
stream.append(&self.to_be_bytes())
}
}
impl Serialise for NonZero<$type> {
fn serialise(&self, stream: &mut SStream) {
self.get().serialise(stream)
}
}
};
}
impl<T: Serialise, const N: usize> Serialise for [T; N] {
fn serialise(&self, stream: &mut SStream) {
u64::try_from(self.len()).unwrap().serialise(stream);
for v in self { v.serialise(stream) }
}
}
impl Serialise for () {
fn serialise(&self, _stream: &mut SStream) { }
}
impl Serialise for bool {
fn serialise(&self, stream: &mut SStream) {
u8::from(*self).serialise(stream)
}
}
impl Serialise for char {
fn serialise(&self, stream: &mut SStream) {
u32::from(*self).serialise(stream)
}
}
impl Serialise for Infallible {
fn serialise(&self, _stream: &mut SStream) { unreachable!() }
}
impl<T: Serialise> Serialise for Option<T> {
fn serialise(&self, stream: &mut SStream) {
match *self {
None => {
stream.append(&[0x00]);
stream.append(&vec![0x00; size_of::<T>()]);
},
Some(ref v) => {
stream.append(&[0x01]);
v.serialise(stream);
},
};
}
}
impl<T: Serialise, E: Serialise> Serialise for Result<T, E> {
fn serialise(&self, stream: &mut SStream) {
match *self {
Ok(ref v) => {
stream.append(&[0x00]);
v.serialise(stream);
},
Err(ref e) => {
stream.append(&[0x01]);
e.serialise(stream);
},
};
}
}
impl_float!(f32);
impl_float!(f64);
impl_int!(i128);
impl_int!(i16);
impl_int!(i32);
impl_int!(i64);
impl_int!(i8);
impl_int!(u128);
impl_int!(u16);
impl_int!(u32);
impl_int!(u64);
impl_int!(u8);
|