various small fixes to the derive macros (#168)

* 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
This commit is contained in:
Tom Dohrmann 2023-01-26 05:49:46 +01:00 committed by GitHub
parent 49c23727fa
commit 2d1f876216
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -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!()
};

View File

@ -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
}
},
))
@ -609,6 +609,8 @@ mk_repr! {
I64 => i64,
I128 => i128,
U128 => u128,
Usize => usize,
Isize => isize,
}
// where
macro_rules! mk_repr {(
@ -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 {