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
use proc_macro2::{Literal, Span, TokenStream};
use quote::quote;
use syn::{Data, DeriveInput, GenericParam, Ident, Lifetime, LifetimeDef, Meta, NestedMeta, Type};
pub fn impl_event_reader(ast: &DeriveInput) -> TokenStream {
let event_name = &ast.ident;
let mut reader_name: Option<Ident> = None;
for meta in ast
.attrs
.iter()
.filter(|attr| attr.path.segments[0].ident == "reader")
.map(|attr| {
attr.interpret_meta()
.expect("reader attribute incorrectly defined")
})
{
if let Meta::List(l) = meta {
for nested_meta in l.nested.iter() {
match *nested_meta {
NestedMeta::Meta(Meta::Word(ref word)) => {
reader_name = Some(word.clone());
}
_ => panic!("reader attribute does not contain a single name"),
}
}
};
}
let reader_name = reader_name.unwrap_or_else(|| {
panic!(
r#"
#[derive(EventReader)] requested for {}, but #[reader(SomeEventReader)] attribute is missing
Example usage:
#[derive(EventReader)]
#[reader(SomeEventReader)]
pub enum SomeEvent {{
One(Event1),
Two(Event2),
}}
"#,
event_name
)
});
let tys = collect_field_types(&ast.data);
let tys = &tys;
let names = collect_variant_names(&ast.data);
let names = &names;
let type_params_with_defaults = ast.generics.type_params();
let (_, type_generics, where_clause) = ast.generics.split_for_impl();
let mut generics = ast.generics.clone();
let reader_lifetime = Lifetime::new("'r", Span::call_site());
let reader_lifetime_iter = std::iter::repeat(reader_lifetime.clone()).take(tys.len());
generics
.params
.push(GenericParam::Lifetime(LifetimeDef::new(
reader_lifetime.clone(),
)));
let (impl_generics, _, _) = generics.split_for_impl();
let reads: Vec<_> = (0..tys.len())
.map(|n| {
let variant = &names[n];
let tuple_index = Literal::usize_unsuffixed(n);
quote! {
events.extend(
data.#tuple_index.read(
self.#tuple_index
.as_mut()
.expect("ReaderId undefined, has setup been run?")
)
.cloned()
.map(#event_name::#variant)
);
}
})
.collect();
let setups: Vec<_> = (0..tys.len())
.map(|n| {
let ty = &tys[n];
let tuple_index = Literal::usize_unsuffixed(n);
quote! {
self.#tuple_index = Some(world.fetch_mut::<EventChannel<#ty>>().register_reader());
}
})
.collect();
quote! {
#[allow(missing_docs)]
#[derive(Default)]
pub struct #reader_name <#(#type_params_with_defaults),*> (
#(Option<ReaderId<#tys>>, )*
) #where_clause;
impl #impl_generics EventReader<#reader_lifetime> for #reader_name #type_generics
#where_clause
{
type SystemData = (
#(Read<#reader_lifetime_iter, EventChannel<#tys>>),*
);
type Event = #event_name #type_generics;
fn read(&mut self, data: Self::SystemData, events: &mut Vec<#event_name #type_generics>) {
#(#reads)*
}
fn setup(&mut self, world: &mut World) {
<Self::SystemData as SystemData<#reader_lifetime>>::setup(world);
#(#setups)*
}
}
}
}
fn collect_field_types(ast: &Data) -> Vec<Type> {
let variants = match *ast {
Data::Enum(ref variants) => &variants.variants,
_ => panic!("EventReader derive only support enums"),
};
variants
.iter()
.map(|v| {
v.fields
.iter()
.next()
.expect("Event enum variant does not contain an inner event type")
.ty
.clone()
})
.collect()
}
fn collect_variant_names(ast: &Data) -> Vec<Ident> {
let variants = match *ast {
Data::Enum(ref variants) => &variants.variants,
_ => panic!("EventReader derive only support enums"),
};
variants.iter().map(|v| v.ident.clone()).collect()
}