forked from estebank/makeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
350 lines (321 loc) · 13.9 KB
/
lib.rs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::spanned::Spanned;
use syn::{GenericParam, ItemStruct, Visibility};
fn capitalize(mut s: &str) -> String {
s = s.trim_start_matches("r#");
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(ch) => ch.to_uppercase().collect::<String>() + c.as_str(),
}
}
#[proc_macro_derive(Builder, attributes(default))]
pub fn derive_builder(input: TokenStream) -> TokenStream {
let input: ItemStruct = syn::parse(input).unwrap();
let struct_name = &input.ident;
// You can't accuse me of being original. 🤷♂️
let builder_name = format_ident!("{}Builder", input.ident);
// We'll nest the entirety of the builder's helper types in a private module so that they don't
// leak into the user's scope.
let mod_name = format_ident!("{}Fields", builder_name);
let struct_generics = input.generics.params.iter().collect::<Vec<_>>();
// The type parameter names representing each field of the type being built.
let mut set_fields_generics = vec![];
// The type names representing fields that have been initialized.
let mut all_set = vec![];
// The type names representing fields that have not yet been initialized.
let mut all_unset = vec![];
// These are the generic parameters for the `impl` that lets the user call `.build()`. They
// normally would all have to be "field_foo_set" and need no params beyond the underlying
// type's, but we support default values so we need to account for them to let people build
// without setting those.
let mut buildable_generics = vec![];
let mut buildable_generics_use = vec![];
let mut default_where_clauses = vec![];
for (i, field) in input.fields.iter().enumerate() {
// We'll use these as the name of the type parameters for the builder's fields.
let field_name = format_ident!(
"{}",
match &field.ident {
Some(field) => capitalize(&field.to_string()),
None => format!("Field{}", i), // Idents can't start with numbers.
}
);
// We'll use these as the base for the types representing the builder state.
let field_generic_name = format_ident!(
"Field{}",
match &field.ident {
Some(field) => capitalize(&field.to_string()),
None => format!("{}", i),
}
);
let set_field_generic_name = format_ident!("{}Set", field_name);
let unset_field_generic_name = format_ident!("{}Unset", field_name);
let default_attr = field.attrs.iter().find_map(|attr| {
if attr.path.is_ident("default") {
Some(attr.tokens.is_empty())
} else {
None
}
});
if let Some(default_is_empty) = default_attr {
let ty = &field.ty;
buildable_generics.push(field_generic_name.clone());
buildable_generics_use.push(field_generic_name.clone());
if default_is_empty {
default_where_clauses
.push(quote_spanned!(ty.span() => #ty: ::std::default::Default));
}
} else {
buildable_generics_use.push(set_field_generic_name.clone());
}
set_fields_generics.push(field_generic_name);
all_set.push(set_field_generic_name);
all_unset.push(unset_field_generic_name);
}
// `input.generics.params` contains bounds. Here we get only the params without the bounds for
// use in type uses, not `impl` declarations.
let use_struct_generics = input
.generics
.params
.iter()
.map(|param| match param {
GenericParam::Type(p) => {
let ident = &p.ident;
quote!(#ident)
}
GenericParam::Lifetime(p) => {
let lt = &p.lifetime;
quote!(#lt)
}
GenericParam::Const(p) => {
let ident = &p.ident;
quote!(#ident)
}
})
.collect::<Vec<_>>();
let comma = if use_struct_generics.is_empty() {
quote!()
} else {
quote!(,)
};
let constrained_generics = quote!(<#(#struct_generics),* #comma #(#set_fields_generics),*>);
let where_clause = &input.generics.where_clause;
let where_clause = if where_clause.is_some() {
quote!(#where_clause, #(#default_where_clauses),*)
} else {
quote!(where #(#default_where_clauses),*)
};
let use_generics = quote!(<#(#use_struct_generics),* #comma #(#set_fields_generics),*>);
// Construct each of the setter methods. These desugar roughly to the following signature:
//
// fn set_<field_name>(self, value: <field_type>) -> <Type>Builder
//
let setters = input.fields.iter().enumerate().map(|(i, f)| {
let (field, method_name) = match &f.ident {
Some(field) => (quote!(#field), format_ident!("set_{}", field)),
None => {
let i = syn::Index::from(i);
(quote!(#i), format_ident!("set_{}", i))
}
};
let inner_method_name = format_ident!("inner_{}", method_name);
let decl_generics = set_fields_generics
.iter()
.enumerate()
.filter(|(j, _)| i!=*j)
.map(|(_, f)| f);
let decl_generics = quote!(<#(#struct_generics),* #comma #(#decl_generics),*>);
let unset_generics = set_fields_generics
.iter()
.zip(input.fields.iter())
.enumerate()
.map(|(j, (g, f))| if i == j {
// FIXME: dedup this logic.
let field_name = format_ident!("{}", match &f.ident {
Some(field) => capitalize(&field.to_string()),
None => format!("Field{}", i),
});
let f = format_ident!("{}Unset", field_name);
quote!(#f)
} else {
quote!(#g)
});
let unset_generics = quote!(<#(#use_struct_generics),* #comma #(#unset_generics),*>);
let set_generics = set_fields_generics
.iter().zip(input.fields.iter()).enumerate().map(|(j, (g, f))| if i == j {
let field_name = format_ident!("{}", match &f.ident {
Some(field) => capitalize(&field.to_string()),
None => format!("Field{}", i),
});
let f = format_ident!("{}Set", field_name);
quote!(#f)
} else {
quote!(#g)
});
let set_generics = quote!(<#(#use_struct_generics),* #comma #(#set_generics),*>);
let ty = &f.ty;
quote! {
impl #decl_generics #builder_name #unset_generics #where_clause {
#[must_use]
pub fn #method_name(mut self, value: #ty) -> #builder_name #set_generics {
self.#inner_method_name(value);
// We do the following instead of `::core::mem::transmute(self)` here
// because we can't `transmute` on fields that involve generics.
let ptr = &self as *const #builder_name #unset_generics as *const #builder_name #set_generics;
::core::mem::forget(self);
unsafe {
ptr.read()
}
}
fn #inner_method_name(&mut self, value: #ty) {
let inner = self.inner.as_mut_ptr();
// We know that `inner` is a valid pointer that we can write to.
unsafe {
::core::ptr::addr_of_mut!((*inner).#field).write(value);
}
}
}
}
});
let field_ptr_methods = input.fields.iter().enumerate().map(|(i, f)| {
let (field, method_name) = match &f.ident {
Some(field) => (quote!(#field), format_ident!("ptr_{}", i)),
None => {
let i = syn::Index::from(i);
(quote!(#i), format_ident!("ptr_{}", i))
}
};
let ty = &f.ty;
quote! {
/// Returns a mutable pointer to a field of the type being built. This is useful if the
/// initialization requires subtle unsafe shenanigans. You will need to call
/// `.unsafe_build()` after ensuring all of the fields have been initialized.
#[must_use]
pub unsafe fn #method_name(&mut self) -> *mut #ty {
let inner = self.inner.as_mut_ptr();
::core::ptr::addr_of_mut!((*inner).#field)
}
}
});
let vis = match &input.vis {
// For private `struct`s we need to change teh visibility of their builders to be
// accessible from their scope without leaking as `pub`.
Visibility::Inherited => quote!(pub(super)),
vis => quote!(#vis),
};
let defaults = input.fields.iter().enumerate().filter_map(|(i, f)| {
let field = match &f.ident {
Some(field) => format_ident!("inner_set_{}", field),
None => format_ident!("inner_set_{}", i),
};
f.attrs
.iter()
.find(|attr| attr.path.is_ident("default"))
.map(|attr| {
let default = &attr.tokens;
if default.is_empty() {
quote!(builder.#field(::std::default::Default::default());)
} else {
let mut default_iter = default.clone().into_iter();
let default = match [default_iter.next(), default_iter.next()] {
[Some(proc_macro2::TokenTree::Group(group)), None]
if group.delimiter() == proc_macro2::Delimiter::Parenthesis =>
{
group.stream()
}
_ => syn::Error::new_spanned(default, "expected `#[default(…)]`")
.into_compile_error(),
};
quote!(builder.#field(#default);)
}
})
});
// Construct the params for the `impl` item that provides the `build` method. Normally it would
// be straightforward: you just specify that all the type params corresponding to fields are
// set to the `Set` state, but that doesn't account for defaulted type params.
let build_generics = input.generics.params.iter().collect::<Vec<_>>();
let build_generics = if buildable_generics.is_empty() {
quote!(<#(#build_generics),*>)
} else {
let comma = if build_generics.is_empty() {
quote!()
} else {
quote!(,)
};
quote!(<#(#build_generics),* #comma #(#buildable_generics),*>)
};
let build_use_generics =
quote!(<#(#use_struct_generics),* #comma #(#buildable_generics_use),*>);
let builder_assoc_type = quote! {
type Builder = #builder_name<#(#use_struct_generics),* #comma #(#all_unset),*>;
};
let input = quote! {
#[allow(non_snake_case)]
#[deny(unused_must_use, clippy::pedantic)]
mod #mod_name {
use super::*;
#[must_use]
#[repr(transparent)]
#vis struct #builder_name #constrained_generics #where_clause {
inner: ::core::mem::MaybeUninit<#struct_name<#(#use_struct_generics),*>>,
__fields: ::core::marker::PhantomData<(#(#set_fields_generics),*)>,
}
#(pub struct #all_set;)*
#(pub struct #all_unset;)*
impl<#(#struct_generics),*> ::makeit::Buildable for #struct_name <#(#use_struct_generics),*>
#where_clause
{
#builder_assoc_type
/// Returns a builder that lets you initialize `Self` field by field in a zero-cost,
/// type-safe manner.
#[must_use]
#[allow(unused_parens)]
fn builder() -> Self::Builder {
let mut builder = #builder_name {
inner: unsafe {
::core::mem::MaybeUninit::<Self>::uninit()
},
__fields: ::core::marker::PhantomData,
};
#(#defaults)*
builder
}
}
impl #build_generics #builder_name #build_use_generics #where_clause {
/// Finalize the builder.
#[must_use]
pub fn build(self) -> #struct_name<#(#use_struct_generics),*> {
// This method is only callable if all of the fields have been initialized, making
// the underlying value at `inner` correctly formed.
unsafe { self.unsafe_build() }
}
}
#(#setters)*
impl #constrained_generics #builder_name #use_generics #where_clause {
#(#field_ptr_methods)*
/// HERE BE DRAGONS!
///
/// # Safety
///
/// You're dealing with `MaybeUninit`. If you have to research what that is, you don't
/// want this.
#[must_use]
pub unsafe fn maybe_uninit(self) -> ::core::mem::MaybeUninit<#struct_name<#(#use_struct_generics),*>> {
self.inner
}
/// Only call if you have set a field through their mutable pointer, instead
/// of using the type-safe builder. It is your responsibility to ensure that
/// all fields have been set before doing this.
#[must_use]
pub unsafe fn unsafe_build(self) -> #struct_name<#(#use_struct_generics),*> {
self.inner.assume_init()
}
}
}
};
TokenStream::from(input.into_token_stream())
}