Commit f048da2b authored by Simon Sapin's avatar Simon Sapin
Browse files

Add a redundant padding check in the derive macro

parent bb3a7571358c
[package]
name = "bytes-cast-derive"
version = "0.1.0"
version = "0.1.1"
authors = ["Octobus"]
license = "Zlib OR Apache-2.0 OR MIT"
repository = "https://foss.heptapod.net/octobus/rust/bytes-cast"
......
......@@ -33,14 +33,24 @@ fn derive_inner(input: DeriveInput) -> Result<TokenStream, &'static str> {
let name = &input.ident;
let span = input.ident.span();
let field_types = fields.iter().map(|field| &field.ty);
let field_types2 = field_types.clone();
let asserts = quote_spanned!(span =>
const _: fn() = || {
let _static_assert_align_1: [(); 1] =
[(); ::core::mem::align_of::<#name>()];
// A struct whose size is the sum of the sizes of its fields cannot
// have padding between fields. This check is somewhat redundant
// since a `#[repr(C)]` struct with `align_of() == 1` is also know
// not to have padding
// (since all fields must also have `align_of() == 1`),
// but it doesn’t hurt to check.
let _static_assert_no_padding: [(); ::core::mem::size_of::<#name>()] =
[(); 0 #( + ::core::mem::size_of::<#field_types>() )*];
fn _static_assert_is_bytes_cast<T: BytesCast>() {}
#(
_static_assert_is_bytes_cast::<#field_types>();
_static_assert_is_bytes_cast::<#field_types2>();
)*
};
);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment