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
#![crate_type = "proc-macro"]
#![deny(warnings, unused_import_braces, unused_qualifications)]
#![forbid(unsafe_code)]
extern crate proc_macro;
use quote::quote;
use synstructure::{decl_derive, BindStyle};
fn zeroize_derive(mut s: synstructure::Structure) -> proc_macro2::TokenStream {
s.bind_with(|_| BindStyle::RefMut);
let body = s.each(|bi| quote! { #bi.zeroize(); });
s.bound_impl(
quote!(zeroize::Zeroize),
quote! {
fn zeroize(&mut self) {
match self {
#body
}
}
},
)
}
decl_derive!([Zeroize] => zeroize_derive);
fn zeroize_on_drop_derive(s: synstructure::Structure) -> proc_macro2::TokenStream {
let drop_impl = s.gen_impl(quote! {
gen impl Drop for @Self {
fn drop(&mut self) {
self.zeroize();
}
}
});
let zeroize_on_drop_impl = s.bound_impl(quote!(zeroize::ZeroizeOnDrop), quote!());
quote! {
#[doc(hidden)]
#drop_impl
#zeroize_on_drop_impl
}
}
decl_derive!([ZeroizeOnDrop] => zeroize_on_drop_derive);
#[cfg(test)]
mod tests {
use super::*;
use synstructure::test_derive;
#[test]
fn zeroize() {
test_derive! {
zeroize_derive {
struct Z {
a: String,
b: Vec<u8>,
c: [u8; 3],
}
}
expands to {
#[allow(non_upper_case_globals)]
#[doc(hidden)]
const _DERIVE_zeroize_Zeroize_FOR_Z: () = {
extern crate zeroize;
impl zeroize::Zeroize for Z {
fn zeroize(&mut self) {
match self {
Z {
a: ref mut __binding_0,
b: ref mut __binding_1,
c: ref mut __binding_2,
} => {
{ __binding_0.zeroize(); }
{ __binding_1.zeroize(); }
{ __binding_2.zeroize(); }
}
}
}
}
};
}
no_build
}
}
#[test]
fn zeroize_on_drop() {
test_derive! {
zeroize_on_drop_derive {
struct Z {
a: String,
b: Vec<u8>,
c: [u8; 3],
}
}
expands to {
#[doc(hidden)]
#[allow(non_upper_case_globals)]
const _DERIVE_Drop_FOR_Z: () = {
impl Drop for Z {
fn drop(&mut self) {
self.zeroize();
}
}
};
#[allow(non_upper_case_globals)]
#[doc(hidden)]
const _DERIVE_zeroize_ZeroizeOnDrop_FOR_Z : () = {
extern crate zeroize;
impl zeroize :: ZeroizeOnDrop for Z {}
};
}
no_build
}
}
}