Skip to content

Commit

Permalink
Utilize proc_macro2 and match
Browse files Browse the repository at this point in the history
  • Loading branch information
zoo868e committed Jun 13, 2024
1 parent 72b0c2e commit a425c2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ macro_rules! assert_size_eq {
macro_rules! static_const_assert {
($x:expr $(,)?) => {
#[allow(unknown_lints, eq_op)]
const _: [(); 0 - !{ const ASSERT: bool = $x; ASSERT } as usize] = [];
const _: [(); 0 - !{
const ASSERT: bool = $x;
ASSERT
} as usize] = [];
};
}

Expand Down
21 changes: 12 additions & 9 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ macro_rules! try_or_print {
/// ```
/// # use zerocopy_derive::assert_size_eq_val;
/// # use static_assertions;
/// # use zerocopy::static_const_assert;
/// #[assert_size_eq_val(4)]
/// struct MyStruct {
/// val: i32
Expand All @@ -70,6 +71,7 @@ macro_rules! try_or_print {
/// ```compile_fail
/// # use zero_derive::assert_size_eq_val;
/// # use static_assertions;
/// # use zerocopy::static_const_assert;
/// #[assert_size_eq_val(1)]
/// struct MyStruct {
/// val: i32
Expand All @@ -81,17 +83,18 @@ pub fn assert_size_eq_val(
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let ast: Result<DeriveInput, _> = syn::parse(input.clone());
let expected_size: Result<usize, _> = args.to_string().trim().parse();
let mut ret: proc_macro::TokenStream = input;
if let (Ok(ast), Ok(size)) = (ast, expected_size) {
let name = &ast.ident;
ret = quote! {
#ast
static_const_assert!(core::mem::size_of::<#name>() == #size);
let args: proc_macro2::TokenStream = args.into();
match ast {
Ok(ast) => {
let name = &ast.ident;
let expand = quote! {
#ast
static_const_assert!(core::mem::size_of::<#name>() == #args);
};
expand.into()
}
.into()
_ => input,
}
ret
}

// TODO(https://github.com/rust-lang/rust/issues/54140): Some errors could be
Expand Down

0 comments on commit a425c2f

Please sign in to comment.