Skip to content

Commit

Permalink
Rejigger the assert generation to match current idea
Browse files Browse the repository at this point in the history
Still needs some methods to explicitly reference the constant in
order to produce a compile error;
  • Loading branch information
kitlith committed Sep 13, 2023
1 parent 3071cf9 commit d6e3b12
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions bilge-impl/src/bitsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
};
}
}

Expand Down

0 comments on commit d6e3b12

Please sign in to comment.