1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::vec::Vec;
use std::string::String;
use proc_macro::{self, TokenStream};
use quote::quote;
use syn::parse_macro_input;
use syn::DeriveInput;
use syn::FieldsNamed;
use proc_macro2::Span;
use syn::Fields;
use syn::Data;
use syn::Type;
use syn::PathArguments;
use syn::GenericArgument;
use syn::Ident;
use syn::Visibility;
fn expand_type_name(t: &syn::Type) -> String
{
let mut s = String::new();
match t
{
Type::Path(p) =>
{
for v in &p.path.segments
{
s.push_str(&v.ident.to_string());
s.push_str("::");
}
return String::from(&s[0..s.len() - 2]);
}
_ => panic!("Invalid generic type name for ComponentPool")
}
}
#[proc_macro_derive(ComponentManager)]
pub fn component_manager(input: TokenStream) -> TokenStream
{
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
let mut v = Vec::new();
match data
{
Data::Struct(s) => match s.fields
{
Fields::Named(FieldsNamed { named, .. }) =>
{
for f in &named
{
match &f.ty
{
Type::Path(p) =>
{
let last = &p.path.segments.last().unwrap();
if let PathArguments::AngleBracketed(b) = &last.arguments
{
if let Some(v1) = b.args.first()
{
if let GenericArgument::Type(t) = v1
{
let name = expand_type_name(&t);
if let Some(useless) = &f.ident
{
v.push((useless.clone(), name));
}
else
{
panic!("How is it possible that you get no identifier???!!!")
}
}
else
{
panic!("Could not identify type of component for field {:?}", f.ident);
}
}
else
{
panic!("Could not identify type of component for field {:?}", f.ident);
}
}
},
_ => panic!("Could not identify type of component for field {:?}", f.ident),
}
}
},
_ => panic!("Your component list must not be empty")
},
_ => panic!("ComponentManager cannot be implemented on non-structs")
};
let mut impl_base_tokens = Vec::new();
for (field_name, _) in &v
{
impl_base_tokens.push(
quote!
{
#field_name: ComponentPool::new()
}
);
};
let mut impls_tokens = Vec::new();
for (field_name, component_type) in &v
{
let mut s = component_type.clone();
s.push_str("Manager");
let new_ident = syn::parse_str::<Type>(&s).unwrap();
let new_ident1 = syn::parse_str::<Type>(&component_type).unwrap();
let mgr_impl_tokens = quote!
{
impl #new_ident for #ident
{
fn get(&mut self, id: usize) -> &mut #new_ident1
{
return self.#field_name.get(id);
}
fn get_pool(&mut self) -> &mut ComponentPool<#new_ident1>
{
return &mut self.#field_name;
}
}
};
impls_tokens.push(mgr_impl_tokens);
}
let output = quote!
{
impl #ident
{
pub fn new() -> #ident
{
return #ident
{
#(#impl_base_tokens,)*
};
}
}
#(#impls_tokens)*
};
return output.into();
}
#[proc_macro_derive(Component)]
pub fn component(input: TokenStream) -> TokenStream
{
let DeriveInput { ident, vis, .. } = parse_macro_input!(input);
let mut s = ident.to_string();
s.push_str("Manager");
let new_ident = Ident::new(&s, Span::call_site());
let vis = match vis
{
Visibility::Public(_) => Some(Ident::new("pub", Span::call_site())),
_ => None
};
let output = quote!
{
#vis trait #new_ident
{
fn get(&mut self, id: usize) -> &mut #ident;
fn get_pool(&mut self) -> &mut ComponentPool<#ident>;
}
};
return output.into();
}