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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*!
`packtool` is a packing library. Useful to define how serializing
and deserializing data from a type level definition.
# Example
## Unit types
unit types can be packed. What this means is that the object
is known to have the same constant value. That way it is possible
to define values that are expected to be found and to be the same.
All [`Packed`] unit structures must have a `#[packed(value = ...)]`
attribute. The value can be set to any literal except: `bool`, `float`.
```
use packtool::{Packed, View};
# use packtool::Error;
/// a unit that is always the utf8 string `"my protocol"`
/// and takes 11 bytes in the packed structure
#[derive(Packed)]
#[packed(value = "my protocol")]
pub struct ProtocolPrefix;
/// a unit that is always `4` and takes 1 byte long
#[derive(Packed)]
#[packed(value = 0b0000_0100u8)]
pub struct OtherUnit();
/// a unit that is always `0xcafe` and takes 4 bytes
/// in the packed structure
#[derive(Packed)]
#[packed(value = 0xcafeu32)]
pub struct LastButNotLeast {}
# fn test() -> Result<(), Error> {
const SLICE: &[u8] = b"my protocol";
let view: View<'_, ProtocolPrefix> = View::try_from_slice(SLICE)?;
# Ok(()) }
# test().unwrap();
# assert_eq!(ProtocolPrefix::SIZE, 11);
# assert_eq!(OtherUnit::SIZE, 1);
# assert_eq!(LastButNotLeast::SIZE, 4);
```
Here we are expecting the `ProtocolPrefix` to always have the
same value in the packed representation. When serializing the
`ProtocolPrefix`, the `value` will be set with these 11
characters.
## Enumeration
Only enumerations without fields are allowed for now.
```
use packtool::{Packed, View};
# use packtool::Error;
#[derive(Packed)]
#[repr(u8)]
pub enum Version {
V1 = 1,
V2 = 2,
}
# fn test() -> Result<(), Error> {
# const SLICE: &[u8] = &[1];
let view: View<'_, Version> = View::try_from_slice(SLICE)?;
assert!(matches!(view.unpack(), Version::V1));
# Ok(()) }
# test().unwrap();
# assert_eq!(Version::SIZE, 1);
```
the `repr(...)` is necessary in order to set a size to the enum.
```compile_fail
use packtool::Packed;
#[derive(Packed)]
pub enum Color {
Red = 1,
Green = 2,
Blue = -1
}
```
Enumerations with data-carrying variants are not supported yet and
are rejected at compile time:
```compile_fail
use packtool::Packed;
#[derive(Packed)]
pub enum ThisOrThat {
This,
That(u32),
}
```
Unions cannot be packed and are rejected at compile time:
```compile_fail
use packtool::Packed;
#[derive(Packed)]
pub union Choice {
a: u32,
b: f32,
}
```
Type-parameter generics are supported on **named** structs. The packed layout
and `SIZE` are computed through `<T as Packed>::SIZE`, and a `T: Packed` bound is
injected for every type parameter (composing with any `where` clause you write):
```
use packtool::Packed;
#[derive(Packed)]
pub struct Log<T> {
parent_id: [u8; 64],
content: T,
signature: [u8; 64],
}
# assert_eq!(<Log<u32> as Packed>::SIZE, 64 + 4 + 64);
```
Generics on enums and on tuple structs are still rejected at compile time:
```compile_fail
use packtool::Packed;
#[derive(Packed)]
pub struct Wrapper<T>(T);
```
```compile_fail
use packtool::Packed;
#[derive(Packed)]
pub enum Either<L, R> {
Left(L),
Right(R),
}
```
A generic struct with no fields is rejected too: its type parameters could not
appear in the (empty) packed layout.
```compile_fail
use packtool::Packed;
#[derive(Packed)]
pub struct Empty<T> {}
```
## combining packed objects
It is possible to compose packed objects in named or tuple structures.
```
use packtool::Packed;
#[derive(Packed)]
#[packed(value = "packcoin")]
pub struct Tag;
/// 1 byte that will be used to store a version number
#[derive(Packed)]
#[repr(u8)]
pub enum Version {
V1 = 1,
V2 = 2,
}
/// 8 bytes that will be used to store a block number
#[derive(Packed)]
pub struct BlockNumber(u32, u32);
/// 9 bytes packed header
#[derive(Packed)]
pub struct Header {
tag: Tag,
version: Version,
block_number: BlockNumber
}
# assert_eq!(Version::SIZE, 1);
# assert_eq!(BlockNumber::SIZE, 8);
# assert_eq!(Header::SIZE, 17);
```
Each of the packed objects have a view accessor for each fields:
* for named fields, the name of the accessor is the name of the field
* for tuples, the name of the accessor is the index of the field preceded by an underscore (`_`): `_0`, `_1` etc.
```
# use packtool::{Packed, View, Packet};
#
# #[derive(Packed)]
# #[packed(value = "packcoin")]
# pub struct Tag;
#
# /// 1 byte that will be used to store a version number
# #[derive(Packed)]
# #[repr(u8)]
# pub enum Version {
# V1 = 1,
# V2 = 2,
# }
#
# /// 8 bytes that will be used to store a block number
# #[derive(Packed)]
# pub struct BlockNumber(u32, u32);
#
# /// 9 bytes packed header
# #[derive(Packed)]
# pub struct Header {
# tag: Tag,
# version: Version,
# block_number: BlockNumber
# }
#
# let header = Header { tag: Tag, version: Version::V1, block_number: BlockNumber(0, 1) };
# let header = Packet::pack(&header);
# let header = header.view();
#
let tag: View<'_, Tag> = Header::tag(header);
let block_number: View<'_, BlockNumber> = Header::block_number(header);
let epoch: View<'_, u32> = BlockNumber::_0(block_number);
let slot: u32 = BlockNumber::_1(block_number).unpack();
#
# assert_eq!(slot, 1);
```
You can rename the accessor with the attribute `accessor`:
```
# use packtool::{Packed, View, Packet};
#
#[derive(Packed)]
pub struct BlockNumber(
#[packed(accessor = "epoch")]
u32,
#[packed(accessor = "slot")]
u32
);
#
# let block_number = Packet::pack(&BlockNumber(0, 1));
# let block_number = block_number.view();
let epoch = BlockNumber::epoch(block_number); // instead of _0
let slot = BlockNumber::slot(block_number).unpack(); // instead of _1
#
# assert_eq!(slot, 1);
```
It is also possible to prevent the accessor to be created. You can set
the accessor with a literal boolean to say if you want the accessor or
not. `true` will simply means the default case (use the index of the field
or use the name for the name of the accessor):
```
# use packtool::{Packed, View, Packet};
#
#[derive(Packed)]
pub struct Hash(
#[packed(accessor = true)]
[u8; 32]
);
#
# let hash = Packet::pack(&Hash([0; 32]));
# let hash = hash.view();
let bytes = Hash::_0(hash);
# assert_eq!(bytes.unpack(), [0; 32]);
```
However if you set it to `false` there will be no accessor created for you:
```compile_fail
# use packtool::{Packed, View, Packet};
#
#[derive(Packed)]
pub struct Hash(
#[packed(accessor = false)]
[u8; 32]
);
#
# let hash = Packet::pack(&Hash([0; 32]));
# let hash = hash.view();
let bytes = Hash::_0(hash);
```
*/
extern crate quickcheck;
extern crate quickcheck_macros;
pub use ;
pub use Packed;
/// trait to define how a fixed size Packed object is serialized
/// into a byte slice representation.
///
/// see crate documentation for more information.