Skip to content

Commit

Permalink
Use longer names instead of mod for alignment calculators.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LPGhatguy committed Oct 13, 2021
1 parent 88a09d0 commit dbd89d0
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 50 deletions.
21 changes: 12 additions & 9 deletions crevice-derive/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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 )*
Expand Down Expand Up @@ -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,
}
})
Expand Down Expand Up @@ -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 )*
}
Expand Down
8 changes: 4 additions & 4 deletions tests/snapshots/std140__matrix_uniform_std140.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/snapshots/std140__matrix_uniform_std430.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/snapshots/std140__more_than_16_alignment.snap
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
source: tests/std140.rs
expression: "<<MoreThan16Alignment as AsStd140>::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

8 changes: 4 additions & 4 deletions tests/snapshots/std140__padding_at_end.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions tests/snapshots/std140__point_light.snap
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
---
source: tests/std140.rs
expression: "<<PointLight as AsStd140>::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

12 changes: 7 additions & 5 deletions tests/snapshots/std140__primitive_f32.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
---
source: tests/std140.rs
expression: "<<PrimitiveF32 as AsStd140>::Std140Type as TypeLayout>::layout()"
expression: "<<PrimitiveF32 as AsStd140>::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

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions tests/snapshots/std140__using_vec3_padding.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
---
source: tests/std140.rs
expression: "<<UsingVec3Padding as AsStd140>::Std140Type as TypeLayout>::layout()"
expression: "<<UsingVec3Padding as AsStd140>::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

12 changes: 7 additions & 5 deletions tests/snapshots/std140__vec3.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
---
source: tests/std140.rs
expression: "<<TestVec3 as AsStd140>::Std140Type as TypeLayout>::layout()"
expression: "<<TestVec3 as AsStd140>::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

0 comments on commit dbd89d0

Please sign in to comment.