Blame struct name, better error for padding (#35)

This commit is contained in:
Lucien Greathouse 2020-08-23 19:12:47 -07:00 committed by GitHub
parent e053f8b456
commit 19e61623d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -210,17 +210,16 @@ fn generate_assert_no_padding(
input: &DeriveInput, input: &DeriveInput,
) -> Result<TokenStream, &'static str> { ) -> Result<TokenStream, &'static str> {
let struct_type = &input.ident; let struct_type = &input.ident;
let span = input.span(); let span = input.ident.span();
let fields = get_struct_fields(input)?; let fields = get_struct_fields(input)?;
let field_types = get_field_types(&fields); let field_types = get_field_types(&fields);
let struct_size =
quote_spanned!(span => ::core::mem::size_of::<#struct_type>());
let size_sum = let size_sum =
quote_spanned!(span => 0 #( + ::core::mem::size_of::<#field_types>() )*); quote_spanned!(span => 0 #( + ::core::mem::size_of::<#field_types>() )*);
Ok(quote_spanned! {span => const _: fn() = || { Ok(quote_spanned! {span => const _: fn() = || {
let _ = ::core::mem::transmute::<[u8; #struct_size], [u8; #size_sum]>; struct TypeWithoutPadding([u8; #size_sum]);
let _ = ::core::mem::transmute::<#struct_type, TypeWithoutPadding>;
};}) };})
} }