-
Notifications
You must be signed in to change notification settings - Fork 32
/
byte_converter_template.rs
249 lines (200 loc) · 5.72 KB
/
byte_converter_template.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
//! Traits for (de)serialization of structs to byte vectors.
use byteorder::*;
use crate::converting_receiver::BrickletError;
/// A trait to serialize the implementing type to a byte vector.
pub trait ToBytes {
/// Serialize the implementing type to a byte vector.
fn to_le_bytes(_: Self) -> Vec<u8>;
/// Try to serialize the implementing type to a byte vector. If the type is shorter than max_len, it will be padded with zero bytes. Currently this method is only used for strings. Other types use the standard implementation, which calls [`to_le_bytes`] without further checks or padding.
/// # Errors
/// Returns an InvalidArgument error if the type was too long.
///
/// [`to_le_bytes`]: #ToBytes.to_le_bytes
fn try_to_le_bytes(var: Self, _max_len: usize) -> Result<Vec<u8>, BrickletError> where Self: std::marker::Sized {
Ok(Self::to_le_bytes(var))
}
}
/// A trait to deserialize the implemeting type from a byte slice.
pub trait FromByteSlice {
/// Deserialize the implementing type from a byte slice.
fn from_le_bytes(bytes: &[u8]) -> Self;
/// Returns how many bytes are expected to deserialize a instance of the implementing type. Currently this method is only used for strings.
fn bytes_expected() -> usize;
}
impl ToBytes for () {
fn to_le_bytes(_: ()) -> Vec<u8> {
vec![]
}
}
impl FromByteSlice for () {
fn from_le_bytes(_: &[u8]) { }
fn bytes_expected() -> usize { 0 }
}
impl ToBytes for bool {
fn to_le_bytes(b: bool) -> Vec<u8> {
vec![b as u8]
}
}
impl FromByteSlice for bool {
fn from_le_bytes(bytes: &[u8]) -> bool {
bytes[0] != 0
}
fn bytes_expected() -> usize { 1 }
}
impl ToBytes for u8 {
fn to_le_bytes(num: u8) -> Vec<u8> {
vec![num]
}
}
impl FromByteSlice for u8 {
fn from_le_bytes(bytes: &[u8]) -> u8 {
bytes[0]
}
fn bytes_expected() -> usize { 1 }
}
impl ToBytes for i8 {
fn to_le_bytes(num: i8) -> Vec<u8> {
vec![num as u8]
}
}
impl FromByteSlice for i8 {
fn from_le_bytes(bytes: &[u8]) -> i8 {
bytes[0] as i8
}
fn bytes_expected() -> usize { 1 }
}
impl ToBytes for u16 {
fn to_le_bytes(num: u16) -> Vec<u8> {
let mut buf = vec![0; 2];
LittleEndian::write_u16(&mut buf, num);
buf
}
}
impl FromByteSlice for u16 {
fn from_le_bytes(bytes: &[u8]) -> u16 {
LittleEndian::read_u16(bytes)
}
fn bytes_expected() -> usize { 2 }
}
impl ToBytes for i16 {
fn to_le_bytes(num: i16) -> Vec<u8> {
let mut buf = vec![0; 2];
LittleEndian::write_i16(&mut buf, num);
buf
}
}
impl FromByteSlice for i16 {
fn from_le_bytes(bytes: &[u8]) -> i16 {
LittleEndian::read_i16(bytes)
}
fn bytes_expected() -> usize { 2 }
}
impl ToBytes for u32 {
fn to_le_bytes(num: u32) -> Vec<u8> {
let mut buf = vec![0; 4];
LittleEndian::write_u32(&mut buf, num);
buf
}
}
impl FromByteSlice for u32 {
fn from_le_bytes(bytes: &[u8]) -> u32 {
LittleEndian::read_u32(bytes)
}
fn bytes_expected() -> usize { 4 }
}
impl ToBytes for i32 {
fn to_le_bytes(num: i32) -> Vec<u8> {
let mut buf = vec![0; 4];
LittleEndian::write_i32(&mut buf, num);
buf
}
}
impl FromByteSlice for i32 {
fn from_le_bytes(bytes: &[u8]) -> i32 {
LittleEndian::read_i32(bytes)
}
fn bytes_expected() -> usize { 4 }
}
impl ToBytes for u64 {
fn to_le_bytes(num: u64) -> Vec<u8> {
let mut buf = vec![0; 8];
LittleEndian::write_u64(&mut buf, num);
buf
}
}
impl FromByteSlice for u64 {
fn from_le_bytes(bytes: &[u8]) -> u64 {
LittleEndian::read_u64(bytes)
}
fn bytes_expected() -> usize { 8 }
}
impl ToBytes for i64 {
fn to_le_bytes(num: i64) -> Vec<u8> {
let mut buf = vec![0; 8];
LittleEndian::write_i64(&mut buf, num);
buf
}
}
impl FromByteSlice for i64 {
fn from_le_bytes(bytes: &[u8]) -> i64 {
LittleEndian::read_i64(bytes)
}
fn bytes_expected() -> usize { 8 }
}
impl ToBytes for char {
fn to_le_bytes(c: char) -> Vec<u8> {
vec![c as u8]
}
}
impl FromByteSlice for char {
fn from_le_bytes(bytes: &[u8]) -> char {
bytes[0] as char
}
fn bytes_expected() -> usize { 1 }
}
impl ToBytes for String {
fn to_le_bytes(s: String) -> Vec<u8> { s.into_bytes() }
fn try_to_le_bytes(s: String, max_len: usize) -> Result<Vec<u8>, BrickletError> {
if s.chars().any(|c| c as u32 > 255) {
return Err(BrickletError::InvalidParameter);
}
let bytes: Vec<u8> = s.chars().map(|c| c as u8).collect();
if bytes.len() > max_len {
Err(BrickletError::InvalidParameter)
} else {
let mut result = vec![0u8; max_len];
result[0..bytes.len()].copy_from_slice(&bytes);
Ok(result)
}
}
}
impl FromByteSlice for String {
fn from_le_bytes(bytes: &[u8]) -> String { bytes.into_iter().filter(|&&b| b != 0).map(|&b| b as char).collect() }
fn bytes_expected() -> usize { 1 }
}
impl ToBytes for f32 {
fn to_le_bytes(num: f32) -> Vec<u8> {
let mut buf = vec![0; 4];
LittleEndian::write_f32(&mut buf, num);
buf
}
}
impl FromByteSlice for f32 {
fn from_le_bytes(bytes: &[u8]) -> f32 {
LittleEndian::read_f32(bytes)
}
fn bytes_expected() -> usize { 4 }
}
impl ToBytes for f64 {
fn to_le_bytes(num: f64) -> Vec<u8> {
let mut buf = vec![0; 8];
LittleEndian::write_f64(&mut buf, num);
buf
}
}
impl FromByteSlice for f64 {
fn from_le_bytes(bytes: &[u8]) -> f64 {
LittleEndian::read_f64(bytes)
}
fn bytes_expected() -> usize { 8 }
}