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
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(SixtyFPSElement, attributes(rtti_field))]
pub fn sixtyfps_element(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
let fields = match &input.data {
syn::Data::Struct(syn::DataStruct { fields: f @ syn::Fields::Named(..), .. }) => f,
_ => {
return syn::Error::new(
input.ident.span(),
"Only `struct` with named field are supported",
)
.to_compile_error()
.into()
}
};
let mut pub_prop_field_names = Vec::new();
let mut pub_prop_field_names_normalized = Vec::new();
let mut pub_prop_field_types = Vec::new();
let mut property_names = Vec::new();
let mut property_visibility = Vec::new();
let mut property_types = Vec::new();
for field in fields {
if let Some(property_type) = property_type(&field.ty) {
let name = field.ident.as_ref().unwrap();
if matches!(field.vis, syn::Visibility::Public(_)) {
pub_prop_field_names_normalized.push(normalize_identifier(name));
pub_prop_field_names.push(name);
pub_prop_field_types.push(&field.ty);
}
property_names.push(name);
property_visibility.push(field.vis.clone());
property_types.push(property_type);
}
}
let (plain_field_names, plain_field_types): (Vec<_>, Vec<_>) = fields
.iter()
.filter(|f| {
f.attrs.iter().any(|attr| {
attr.parse_meta()
.ok()
.map(|meta| match meta {
syn::Meta::Path(path) => {
path.get_ident().map(|ident| *ident == "rtti_field").unwrap_or(false)
}
_ => false,
})
.unwrap_or(false)
})
})
.map(|f| (f.ident.as_ref().unwrap(), &f.ty))
.unzip();
let plain_field_names_normalized =
plain_field_names.iter().map(|f| normalize_identifier(*f)).collect::<Vec<_>>();
let mut callback_field_names = Vec::new();
let mut callback_field_names_normalized = Vec::new();
let mut callback_args = Vec::new();
let mut callback_rets = Vec::new();
for field in fields {
if let Some((arg, ret)) = callback_arg(&field.ty) {
if matches!(field.vis, syn::Visibility::Public(_)) {
let name = field.ident.as_ref().unwrap();
callback_field_names_normalized.push(normalize_identifier(name));
callback_field_names.push(name);
callback_args.push(arg);
callback_rets.push(ret);
}
}
}
let item_name = &input.ident;
quote!(
#[allow(clippy::nonstandard_macro_braces)]
#[cfg(feature = "rtti")]
impl BuiltinItem for #item_name {
fn name() -> &'static str {
stringify!(#item_name)
}
fn properties<Value: ValueType>() -> ::alloc::vec::Vec<(&'static str, &'static dyn PropertyInfo<Self, Value>)> {
::alloc::vec![#( {
const O : MaybeAnimatedPropertyInfoWrapper<#item_name, #pub_prop_field_types> =
MaybeAnimatedPropertyInfoWrapper(#item_name::FIELD_OFFSETS.#pub_prop_field_names);
(#pub_prop_field_names_normalized, (&O).as_property_info())
} ),*]
}
fn fields<Value: ValueType>() -> ::alloc::vec::Vec<(&'static str, &'static dyn FieldInfo<Self, Value>)> {
::alloc::vec![#( {
const O : const_field_offset::FieldOffset<#item_name, #plain_field_types, const_field_offset::AllowPin> =
#item_name::FIELD_OFFSETS.#plain_field_names;
(#plain_field_names_normalized, &O as &'static dyn FieldInfo<Self, Value>)
} ),*]
}
fn callbacks<Value: ValueType>() -> ::alloc::vec::Vec<(&'static str, &'static dyn CallbackInfo<Self, Value>)> {
::alloc::vec![#( {
const O : const_field_offset::FieldOffset<#item_name, Callback<#callback_args, #callback_rets>, const_field_offset::AllowPin> =
#item_name::FIELD_OFFSETS.#callback_field_names;
(#callback_field_names_normalized, &O as &'static dyn CallbackInfo<Self, Value>)
} ),*]
}
}
impl #item_name {
#(
#property_visibility fn #property_names(self: core::pin::Pin<&Self>) -> #property_types {
Self::FIELD_OFFSETS.#property_names.apply_pin(self).get()
}
)*
}
)
.into()
}
fn normalize_identifier(name: &syn::Ident) -> String {
name.to_string().replace('_', "-")
}
fn property_type(ty: &syn::Type) -> Option<&syn::Type> {
if let syn::Type::Path(syn::TypePath { path: syn::Path { segments, .. }, .. }) = ty {
if let Some(syn::PathSegment {
ident,
arguments:
syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { args, .. }),
}) = segments.first()
{
match args.first() {
Some(syn::GenericArgument::Type(property_type)) if *ident == "Property" => {
return Some(property_type)
}
_ => {}
}
}
}
None
}
fn callback_arg(ty: &syn::Type) -> Option<(&syn::Type, Option<&syn::Type>)> {
if let syn::Type::Path(syn::TypePath { path: syn::Path { segments, .. }, .. }) = ty {
if let Some(syn::PathSegment {
ident,
arguments:
syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { args, .. }),
}) = segments.first()
{
if ident != "Callback" {
return None;
}
let mut it = args.iter();
let first = match it.next() {
Some(syn::GenericArgument::Type(ty)) => ty,
_ => return None,
};
let sec = match it.next() {
Some(syn::GenericArgument::Type(ty)) => Some(ty),
_ => None,
};
return Some((first, sec));
}
}
None
}