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
#![recursion_limit = "256"]
extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate syn;
use proc_macro::TokenStream;
use quote::Tokens;
use syn::{Body, Field, Ident, Lifetime, LifetimeDef, MacroInput, Ty, TyParam, VariantData,
WhereClause};
#[proc_macro_derive(SystemData)]
pub fn system_data(input: TokenStream) -> TokenStream {
let s = input.to_string();
let ast = syn::parse_macro_input(&s).unwrap();
let gen = impl_system_data(&ast);
gen.parse().expect("Invalid")
}
fn impl_system_data(ast: &MacroInput) -> Tokens {
let name = &ast.ident;
let lifetime_defs = &ast.generics.lifetimes;
let ty_params = &ast.generics.ty_params;
let where_clause = &ast.generics.where_clause;
let (fetch_return, tys) = gen_from_body(&ast.body, name);
let tys = &tys;
let def_fetch_lt = lifetime_defs
.iter()
.next()
.expect("There has to be at least one lifetime");
let ref impl_fetch_lt = def_fetch_lt.lifetime;
let def_lt_tokens = gen_def_lt_tokens(lifetime_defs);
let impl_lt_tokens = gen_impl_lt_tokens(lifetime_defs);
let def_ty_params = gen_def_ty_params(ty_params);
let impl_ty_params = gen_impl_ty_params(ty_params);
let where_clause = gen_where_clause(where_clause, impl_fetch_lt, tys);
quote! {
impl< #def_lt_tokens , #def_ty_params >
::shred::SystemData< #impl_fetch_lt >
for #name< #impl_lt_tokens , #impl_ty_params >
where #where_clause
{
fn setup(res: &mut ::shred::Resources) {
#(
<#tys as ::shred::SystemData> :: setup(res);
)*
}
fn fetch(res: & #impl_fetch_lt ::shred::Resources) -> Self {
#fetch_return
}
fn reads() -> Vec<::shred::ResourceId> {
let mut r = Vec::new();
#( {
let mut reads = <#tys as ::shred::SystemData> :: reads();
r.append(&mut reads);
} )*
r
}
fn writes() -> Vec<::shred::ResourceId> {
let mut r = Vec::new();
#( {
let mut writes = <#tys as ::shred::SystemData> :: writes();
r.append(&mut writes);
} )*
r
}
}
}
}
fn collect_field_types(fields: &Vec<Field>) -> Vec<Ty> {
fields.iter().map(|x| x.ty.clone()).collect()
}
fn gen_identifiers(fields: &Vec<Field>) -> Vec<Ident> {
fields.iter().map(|x| x.ident.clone().unwrap()).collect()
}
fn gen_def_lt_tokens(lifetime_defs: &Vec<LifetimeDef>) -> Tokens {
let lts: Vec<Tokens> = lifetime_defs
.iter()
.map(|x| {
let ref lt = x.lifetime;
let ref bounds = x.bounds;
if bounds.is_empty() {
quote! { #lt }
} else {
quote! { #lt: #( #bounds )+* }
}
})
.collect();
quote! { #( #lts ),* }
}
fn gen_impl_lt_tokens(lifetime_defs: &Vec<LifetimeDef>) -> Tokens {
let lts: Vec<Lifetime> = lifetime_defs.iter().map(|x| x.lifetime.clone()).collect();
quote! { #( #lts ),* }
}
fn gen_def_ty_params(ty_params: &Vec<TyParam>) -> Tokens {
let ty_params: Vec<Tokens> = ty_params
.iter()
.map(|x| {
let ref ty = x.ident;
let ref bounds = x.bounds;
quote! { #ty: #( #bounds )+* }
})
.collect();
quote! { #( #ty_params ),* }
}
fn gen_impl_ty_params(ty_params: &Vec<TyParam>) -> Tokens {
let ty_params: Vec<Ident> = ty_params.iter().map(|x| x.ident.clone()).collect();
quote! { #( #ty_params ),* }
}
fn gen_where_clause(clause: &WhereClause, fetch_lt: &Lifetime, tys: &Vec<Ty>) -> Tokens {
let user_predicates = clause.predicates.iter().map(|x| quote! { #x });
let system_data_predicates = tys.iter().map(|ty| {
quote! { #ty : ::shred::SystemData< #fetch_lt > }
});
let mut tokens = Tokens::new();
tokens.append_separated(user_predicates.chain(system_data_predicates), ",");
tokens
}
fn gen_from_body(ast: &Body, name: &Ident) -> (Tokens, Vec<Ty>) {
enum BodyType {
Struct,
Tuple,
}
let (body, fields) = match *ast {
Body::Struct(VariantData::Struct(ref x)) => (BodyType::Struct, x),
Body::Struct(VariantData::Tuple(ref x)) => (BodyType::Tuple, x),
_ => panic!("Enums are not supported"),
};
let tys = collect_field_types(fields);
let fetch_return = match body {
BodyType::Struct => {
let identifiers = gen_identifiers(fields);
quote! {
#name {
#( #identifiers: ::shred::SystemData::fetch(res) ),*
}
}
}
BodyType::Tuple => {
let count = tys.len();
let fetch = vec![quote! { ::shred::SystemData::fetch(res) }; count];
quote! {
#name ( #( #fetch ),* )
}
}
};
(fetch_return, tys)
}