1
1
use quote:: format_ident;
2
+ use syn:: punctuated:: Punctuated ;
2
3
use syn:: {
3
- parse_quote, Attribute , Fields , Ident , Item , ItemImpl , ItemStruct , ItemTrait , Path , Result ,
4
- Signature , Visibility ,
4
+ parse_quote, Attribute , Fields , Ident , Item , ItemImpl , ItemStruct , ItemTrait , Meta , Path ,
5
+ Result , Signature , Token , Visibility ,
5
6
} ;
6
7
7
8
fn validate_input ( input : & ItemStruct ) -> Result < ( ) > {
@@ -22,9 +23,13 @@ fn validate_input(input: &ItemStruct) -> Result<()> {
22
23
let mut valid_repr = false ;
23
24
for attr in & input. attrs {
24
25
if attr. path ( ) . is_ident ( "repr" ) {
25
- let ident = attr. parse_args :: < Ident > ( ) ?;
26
- if ident == "C" || ident == "transparent" {
27
- valid_repr = true ;
26
+ let nested = attr. parse_args_with ( Punctuated :: < Meta , Token ! [ , ] > :: parse_terminated) ?;
27
+ for meta in nested {
28
+ if let Meta :: Path ( path) = meta {
29
+ if path. is_ident ( "C" ) || path. is_ident ( "transparent" ) {
30
+ valid_repr = true ;
31
+ }
32
+ }
28
33
}
29
34
}
30
35
}
@@ -212,4 +217,36 @@ mod tests {
212
217
213
218
Ok ( ( ) )
214
219
}
220
+
221
+ #[ test]
222
+ fn test_align ( ) -> Result < ( ) > {
223
+ let input = parse_quote ! {
224
+ #[ repr( C , align( 8 ) ) ]
225
+ #[ derive( VolatileFieldAccess ) ]
226
+ pub struct DeviceConfig { }
227
+ } ;
228
+
229
+ let result = derive_volatile ( input) ?;
230
+
231
+ let expected_trait = quote ! {
232
+ #[ allow( non_camel_case_types) ]
233
+ pub trait DeviceConfigVolatileFieldAccess <' a> { }
234
+ } ;
235
+
236
+ let expected_impl = quote ! {
237
+ #[ automatically_derived]
238
+ impl <' a> DeviceConfigVolatileFieldAccess <' a> for :: volatile:: VolatilePtr <' a, DeviceConfig , :: volatile:: access:: ReadWrite > { }
239
+ } ;
240
+
241
+ assert_eq ! (
242
+ expected_trait. to_string( ) ,
243
+ result[ 0 ] . to_token_stream( ) . to_string( )
244
+ ) ;
245
+ assert_eq ! (
246
+ expected_impl. to_string( ) ,
247
+ result[ 1 ] . to_token_stream( ) . to_string( )
248
+ ) ;
249
+
250
+ Ok ( ( ) )
251
+ }
215
252
}
0 commit comments