Skip to content

Commit 846c5c5

Browse files
authored
Merge pull request #57 from mkroening/macro-repr
fix(macro): support `#[repr(align(N))]`
2 parents 1b97156 + bd7fdac commit 846c5c5

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

volatile-macro/src/volatile.rs

+42-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use quote::format_ident;
2+
use syn::punctuated::Punctuated;
23
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,
56
};
67

78
fn validate_input(input: &ItemStruct) -> Result<()> {
@@ -22,9 +23,13 @@ fn validate_input(input: &ItemStruct) -> Result<()> {
2223
let mut valid_repr = false;
2324
for attr in &input.attrs {
2425
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+
}
2833
}
2934
}
3035
}
@@ -212,4 +217,36 @@ mod tests {
212217

213218
Ok(())
214219
}
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+
}
215252
}

0 commit comments

Comments
 (0)