From d6e3b124b107491cba40d9e42974c3d6afd779eb Mon Sep 17 00:00:00 2001 From: Kitlith Date: Tue, 12 Sep 2023 18:55:40 -0600 Subject: [PATCH] Rejigger the assert generation to match current idea Still needs some methods to explicitly reference the constant in order to produce a compile error; --- bilge-impl/src/bitsize.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/bilge-impl/src/bitsize.rs b/bilge-impl/src/bitsize.rs index db214ee..2e2bc40 100644 --- a/bilge-impl/src/bitsize.rs +++ b/bilge-impl/src/bitsize.rs @@ -139,16 +139,24 @@ fn generate_struct(item: &ItemStruct, declared_bitsize: u8) -> TokenStream { quote! { #item - impl #impl_generics #ident #ty_generics #where_clause { - // constness: when we get const blocks evaluated at compile time, add a const computed_bitsize - const _BITSIZE_CHECK: () = assert!( - (#computed_bitsize) == (#declared_bitsize), - concat!("struct size and declared bit size differ: ", - // stringify!(#computed_bitsize), - " != ", - stringify!(#declared_bitsize)) - ); - } + const _: () = { + // TODO: This is useless without methods that reference it, and this is un-namable outside of this block. + // Move or add some method implementations inside this block? + trait Assertion { + const SIZE_CHECK: (); + } + + impl #impl_generics Assertion for #ident #ty_generics #where_clause { + // constness: when we get const blocks evaluated at compile time, add a const computed_bitsize + const SIZE_CHECK: () = assert!( + (#computed_bitsize) == (#declared_bitsize), + concat!("struct size and declared bit size differ: ", + // stringify!(#computed_bitsize), + " != ", + stringify!(#declared_bitsize)) + ); + } + }; } }