From 2d1f87621683bbc5bb300dcb013d8d35d1c9f34d Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Thu, 26 Jan 2023 05:49:46 +0100 Subject: [PATCH] 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 --- derive/src/lib.rs | 2 +- derive/src/traits.rs | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/derive/src/lib.rs b/derive/src/lib.rs index e16d43e..448e503 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -334,7 +334,7 @@ fn derive_marker_trait_inner( 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!() }; diff --git a/derive/src/traits.rs b/derive/src/traits.rs index 0f4e0a9..1e6920b 100644 --- a/derive/src/traits.rs +++ b/derive/src/traits.rs @@ -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 {