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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*!
Documentation for the `Zeroable` derive macro.
This macro is for deriving the
[`bytemuck::Zeroable` trait](https://docs.rs/bytemuck/1/bytemuck/trait.Zeroable.html).
# Restrictions
All of these restrictions are enforced at compile-time.
### Structs
All fields are required to implement Zeroable.
```rust
use zeroable::Zeroable;
#[derive(Zeroable)]
struct AStruct{
left:u32,
right:u32,
}
```
### Enums
Enums must satisfy one of these:
- Having a `#[repr(C/u8/i8/u16/i16/u32/i32/u64/i64/u128/i128/usize/isize)]` attribute,
with either an implicit discriminant for the first variant (which is always `0`),
or an explicit `0` discriminant for some variant.
<br>
The fields of the variant with a `0` discriminant will then be required to
implement Zeroable,while the fields of other variants won't be.
- Having a `#[repr(transparent)]` attribute,with a single variant and field,
which must implement Zeroable.
### Unions
All fields are required to implement Zeroable by default,
opting out of Zeroable for fields individually with `#[zero(nonzero)]`.
The alternative to using the `#[zero(nonzero)]` attribute on fields is
to use the `#[zero(nonzero_fields)]` attribute on the union
(which makes not requiring zeroable for fields the default for that union),
then using the `#[zero(zeroable)]` attribute on zeroable fields.
Zeroable impls for unions have documentation mentioning
which fields were marked as zeroable,and which are not.
# Attributes
These are all the attributes for the derive macro,grouped by where they can be used.
## Container attributes
##### `#[zero(bound="Type:ATrait")]`
Adds a contraint to the `Zeroable` impl.
##### `#[zero(not_zeroable(TypeParamA,TypeParamB,TypeParamC))]`
Removes the default `Zeroable` bound for one/many type parameters.
##### `#[zero(nonzero_fields)]`
For unions only.
Marks all the fields as not being zeroable,
removing the assertion that they implement Zeroable,
requiring some fields to have a `#[zero(zeroable)]` attribute.
##### `#[zero(debug_print)]`
Prints the generated code,stopping compilation.
## Field attributes
##### `#[zero(zeroable)]`
For unions only.
Marks the field as being initializable with zeroes,
adding an assertion that it implements Zeroable.
The field is then mentioned in the generated documentation for
the Zeroable impl under `Zeroable Fields`.
##### `#[zero(nonzero)]`
For unions only.
Marks the field as not being initializable with zeroes,
removing the assertion that it implements Zeroable.
The field is then mentioned in the generated documentation for
the Zeroable impl under `NonZero Fields`.
# Examples
### Enum
A Result-like enum,with `Ok` as the variant instantiated by `zeroed`.
```rust
use zeroable::Zeroable;
#[derive(Debug,PartialEq,Zeroable)]
#[repr(u8)]
#[zero(not_zeroable(E))]
enum MyResult<T,E>{
Ok(T),
Err(E),
}
assert_eq!( MyResult::<(),String>::zeroed(), MyResult::Ok(()) );
assert_eq!( MyResult::<bool,Vec<()>>::zeroed(), MyResult::Ok(false) );
// This won't compile because Vec is not zeroable.
// assert_eq!( MyResult::<Vec<()>,String>::zeroed(), MyResult::Ok(vec![]) );
```
### Enum
A simple Option-like enum.
In this the None variant is the one instantiated by `zeroed`.
`#[zero(not_zeroable(T))]` doesn't cause an error because `T` is not in
the variant instantiated by `zeroed`
(if `None` contained a `T`,then it would be an error).
```rust
use zeroable::Zeroable;
#[derive(Debug,PartialEq,Zeroable)]
#[repr(u8)]
#[zero(not_zeroable(T))]
enum MyOption<T>{
None,
Some(T)
}
assert_eq!( MyOption::<String>::zeroed(), MyOption::None );
```
### Enum
Here is an Ordering-like enum.
```rust
use zeroable::Zeroable;
#[derive(Debug,PartialEq,Zeroable)]
#[repr(i8)]
enum Ordering{
Less=-1,
Equal=0,
Greater=1,
}
assert_eq!( Ordering::zeroed(), Ordering::Equal );
```
### Enum (non-compiling)
This doesn't compile because there is no variant with a `0` discriminant.
```compile_fail
use zeroable::Zeroable;
#[derive(Debug,PartialEq,Zeroable)]
#[repr(u8)]
enum Directions{
Left=1,
Right,
Up,
Down,
}
```
### Enum (non-compiling)
This doesn't compile because the first variant contains a `NonZeroU8`,
which is not zeroable.
```compile_fail
use zeroable::Zeroable;
use core::num::NonZeroU8;
#[derive(Debug,PartialEq,Zeroable)]
#[repr(u8)]
enum NonZeroOrZeroable{
NonZero(NonZeroU8),
Zeroable(u8),
}
```
It compiles if you swap the variants:
```rust
use zeroable::Zeroable;
use core::num::NonZeroU8;
#[derive(Debug,PartialEq,Zeroable)]
#[repr(u8)]
enum NonZeroOrZeroable{
Zeroable(u8),
NonZero(NonZeroU8),
}
assert_eq!( NonZeroOrZeroable::zeroed(), NonZeroOrZeroable::Zeroable(0) );
```
this is because the first variant of an enum implicitly has a `0` discriminant,
and `u8` is zeroable.
### Enum (requires nightly)
This is an example of a `#[repr(transparent)]` enum.
*/
/*!
#![feature(transparent_enums)]
use zeroable::Zeroable;
#[derive(Debug,PartialEq,Zeroable)]
#[repr(transparent)]
enum Wrapper<T>{
Value(T),
}
assert_eq!( Wrapper::<isize>::zeroed(), Wrapper::Value(0_isize) );
assert_eq!( Wrapper::<usize>::zeroed(), Wrapper::Value(0_usize) );
assert_eq!( Wrapper::<(usize,usize)>::zeroed(), Wrapper::Value((0_usize,0_usize)) );
```
### Enum (requires nightly) (non-compiling)
This is an example that fixes a non-compiling enum by setting the discriminant
of a variant to 0.
*/
/*!
use zeroable::Zeroable;
use std::error::Error;
#[derive(Debug,Zeroable)]
#[repr(i8)]
enum MyError{
PageNotFound{name:String},
Undefined,
Other(Box<dyn Error>)
}
```
This fails to compile because String isn't zeroable,
so let's change the variant with a zero discriminant to `Undefined`
*/
/*!
#![feature(arbitrary_enum_discriminant)]
use zeroable::Zeroable;
use std::error::Error;
#[derive(Debug,Zeroable)]
#[repr(i8)]
enum MyError{
PageNotFound{name:String}=-1,
Undefined=0,
Other(Box<dyn Error>),
}
```
The first variant has to have an explicit discriminant,
because otherwise it uses `0` as its discriminant,
causing a compile time error.
### Struct
A Rectangle type.
```rust
use zeroable::Zeroable;
#[derive(Debug,PartialEq,Zeroable)]
struct Rectangle<T>{
x:T,
y:T,
w:T,
h:T,
}
assert_eq!( Rectangle::zeroed(), Rectangle{ x:0, y:0, w:0, h:0 } );
```
### Struct
Here we define a binary tree of zeroable with indices instead of pointers:
```
use zeroable::Zeroable;
use core::num::NonZeroU32;
#[derive(Debug,PartialEq)]
struct Tree<T>{
list:Vec<TreeNode<T>>,
}
#[derive(Debug,PartialEq,Zeroable)]
struct TreeNode<T>{
value:T,
left:Option<NonZeroU32>,
right:Option<NonZeroU32>,
}
assert_eq!(
TreeNode::<[u8;32]>::zeroed(),
TreeNode{
value:[0;32],
left:None,
right:None,
},
);
```
### Struct (non-compiling)
This doesn't compile because `&[T]` is not zeroable.
```compile_fail
use zeroable::Zeroable;
#[derive(Debug,PartialEq,Zeroable)]
struct NonEmptySlice<'a,T>{
slice:&'a [T],
}
```
### Union
```rust
use zeroable::Zeroable;
#[derive(Zeroable)]
#[repr(C)] // This isn't necessary for Zeroable
union U32OrArray{
num:u32,
arr:[u8;4],
}
unsafe{
let zeroed=U32OrArray::zeroed();
assert_eq!( zeroed.num, 0 );
assert_eq!( zeroed.arr, [0;4] );
}
```
### Union
```rust
use zeroable::Zeroable;
#[derive(Zeroable)]
#[zero(not_zeroable(T))]
#[zero(nonzero_fields)]
union CondValue<T:Copy>{
#[zero(zeroable)]
cond:bool,
value:T,
}
unsafe{
let zeroed=CondValue::<&'static str>::zeroed();
assert_eq!( zeroed.cond, false );
// You can't read from `zeroed.value` because a reference can't be zeroed.
}
unsafe{
let zeroed=CondValue::<char>::zeroed();
assert_eq!( zeroed.cond, false );
assert_eq!( zeroed.value, '\0' );
}
```
### Union (requires nightly)
This is an example of a `#[repr(transparent)]` union.
*/
/*!
#![feature(transparent_unions)]
use zeroable::Zeroable;
#[derive(Zeroable)]
#[repr(transparent)]
union Wrapper<T:Copy>{
value:T,
}
unsafe{
assert_eq!( Wrapper::<isize>::zeroed().value, 0_isize );
assert_eq!( Wrapper::<usize>::zeroed().value, 0_usize );
assert_eq!( Wrapper::<(usize,usize)>::zeroed().value, (0_usize,0_usize) );
}
```
### Union (non-compiling)
This doesn't compile because `ManuallyDrop<T>` is not zeroable.
```compile_fail
use zeroable::Zeroable;
use core::mem::ManuallyDrop;
#[derive(Zeroable)]
#[zero(not_zeroable(T))]
union MaybeUninitialized<T:Copy>{
uninit:(),
init:ManuallyDrop<T>,
}
```
To fix it simply remove the default `Zeroable` bound on the field like this:
```
use zeroable::Zeroable;
use core::mem::ManuallyDrop;
#[derive(Zeroable)]
#[zero(not_zeroable(T))]
union MaybeUninitialized<T:Copy>{
uninit:(),
#[zero(nonzero)]
init:ManuallyDrop<T>,
}
```
### Union (non-compiling)
This doesn't compile because the union has a `#[zero(nonzero_fields)]` attribute,
but no field has a `#[zero(zeroable)]` attribute.
```compile_fail
use zeroable::Zeroable;
use core::mem::ManuallyDrop;
#[derive(Zeroable)]
#[zero(not_zeroable(T))]
#[zero(nonzero_fields)]
union UnsafeEither<T:Copy,U:Copy>{
left:T,
right:U,
}
```
To fix this instance simply add a `#[zero(zeroable)]` attribute to a field like this:
```
use zeroable::Zeroable;
use core::mem::ManuallyDrop;
#[derive(Zeroable)]
#[zero(not_zeroable(T))]
#[zero(nonzero_fields)]
union UnsafeEither<T:Copy,U:Copy>{
left:T,
#[zero(zeroable)]
right:U,
}
```
*/