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
//! The main goal of this crate is to simplify serialization of type's and structures to bytes.
//!
//! # Why?
//! If one works with binary formats/protocols a lot of time is spent implementing
//! decoding and encoding types and structures of the format/protocol in order to further
//! process the contained data.
//!
//! For decoding parsers generators like [nom](https://github.com/Geal/nom) are very helpful and easy the implmentation.
//! This create tries to provide a lightweight encoding/output conbinator by just introducing 2 new traits
//! which in turn then can make use of the iterator facilites to crate the desired output chain of bytes.
//!
//! ## Why this extra step with the trait's
//!
//! 1. By introducing such a trait complex (compsites) structures in a lot of cases can be implemented
//! by just encoding and chaining the childs in order
//!
//! 2. The fileds of a type still can be used for encoding but there is no hard dependency
//! on their order nor their actual size
//!
//! e.g. a protocol field size with the encoded size of 2 Bytes (u16), still can
//! be represented e.g. as usize withn the structures/type which save quite some
//! converting and casting.
//!
//! 3. There is no need of a type to provide a specific amount of memory in order
//! to be serialized (the serialization of a type or a type could be 100% computational)
//!
//! e.g.: assume this protocol type/structure (Packet)
//! ```shell
//! +-----------------+-------------------+-----------------+
//! | field1 (1 Byte) | reserved (7 Byte) | filed2 (8 Byte) |
//! +-----------------+-------------------+-----------------+
//! ```
//!
//! internally it could be represented and implemented like this
//!
//! ```rust
//! use tobytes::ByteView;
//! use tobytes::ToBytes;
//!
//! struct Packet {
//! field1: u8,
//! field2: u64
//! }
//!
//! impl Packet {
//! const RESERVED : u8 = 0x00;
//! }
//!
//! impl ByteView for Packet {
//!
//!
//! fn byte_at(&self, index: usize) -> Option<u8> {
//! if index < ByteView::byte_size(self) {
//! match index {
//! 0 => self.field1.byte_at(index),
//! 1..=7 => Some(Packet::RESERVED),
//! 8..=15 => self.field2.byte_at(index -7),
//! _ => None
//! }
//! }
//! else {
//! None
//! }
//! }
//!
//! fn byte_size(&self) -> usize {
//! ByteView::byte_size(&self.field1) + 7usize + ByteView::byte_size(&self.field2)
//! }
//! }
//!
//! let field1 = 0xaau8;
//! let field2 = 0xaabbccddeeff11u64.to_be();
//! let p = Packet {field1, field2};
//! let mut bytes = p.to_bytes();
//!
//! assert_eq!(16usize, p.byte_size());
//!
//! assert_eq!(
//! vec![0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11],
//! bytes.collect::<Vec<u8>>()
//! );
//! ```
//!
//! # How? (Usage)
//!
//! ## Example(s)
//!
//! ### How to serialize integers of different endianess and size
//!
//! ```rust
//! use tobytes::ByteView;
//! use tobytes::ToBytes;
//!
//! let uint16_be : u16 = 0x0A0Bu16.to_be();
//! let uint16_le : u16 = 0x0C0Du16.to_le();
//! let uint32_le : u32 = 0x01020304u32.to_le();
//!
//! let uint16_be_bytes = uint16_be.to_bytes();
//! let uint16_le_bytes = uint16_le.to_bytes();
//! let uint32_le_bytes = uint32_le.to_bytes();
//!
//! let mut bytes = uint16_be_bytes.chain(uint16_le_bytes.chain(uint32_le_bytes));
//!
//! assert_eq!(vec![0x0A, 0x0B, 0x0D, 0x0C, 0x04, 0x03, 0x02, 0x01], bytes.collect::<Vec<u8>>())
//! ```
//!
//! ### How to serialize a custom type which contains different endinesses and types
//!
//! **TBD**
//!
//! ### How to serialize a custom type which contains types which also implent the ByteView trait
//!
//! **TBD**
/// The ByteView trait allows a type to provide a continues byte view of itself.
/// # Example(s)
/// ```rust
/// use tobytes::ByteView;
/// use tobytes::ToBytes;
/// use std::fmt::Pointer;
/// use std::marker::Sized;
///
/// struct Foo {
/// field1: u8,
/// field2: u16,
/// }
///
/// impl ByteView for Foo {
/// fn byte_at(&self, index: usize) -> Option<u8> {
/// if index < ByteView::byte_size(self) {
/// match index {
/// 0 => self.field1.byte_at(index),
/// 1..=2 => self.field2.byte_at(index -1),
/// _ => None
/// }
/// }
/// else {
/// None
/// }
/// }
///
/// fn byte_size(&self) -> usize {
/// ByteView::byte_size(&self.field1) + ByteView::byte_size(&self.field2)
/// }
/// }
///
/// let foo = Foo {field1: 0xFF, field2: 0xAABBu16.to_be()};
/// let bytes = foo.to_bytes().collect::<Vec<u8>>();
///
/// assert_eq!(3, foo.byte_size());
/// assert_eq!(vec![0xFF, 0xAA, 0xBB], bytes);
/// ```
/// Implements an iterator over the bytes of a ByteView.
/// Trait which converts a Sized type which is implementing the ByteView trait into a Bytes object.
/// Implements the [ByteView](trait.ByteView.html) trait for types which provide a `to_ne_bytes` method.
/// (for more details on `to_ne_bytes` check e.g. `U8, U16, U32, ...`.
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
implement_byte_view_for!;
// TODO: add implement macro or impl. for types which can be converted into a slice of bytes
// TODO's: Implement ByteView for
// * [u8; T]
// * Vec<u8>
// * &[u8]
// ...
// TODO: Implement Derive Macro if all members implement ByteView
// Add test for all supported built in types