1
1
use proc_macro2:: TokenStream ;
2
2
use quote:: { format_ident, quote} ;
3
- use syn:: { parse_quote, Field , Ident , ItemStruct , Type } ;
3
+ use syn:: { parse_quote, Ident , ItemStruct , Type } ;
4
4
5
5
use crate :: Result ;
6
6
7
7
pub ( crate ) fn impl_joined_value ( input_struct : & ItemStruct ) -> Result < TokenStream > {
8
8
let struct_ident = & input_struct. ident ;
9
9
let ( impl_generics, ty_generics, where_clause) = input_struct. generics . split_for_impl ( ) ;
10
- let ( field_ident, field_type, _) = get_fields_map ( & input_struct. fields ) ?;
11
- let struct_buffer_ident = format_ident ! ( "__bevy_impulse_{}_Buffers" , struct_ident) ;
10
+ let ( field_ident, field_type) = get_fields_map ( & input_struct. fields ) ?;
11
+ let BufferStructConfig {
12
+ struct_name : buffer_struct_ident,
13
+ } = BufferStructConfig :: from_data_struct ( & input_struct) ;
14
+ let buffer_struct_vis = & input_struct. vis ;
12
15
13
16
let buffer_struct: ItemStruct = parse_quote ! {
14
17
#[ derive( Clone ) ]
15
18
#[ allow( non_camel_case_types) ]
16
- struct #struct_buffer_ident #impl_generics #where_clause {
19
+ #buffer_struct_vis struct #buffer_struct_ident #impl_generics #where_clause {
17
20
#(
18
- #field_ident: :: bevy_impulse:: Buffer <#field_type>,
21
+ #buffer_struct_vis # field_ident: :: bevy_impulse:: Buffer <#field_type>,
19
22
) *
20
23
}
21
24
} ;
@@ -25,7 +28,7 @@ pub(crate) fn impl_joined_value(input_struct: &ItemStruct) -> Result<TokenStream
25
28
26
29
let gen = quote ! {
27
30
impl #impl_generics :: bevy_impulse:: JoinedValue for #struct_ident #ty_generics #where_clause {
28
- type Buffers = #struct_buffer_ident #ty_generics;
31
+ type Buffers = #buffer_struct_ident #ty_generics;
29
32
}
30
33
31
34
#buffer_struct
@@ -36,8 +39,8 @@ pub(crate) fn impl_joined_value(input_struct: &ItemStruct) -> Result<TokenStream
36
39
#(
37
40
#field_ident: impl :: bevy_impulse:: Bufferable <BufferType = :: bevy_impulse:: Buffer <#field_type>>,
38
41
) *
39
- ) -> #struct_buffer_ident #ty_generics {
40
- #struct_buffer_ident {
42
+ ) -> #buffer_struct_ident #ty_generics {
43
+ #buffer_struct_ident {
41
44
#(
42
45
#field_ident: #field_ident. into_buffer( builder) ,
43
46
) *
@@ -53,52 +56,50 @@ pub(crate) fn impl_joined_value(input_struct: &ItemStruct) -> Result<TokenStream
53
56
Ok ( gen. into ( ) )
54
57
}
55
58
56
- struct FieldConfig {
57
- buffer_type : TokenStream ,
59
+ struct BufferStructConfig {
60
+ struct_name : Ident ,
58
61
}
59
62
60
- impl FieldConfig {
61
- fn from_field ( field : & Field ) -> Self {
62
- let ty = & field. ty ;
63
- let mut buffer_type = quote ! { :: bevy_impulse:: Buffer <#ty> } ;
63
+ impl BufferStructConfig {
64
+ fn from_data_struct ( data_struct : & ItemStruct ) -> Self {
65
+ let mut config = Self {
66
+ struct_name : format_ident ! ( "__bevy_impulse_{}_Buffers" , data_struct. ident) ,
67
+ } ;
64
68
65
- let attr = field
69
+ let attr = data_struct
66
70
. attrs
67
71
. iter ( )
68
- . find ( |attr| attr. path ( ) . is_ident ( "bevy_impulse " ) ) ;
72
+ . find ( |attr| attr. path ( ) . is_ident ( "buffers " ) ) ;
69
73
70
74
if let Some ( attr) = attr {
71
75
attr. parse_nested_meta ( |meta| {
72
- if meta. path . is_ident ( "buffer_type " ) {
73
- buffer_type = meta. value ( ) ?. parse ( ) ?;
76
+ if meta. path . is_ident ( "struct_name " ) {
77
+ config . struct_name = meta. value ( ) ?. parse ( ) ?;
74
78
}
75
79
Ok ( ( ) )
76
80
} )
77
- // panic if attribute is malformed, this will result in a compile error
81
+ // panic if attribute is malformed, this will result in a compile error which is intended.
78
82
. unwrap ( ) ;
79
83
}
80
84
81
- Self { buffer_type }
85
+ config
82
86
}
83
87
}
84
88
85
- fn get_fields_map ( fields : & syn:: Fields ) -> Result < ( Vec < & Ident > , Vec < & Type > , Vec < FieldConfig > ) > {
89
+ fn get_fields_map ( fields : & syn:: Fields ) -> Result < ( Vec < & Ident > , Vec < & Type > ) > {
86
90
match fields {
87
91
syn:: Fields :: Named ( data) => {
88
92
let mut idents = Vec :: new ( ) ;
89
93
let mut types = Vec :: new ( ) ;
90
- let mut configs = Vec :: new ( ) ;
91
94
for field in & data. named {
92
95
let ident = field
93
96
. ident
94
97
. as_ref ( )
95
98
. ok_or ( "expected named fields" . to_string ( ) ) ?;
96
- let config = FieldConfig :: from_field ( field) ;
97
99
idents. push ( ident) ;
98
100
types. push ( & field. ty ) ;
99
- configs. push ( config) ;
100
101
}
101
- Ok ( ( idents, types, configs ) )
102
+ Ok ( ( idents, types) )
102
103
}
103
104
_ => return Err ( "expected named fields" . to_string ( ) ) ,
104
105
}
@@ -113,7 +114,7 @@ fn impl_buffer_map_layout(
113
114
) -> Result < proc_macro2:: TokenStream > {
114
115
let struct_ident = & buffer_struct. ident ;
115
116
let ( impl_generics, ty_generics, where_clause) = buffer_struct. generics . split_for_impl ( ) ;
116
- let ( field_ident, field_type, _ ) = get_fields_map ( & item_struct. fields ) ?;
117
+ let ( field_ident, field_type) = get_fields_map ( & item_struct. fields ) ?;
117
118
let map_key: Vec < String > = field_ident. iter ( ) . map ( |v| v. to_string ( ) ) . collect ( ) ;
118
119
119
120
Ok ( quote ! {
@@ -154,7 +155,7 @@ fn impl_joined(
154
155
let struct_ident = & joined_struct. ident ;
155
156
let item_struct_ident = & item_struct. ident ;
156
157
let ( impl_generics, ty_generics, where_clause) = item_struct. generics . split_for_impl ( ) ;
157
- let ( field_ident, _, _ ) = get_fields_map ( & item_struct. fields ) ?;
158
+ let ( field_ident, _) = get_fields_map ( & item_struct. fields ) ?;
158
159
159
160
Ok ( quote ! {
160
161
impl #impl_generics :: bevy_impulse:: Joined for #struct_ident #ty_generics #where_clause {
0 commit comments