From dbd89d01dcb9a391b1fca8bf069071749b5d8d5e Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 13 Oct 2021 04:29:33 -0400 Subject: [PATCH] Use longer names instead of mod for alignment calculators. This works around rust-lang/rust#83583 by avoiding generating a mod block. This code previously worked everywhere, but tripped this deprecation lint if Crevice's derives were used on a struct inside a function. In order to have consistent name resolution, we instead blast all the generated functions into the main scope. This is... not great, but there is no other way to let code inside a mod inside a fn refer to other items inside the fn. --- crevice-derive/src/layout.rs | 21 +++++++++++-------- .../std140__matrix_uniform_std140.snap | 8 +++---- .../std140__matrix_uniform_std430.snap | 8 +++---- .../std140__more_than_16_alignment.snap | 6 ++++-- tests/snapshots/std140__padding_at_end.snap | 8 +++---- tests/snapshots/std140__point_light.snap | 18 +++++++++------- tests/snapshots/std140__primitive_f32.snap | 12 ++++++----- ...lculations_for_differing_member_sizes.snap | 8 +++---- .../snapshots/std140__using_vec3_padding.snap | 12 ++++++----- tests/snapshots/std140__vec3.snap | 12 ++++++----- 10 files changed, 63 insertions(+), 50 deletions(-) diff --git a/crevice-derive/src/layout.rs b/crevice-derive/src/layout.rs index 4c2e35a..5d54124 100644 --- a/crevice-derive/src/layout.rs +++ b/crevice-derive/src/layout.rs @@ -75,7 +75,6 @@ impl EmitOptions { let name = input.ident; let generated_name = format_ident!("{}{}", layout_name, name); - let alignment_mod_name = format_ident!("{}{}Alignment", layout_name, name); let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); @@ -94,7 +93,14 @@ impl EmitOptions { let align_names: Vec<_> = fields .named .iter() - .map(|field| format_ident!("_{}_align", field.ident.as_ref().unwrap())) + .map(|field| { + format_ident!( + "_{}__{}__{}__align", + name, + field.ident.as_ref().unwrap(), + layout_name + ) + }) .collect(); // Generate one function per field that is used to apply alignment @@ -140,6 +146,7 @@ impl EmitOptions { let field_ty = &field.ty; quote! { + #[allow(non_snake_case)] pub const fn #align_name() -> usize { let mut offset = 0; #( #offset_accumulation )* @@ -171,7 +178,7 @@ impl EmitOptions { let field_name = field.ident.as_ref().unwrap(); quote! { - #align_name: [u8; #alignment_mod_name::#align_name()], + #align_name: [u8; #align_name()], #field_name: <#field_ty as #as_trait_path>::#as_trait_assoc, } }) @@ -231,16 +238,12 @@ impl EmitOptions { }; quote! { - #[allow(non_snake_case)] - mod #alignment_mod_name { - use super::*; - - #( #alignment_calculators )* - } + #( #alignment_calculators )* #[derive(Debug, Clone, Copy)] #type_layout_derive #[repr(C)] + #[allow(non_snake_case)] #visibility struct #generated_name #ty_generics #where_clause { #( #generated_fields )* } diff --git a/tests/snapshots/std140__matrix_uniform_std140.snap b/tests/snapshots/std140__matrix_uniform_std140.snap index 1e42f5e..5de0841 100644 --- a/tests/snapshots/std140__matrix_uniform_std140.snap +++ b/tests/snapshots/std140__matrix_uniform_std140.snap @@ -8,16 +8,16 @@ size: 52 alignment: 4 fields: - Field: - name: _e_align - ty: "[u8 ; Std140MatrixUniformAlignment :: _e_align()]" + name: _MatrixUniform__e__Std140__align + ty: "[u8 ; _MatrixUniform__e__Std140__align()]" size: 0 - Field: name: e ty: "< mint :: ColumnMatrix3 < f32 > as :: crevice :: std140 :: AsStd140 > ::\nStd140Type" size: 48 - Field: - name: _f_align - ty: "[u8 ; Std140MatrixUniformAlignment :: _f_align()]" + name: _MatrixUniform__f__Std140__align + ty: "[u8 ; _MatrixUniform__f__Std140__align()]" size: 0 - Field: name: f diff --git a/tests/snapshots/std140__matrix_uniform_std430.snap b/tests/snapshots/std140__matrix_uniform_std430.snap index 45cf748..05273ca 100644 --- a/tests/snapshots/std140__matrix_uniform_std430.snap +++ b/tests/snapshots/std140__matrix_uniform_std430.snap @@ -8,16 +8,16 @@ size: 52 alignment: 4 fields: - Field: - name: _e_align - ty: "[u8 ; Std430MatrixUniformAlignment :: _e_align()]" + name: _MatrixUniform__e__Std430__align + ty: "[u8 ; _MatrixUniform__e__Std430__align()]" size: 0 - Field: name: e ty: "< mint :: ColumnMatrix3 < f32 > as :: crevice :: std430 :: AsStd430 > ::\nStd430Type" size: 36 - Field: - name: _f_align - ty: "[u8 ; Std430MatrixUniformAlignment :: _f_align()]" + name: _MatrixUniform__f__Std430__align + ty: "[u8 ; _MatrixUniform__f__Std430__align()]" size: 12 - Field: name: f diff --git a/tests/snapshots/std140__more_than_16_alignment.snap b/tests/snapshots/std140__more_than_16_alignment.snap index 99f9a97..e0892fd 100644 --- a/tests/snapshots/std140__more_than_16_alignment.snap +++ b/tests/snapshots/std140__more_than_16_alignment.snap @@ -1,16 +1,18 @@ --- source: tests/std140.rs expression: "<::Std140Type as TypeLayout>::type_layout()" + --- name: Std140MoreThan16Alignment size: 32 alignment: 8 fields: - Field: - name: _doubles_align - ty: "[u8 ; Std140MoreThan16AlignmentAlignment :: _doubles_align()]" + name: _MoreThan16Alignment__doubles__Std140__align + ty: "[u8 ; _MoreThan16Alignment__doubles__Std140__align()]" size: 0 - Field: name: doubles ty: "< DVec4 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 32 + diff --git a/tests/snapshots/std140__padding_at_end.snap b/tests/snapshots/std140__padding_at_end.snap index d909596..fa296a0 100644 --- a/tests/snapshots/std140__padding_at_end.snap +++ b/tests/snapshots/std140__padding_at_end.snap @@ -8,16 +8,16 @@ size: 20 alignment: 4 fields: - Field: - name: _base_value_align - ty: "[u8 ; Std140PaddingAtEndAlignment :: _base_value_align()]" + name: _PaddingAtEnd__base_value__Std140__align + ty: "[u8 ; _PaddingAtEnd__base_value__Std140__align()]" size: 0 - Field: name: base_value ty: "< PrimitiveF32 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 8 - Field: - name: _small_field_align - ty: "[u8 ; Std140PaddingAtEndAlignment :: _small_field_align()]" + name: _PaddingAtEnd__small_field__Std140__align + ty: "[u8 ; _PaddingAtEnd__small_field__Std140__align()]" size: 8 - Field: name: small_field diff --git a/tests/snapshots/std140__point_light.snap b/tests/snapshots/std140__point_light.snap index d40a1d1..20454a9 100644 --- a/tests/snapshots/std140__point_light.snap +++ b/tests/snapshots/std140__point_light.snap @@ -1,40 +1,42 @@ --- source: tests/std140.rs expression: "<::Std140Type as TypeLayout>::type_layout()" + --- name: Std140PointLight size: 48 alignment: 4 fields: - Field: - name: _position_align - ty: "[u8 ; Std140PointLightAlignment :: _position_align()]" + name: _PointLight__position__Std140__align + ty: "[u8 ; _PointLight__position__Std140__align()]" size: 0 - Field: name: position ty: "< Vec3 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 12 - Field: - name: _diffuse_align - ty: "[u8 ; Std140PointLightAlignment :: _diffuse_align()]" + name: _PointLight__diffuse__Std140__align + ty: "[u8 ; _PointLight__diffuse__Std140__align()]" size: 4 - Field: name: diffuse ty: "< Vec3 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 12 - Field: - name: _specular_align - ty: "[u8 ; Std140PointLightAlignment :: _specular_align()]" + name: _PointLight__specular__Std140__align + ty: "[u8 ; _PointLight__specular__Std140__align()]" size: 4 - Field: name: specular ty: "< Vec3 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 12 - Field: - name: _brightness_align - ty: "[u8 ; Std140PointLightAlignment :: _brightness_align()]" + name: _PointLight__brightness__Std140__align + ty: "[u8 ; _PointLight__brightness__Std140__align()]" size: 0 - Field: name: brightness ty: "< f32 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 4 + diff --git a/tests/snapshots/std140__primitive_f32.snap b/tests/snapshots/std140__primitive_f32.snap index dc3b93d..cb44cc8 100644 --- a/tests/snapshots/std140__primitive_f32.snap +++ b/tests/snapshots/std140__primitive_f32.snap @@ -1,24 +1,26 @@ --- source: tests/std140.rs -expression: "<::Std140Type as TypeLayout>::layout()" +expression: "<::Std140Type as TypeLayout>::type_layout()" + --- name: Std140PrimitiveF32 size: 8 alignment: 4 fields: - Field: - name: _x_align - ty: "[u8 ; Std140PrimitiveF32Alignment :: _x_align()]" + name: _PrimitiveF32__x__Std140__align + ty: "[u8 ; _PrimitiveF32__x__Std140__align()]" size: 0 - Field: name: x ty: "< f32 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 4 - Field: - name: _y_align - ty: "[u8 ; Std140PrimitiveF32Alignment :: _y_align()]" + name: _PrimitiveF32__y__Std140__align + ty: "[u8 ; _PrimitiveF32__y__Std140__align()]" size: 0 - Field: name: y ty: "< f32 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 4 + diff --git a/tests/snapshots/std140__proper_offset_calculations_for_differing_member_sizes.snap b/tests/snapshots/std140__proper_offset_calculations_for_differing_member_sizes.snap index f73e8ad..a44d3a0 100644 --- a/tests/snapshots/std140__proper_offset_calculations_for_differing_member_sizes.snap +++ b/tests/snapshots/std140__proper_offset_calculations_for_differing_member_sizes.snap @@ -8,16 +8,16 @@ size: 36 alignment: 4 fields: - Field: - name: _leading_align - ty: "[u8 ; Std140ProperlyChecksForUnderlyingTypeSizeAlignment :: _leading_align()]" + name: _ProperlyChecksForUnderlyingTypeSize__leading__Std140__align + ty: "[u8 ; _ProperlyChecksForUnderlyingTypeSize__leading__Std140__align()]" size: 0 - Field: name: leading ty: "< BaseSizeAndStdSizeAreDifferent as :: crevice :: std140 :: AsStd140 > ::\nStd140Type" size: 20 - Field: - name: _trailing_align - ty: "[u8 ; Std140ProperlyChecksForUnderlyingTypeSizeAlignment :: _trailing_align()]" + name: _ProperlyChecksForUnderlyingTypeSize__trailing__Std140__align + ty: "[u8 ; _ProperlyChecksForUnderlyingTypeSize__trailing__Std140__align()]" size: 12 - Field: name: trailing diff --git a/tests/snapshots/std140__using_vec3_padding.snap b/tests/snapshots/std140__using_vec3_padding.snap index 598e6d0..49effc3 100644 --- a/tests/snapshots/std140__using_vec3_padding.snap +++ b/tests/snapshots/std140__using_vec3_padding.snap @@ -1,24 +1,26 @@ --- source: tests/std140.rs -expression: "<::Std140Type as TypeLayout>::layout()" +expression: "<::Std140Type as TypeLayout>::type_layout()" + --- name: Std140UsingVec3Padding size: 16 alignment: 4 fields: - Field: - name: _pos_align - ty: "[u8 ; Std140UsingVec3PaddingAlignment :: _pos_align()]" + name: _UsingVec3Padding__pos__Std140__align + ty: "[u8 ; _UsingVec3Padding__pos__Std140__align()]" size: 0 - Field: name: pos ty: "< Vec3 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 12 - Field: - name: _brightness_align - ty: "[u8 ; Std140UsingVec3PaddingAlignment :: _brightness_align()]" + name: _UsingVec3Padding__brightness__Std140__align + ty: "[u8 ; _UsingVec3Padding__brightness__Std140__align()]" size: 0 - Field: name: brightness ty: "< f32 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 4 + diff --git a/tests/snapshots/std140__vec3.snap b/tests/snapshots/std140__vec3.snap index fbf9988..764e633 100644 --- a/tests/snapshots/std140__vec3.snap +++ b/tests/snapshots/std140__vec3.snap @@ -1,24 +1,26 @@ --- source: tests/std140.rs -expression: "<::Std140Type as TypeLayout>::layout()" +expression: "<::Std140Type as TypeLayout>::type_layout()" + --- name: Std140TestVec3 size: 28 alignment: 4 fields: - Field: - name: _pos_align - ty: "[u8 ; Std140TestVec3Alignment :: _pos_align()]" + name: _TestVec3__pos__Std140__align + ty: "[u8 ; _TestVec3__pos__Std140__align()]" size: 0 - Field: name: pos ty: "< Vec3 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 12 - Field: - name: _velocity_align - ty: "[u8 ; Std140TestVec3Alignment :: _velocity_align()]" + name: _TestVec3__velocity__Std140__align + ty: "[u8 ; _TestVec3__velocity__Std140__align()]" size: 4 - Field: name: velocity ty: "< Vec3 as :: crevice :: std140 :: AsStd140 > :: Std140Type" size: 12 +