Skip to content

Commit

Permalink
Potential fix for asserting bitsize on generic structs
Browse files Browse the repository at this point in the history
  • Loading branch information
kitlith committed Aug 30, 2023
1 parent 503fa47 commit 8c44e5b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
20 changes: 12 additions & 8 deletions bilge-impl/src/bitsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,21 @@ fn generate_struct(item: &ItemStruct, declared_bitsize: u8) -> TokenStream {
}
};

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

quote! {
#vis struct #ident #generics #fields_def

// constness: when we get const blocks evaluated at compile time, add a const computed_bitsize
const _: () = assert!(
(#computed_bitsize) == (#declared_bitsize),
concat!("struct size and declared bit size differ: ",
// stringify!(#computed_bitsize),
" != ",
stringify!(#declared_bitsize))
);
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))
);
}
}
}

Expand Down
7 changes: 3 additions & 4 deletions tests/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,10 @@ impl_from!(T; Generic<T> => u2; |val| val.0);
#[derive(DefaultBits, PartialEq, DebugBits, FromBits)]
struct UsingGeneric(Generic<()>);

// TODO: bitsize doesn't work here yet because of the size check: it's trying to get the size of Generic<T> in a non-generic context
// #[bitsize(2)]
#[bitsize(2)]
// FromBits, DefaultBits, and DebugBits don't work yet because they need the generics threaded through to the correct spot.
// #[derive(PartialEq)] // TODO: FromBits, DefaultBits, DebugBits
// struct IsGeneric<T>(Generic<T>);
#[derive(PartialEq)] // TODO: FromBits, DefaultBits, DebugBits
struct IsGeneric<T>(Generic<T>);

#[bitsize(2)]
#[derive(DefaultBits, PartialEq, DebugBits, TryFromBits)]
Expand Down

0 comments on commit 8c44e5b

Please sign in to comment.