Skip to content

Commit

Permalink
various small fixes to the derive macros (#168)
Browse files Browse the repository at this point in the history
* parse `repr(usize)` and `repr(isize)`

This can be used with enums.

* emit packed repr without type suffix

Previously we emitted the packed attribute with a type suffix for the
value `packed(4u32)`. This is not allowed. Instead we should emit
`packed(4)`.

* read bits instead of creating reference

This is required for packed structs where creating a reference to
fields is not allowed. We can make a copy of the bits because the bits
are `AnyBitPattern` which implies `Copy`.

* emit generics on implied traits
  • Loading branch information
Freax13 authored Jan 26, 2023
1 parent 49c2372 commit 2d1f876
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ fn derive_marker_trait_inner<Trait: Derivable>(
let (trait_impl_extras, trait_impl) = Trait::trait_impl(&input)?;

let implies_trait = if let Some(implies_trait) = Trait::implies_trait() {
quote!(unsafe impl #implies_trait for #name {})
quote!(unsafe impl #impl_generics #implies_trait for #name #ty_generics #where_clause {})
} else {
quote!()
};
Expand Down
9 changes: 7 additions & 2 deletions derive/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ fn generate_checked_bit_pattern_struct(
#[inline]
#[allow(clippy::double_comparisons)]
fn is_valid_bit_pattern(bits: &#bits_ty) -> bool {
#(<#field_ty as ::bytemuck::CheckedBitPattern>::is_valid_bit_pattern(&bits.#field_name) && )* true
#(<#field_ty as ::bytemuck::CheckedBitPattern>::is_valid_bit_pattern(&{ bits.#field_name }) && )* true
}
},
))
Expand Down Expand Up @@ -609,6 +609,8 @@ mk_repr! {
I64 => i64,
I128 => i128,
U128 => u128,
Usize => usize,
Isize => isize,
}
// where
macro_rules! mk_repr {(
Expand Down Expand Up @@ -705,7 +707,10 @@ macro_rules! mk_repr {(
Repr::$Xn => Some(quote!($xn)),
)*
};
let packed = self.packed.map(|p| quote!(packed(#p)));
let packed = self.packed.map(|p| {
let lit = LitInt::new(&p.to_string(), Span::call_site());
quote!(packed(#lit))
});
let comma = if packed.is_some() && repr.is_some() {
Some(quote!(,))
} else {
Expand Down

0 comments on commit 2d1f876

Please sign in to comment.