@@ -7,9 +7,9 @@ use syn::{
77 Token ,
88} ;
99
10- use crate :: {
11- util , Data , DeriveTrait , DeriveWhere , Error , Item , SimpleType , SplitGenerics , TraitImpl ,
12- } ;
10+ use crate :: { util , DeriveTrait , DeriveWhere , Error , Item , SplitGenerics , TraitImpl } ;
11+ # [ cfg ( feature = "zeroize-on-drop" ) ]
12+ use crate :: { Data , SimpleType } ;
1313
1414/// Dummy-struct implement [`Trait`](crate::Trait) for [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html) .
1515pub struct ZeroizeOnDrop ;
@@ -97,80 +97,82 @@ impl TraitImpl for ZeroizeOnDrop {
9797 Item :: Item ( data) if data. is_empty ( * * trait_) => quote ! {
9898 fn drop( & mut self ) { }
9999 } ,
100+ #[ cfg( feature = "zeroize-on-drop" ) ]
100101 _ => {
101- #[ cfg( feature = "zeroize-on-drop" ) ]
102- {
103- let crate_ = trait_. crate_ ( ) ;
104- let internal = util:: path_segment ( "__internal" ) ;
102+ let crate_ = trait_. crate_ ( ) ;
103+ let internal = util:: path_segment ( "__internal" ) ;
105104
106- let mut assert_zeroize = crate_. clone ( ) ;
107- assert_zeroize
108- . segments
109- . extend ( [ internal. clone ( ) , util:: path_segment ( "AssertZeroize" ) ] ) ;
105+ let mut assert_zeroize = crate_. clone ( ) ;
106+ assert_zeroize
107+ . segments
108+ . extend ( [ internal. clone ( ) , util:: path_segment ( "AssertZeroize" ) ] ) ;
110109
111- let mut assert_zeroize_on_drop = crate_;
112- assert_zeroize_on_drop
113- . segments
114- . extend ( [ internal, util:: path_segment ( "AssertZeroizeOnDrop" ) ] ) ;
110+ let mut assert_zeroize_on_drop = crate_;
111+ assert_zeroize_on_drop
112+ . segments
113+ . extend ( [ internal, util:: path_segment ( "AssertZeroizeOnDrop" ) ] ) ;
115114
116- quote ! {
117- fn drop( & mut self ) {
118- use #assert_zeroize;
119- use #assert_zeroize_on_drop;
115+ quote ! {
116+ fn drop( & mut self ) {
117+ use #assert_zeroize;
118+ use #assert_zeroize_on_drop;
120119
121- match self {
122- #body
123- }
120+ match self {
121+ #body
124122 }
125123 }
126124 }
127- #[ cfg( not( feature = "zeroize-on-drop" ) ) ]
125+ }
126+ #[ cfg( not( feature = "zeroize-on-drop" ) ) ]
127+ _ => {
128+ // Use unused variables.
129+ let _ = body;
130+
131+ let path = util:: path_from_root_and_strs ( trait_. crate_ ( ) , & [ "Zeroize" ] ) ;
132+
128133 quote ! {
129134 fn drop( & mut self ) {
130- #body
135+ #path :: zeroize ( self ) ;
131136 }
132137 }
133138 }
134139 }
135140 }
136141
142+ #[ cfg( feature = "zeroize-on-drop" ) ]
137143 fn build_body (
138144 & self ,
139145 _derive_where : & DeriveWhere ,
140146 trait_ : & DeriveTrait ,
141147 data : & Data ,
142148 ) -> TokenStream {
143- if data. is_empty ( * * trait_) {
144- TokenStream :: new ( )
145- } else {
146- match data. simple_type ( ) {
147- SimpleType :: Struct ( fields) | SimpleType :: Tuple ( fields) => {
148- #[ cfg( feature = "zeroize-on-drop" ) ]
149- {
150- let self_pattern = fields. self_pattern_mut ( ) ;
151- let self_ident = data. iter_self_ident ( * * trait_) ;
152-
153- quote ! {
154- #self_pattern => {
155- #( #self_ident. zeroize_or_on_drop( ) ; ) *
156- }
149+ match data. simple_type ( ) {
150+ SimpleType :: Struct ( fields) | SimpleType :: Tuple ( fields) => {
151+ #[ cfg( feature = "zeroize-on-drop" ) ]
152+ {
153+ let self_pattern = fields. self_pattern_mut ( ) ;
154+ let self_ident = data. iter_self_ident ( * * trait_) ;
155+
156+ quote ! {
157+ #self_pattern => {
158+ #( #self_ident. zeroize_or_on_drop( ) ; ) *
157159 }
158160 }
159- #[ cfg( not( feature = "zeroize-on-drop" ) ) ]
160- {
161- // Use unused variables.
162- let _ = fields;
161+ }
162+ #[ cfg( not( feature = "zeroize-on-drop" ) ) ]
163+ {
164+ // Use unused variables.
165+ let _ = fields;
163166
164- let path = util:: path_from_root_and_strs ( trait_. crate_ ( ) , & [ "Zeroize" ] ) ;
167+ let path = util:: path_from_root_and_strs ( trait_. crate_ ( ) , & [ "Zeroize" ] ) ;
165168
166- quote ! {
167- #path:: zeroize( self ) ;
168- }
169+ quote ! {
170+ #path:: zeroize( self ) ;
169171 }
170172 }
171- SimpleType :: Unit ( _) => TokenStream :: new ( ) ,
172- SimpleType :: Union => unreachable ! ( "unexpected trait for union" ) ,
173173 }
174+ SimpleType :: Unit ( _) => TokenStream :: new ( ) ,
175+ SimpleType :: Union => unreachable ! ( "unexpected trait for union" ) ,
174176 }
175177 }
176178}
0 commit comments