butcher_proc_macro 0.3.0

Procedural macros for butcher
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use std::{collections::HashSet, iter};

use syn::{
    parse::{Parse, ParseStream},
    AngleBracketedGenericArguments, Attribute, GenericArgument, Ident, Lifetime, PathArguments,
    QSelf, Result as SynResult, ReturnType, Token, Type, TypeArray, TypeBareFn, TypeGroup,
    TypeParen, TypePath, TypePtr, TypeReference, TypeSlice, TypeTuple, Visibility,
};

use quote::{quote, ToTokens};

use proc_macro2::TokenStream;

use crate::{
    derive_butcher::DeriveError,
    utils::{self, FieldName},
};

fn cow() -> TokenStream {
    quote! { std::borrow::Cow }
}

fn borrowed() -> TokenStream {
    let cow = cow();
    quote! { #cow :: Borrowed }
}

fn owned() -> TokenStream {
    let cow = cow();
    quote! { #cow :: Owned }
}

fn butcher_field() -> TokenStream {
    quote! { butcher::ButcherField }
}

fn phantom() -> TokenStream {
    quote! { std::marker::PhantomData }
}

pub(super) struct Field {
    pub name: FieldName,
    pub method: ButcheringMethod,
    pub vis: Visibility,
    pub ty: Type,
    pub associated_generics: Vec<Ident>,
    pub associated_lifetimes: Vec<Lifetime>,
    additional_traits: Option<TokenStream>,
}

impl Field {
    pub(super) fn from(
        input: syn::Field,
        generic_types: &HashSet<Ident>,
        lifetimes: &HashSet<Lifetime>,
        id: usize,
    ) -> Result<Field, syn::Error> {
        let FieldMetadata(method, additional_traits) = parse_meta_attrs(input.attrs.as_slice())?;

        let vis = input.vis;

        let name = input
            .ident
            .map(FieldName::from)
            .unwrap_or_else(|| FieldName::Unnamed(id));

        let ty = input.ty;

        let mut associated_generics = find_generics_in_type(&ty, generic_types)?;
        let mut associated_lifetimes = find_lifetimes_in_type(&ty, lifetimes)?;

        associated_generics.sort_unstable();
        associated_lifetimes.sort_unstable();

        associated_generics.dedup();
        associated_lifetimes.dedup();

        Ok(Field {
            vis,
            method,
            name,
            ty,
            associated_generics,
            associated_lifetimes,
            additional_traits,
        })
    }

    pub(super) fn expand_to_code(&self, main_struct_name: &Ident, lt: &TokenStream) -> TokenStream {
        let associated_struct = self.associated_struct_declaration(main_struct_name);
        let associated_trait = self.butcher_field_implementation(main_struct_name, lt);

        quote! {
            #associated_struct
            #associated_trait
        }
    }

    pub(super) fn associated_struct_declaration(&self, main_struct_name: &Ident) -> TokenStream {
        let vis = &self.vis;
        let struct_with_generics = self.associated_struct_with_generics(main_struct_name);

        let types_in_phantom = self.associated_generics.iter();
        let lifetimes_in_phantom = self.associated_lifetimes_in_phantom();

        let phantom = phantom();

        quote! {
            #[allow(non_camel_case_types)]
            #vis struct #struct_with_generics
                (
                    #phantom< ( #( #types_in_phantom, )* ) > ,
                    #phantom< ( #( #lifetimes_in_phantom, )* ) > ,
                );
        }
    }

    pub(super) fn associated_struct_with_generics(&self, main_struct_name: &Ident) -> TokenStream {
        let struct_name = self.associated_struct_name(main_struct_name);

        let lifetimes_declaration = self.associated_lifetimes.as_slice();
        let generics_declaration = self.associated_generics.as_slice();

        quote! { #struct_name < #( #lifetimes_declaration, )* #( #generics_declaration, )* > }
    }

    fn associated_struct_name(&self, main_struct_name: &Ident) -> Ident {
        utils::associated_struct_name(main_struct_name, &self.name)
    }

    fn associated_lifetimes_in_phantom(&self) -> impl Iterator<Item = impl ToTokens> + '_ {
        self.associated_lifetimes
            .iter()
            .map(|lt| quote! { & #lt () })
    }

    pub(super) fn butcher_field_implementation(
        &self,
        main_struct_name: &Ident,
        lt: &TokenStream,
    ) -> TokenStream {
        let butcher_field = butcher_field();
        let struct_with_generics = self.associated_struct_with_generics(main_struct_name);

        let generic_types = self.associated_generics.as_slice();
        let lifetimes = self.associated_lifetimes.as_slice();

        let where_clause = self.where_clause_trait(&lt);

        let input_type = self.input_type();
        let output_type = self.output_type(&lt);

        let borrowed_function = self.method.expand_borrowed_function(&lt);
        let owned_function = self.method.expand_owned_function();

        quote! {
            impl
                <#lt, #( #lifetimes, )* #( #generic_types ),*>
                #butcher_field<#lt> for #struct_with_generics
                #where_clause
            {
                #input_type
                #output_type

                #borrowed_function
                #owned_function
            }
        }
    }

    pub(super) fn where_clause_items<'a>(
        &'a self,
        lt: &'a TokenStream,
    ) -> impl Iterator<Item = TokenStream> + 'a {
        let required_by_method = self.method.required_traits_for(&self.ty);

        let bounds_for_generic_types = self
            .associated_generics
            .iter()
            .map(move |t| quote! { #t: #lt });
        let bounds_for_lifetimes = self
            .associated_lifetimes
            .iter()
            .map(move |l| quote! { #l: #lt });

        iter::once(required_by_method)
            .chain(bounds_for_generic_types)
            .chain(bounds_for_lifetimes)
            .chain(self.additional_traits.clone())
    }

    fn where_clause_trait(&self, lt: &TokenStream) -> TokenStream {
        let items = self.where_clause_items(lt);

        quote! {
            where
                #( #items ),*
        }
    }

    fn input_type(&self) -> TokenStream {
        let ty = &self.ty;
        quote! { type Input = #ty; }
    }

    fn output_type(&self, lt: &TokenStream) -> TokenStream {
        self.method.output_type_for(&self.ty, lt)
    }

    fn output_type_unwrapped(&self, lt: &TokenStream) -> TokenStream {
        self.method.output_type_unwrapped(&self.ty, lt)
    }

    pub(super) fn associated_main_struct_data(
        &self,
        lt: &TokenStream,
    ) -> (&FieldName, TokenStream) {
        (&self.name, self.output_type_unwrapped(lt))
    }
}

fn parse_meta_attrs(input: &[Attribute]) -> Result<FieldMetadata, syn::Error> {
    let methods = input
        .iter()
        .map(parse_meta_attr)
        .flatten()
        .collect::<Result<Vec<_>, _>>()?;

    match methods.as_slice() {
        [(_, metadata)] => Ok(metadata.clone()),
        [] => Ok(FieldMetadata(ButcheringMethod::Regular, None)),
        [.., (last, _)] => Err(syn::Error::new_spanned(
            last,
            DeriveError::MultipleButcheringMethod,
        )),
    }
}

fn parse_meta_attr(attr: &Attribute) -> Option<Result<(&Attribute, FieldMetadata), syn::Error>> {
    if !attr.path.is_ident("butcher") {
        return None;
    }

    Some(attr.parse_args::<FieldMetadata>().map(|md| (attr, md)))
}

#[derive(Clone, Debug)]
struct FieldMetadata(ButcheringMethod, Option<TokenStream>);

impl Parse for FieldMetadata {
    fn parse(input: ParseStream) -> SynResult<Self> {
        let method = input.parse::<ButcheringMethod>()?;

        let traits = if input.is_empty() {
            None
        } else {
            let _ = input.parse::<Token![,]>()?;
            Some(input.parse::<TokenStream>()?)
        };

        Ok(FieldMetadata(method, traits))
    }
}

fn find_generics_in_type(ty: &Type, generics: &HashSet<Ident>) -> Result<Vec<Ident>, syn::Error> {
    match ty {
        Type::Array(TypeArray { elem, .. })
        | Type::Group(TypeGroup { elem, .. })
        | Type::Paren(TypeParen { elem, .. })
        | Type::Ptr(TypePtr { elem, .. })
        | Type::Reference(TypeReference { elem, .. })
        | Type::Slice(TypeSlice { elem, .. }) => find_generics_in_type(elem.as_ref(), generics),

        Type::Tuple(TypeTuple { elems, .. }) => elems
            .into_iter()
            .map(|ty| find_generics_in_type(ty, generics))
            .try_fold(Vec::new(), extend_discovered),

        Type::BareFn(TypeBareFn { inputs, output, .. }) => {
            let mut found_generics = inputs
                .into_iter()
                .map(|arg| find_generics_in_type(&arg.ty, generics))
                .try_fold(Vec::new(), extend_discovered)?;

            if let ReturnType::Type(_, ty) = output {
                found_generics.extend(find_generics_in_type(ty.as_ref(), generics)?);
            }

            Ok(found_generics)
        }

        Type::Path(TypePath { path, qself }) => {
            let mut found_generics = path
                .segments
                .iter()
                .filter_map(|s| match &s.arguments {
                    PathArguments::None | PathArguments::Parenthesized(_) => None,
                    PathArguments::AngleBracketed(AngleBracketedGenericArguments {
                        args, ..
                    }) => Some(args),
                })
                .flatten()
                .filter_map(|arg| match arg {
                    GenericArgument::Type(t) => Some(t),
                    _ => None,
                })
                .map(|t| find_generics_in_type(t, generics))
                .try_fold(Vec::new(), extend_discovered)?;

            match path.get_ident() {
                Some(id) if generics.contains(id) => found_generics.push(id.clone()),
                _ => {}
            }

            if let Some(QSelf { ty, .. }) = qself {
                found_generics.extend(find_generics_in_type(ty.as_ref(), generics)?);
            }

            Ok(found_generics)
        }

        Type::ImplTrait(tit) => Err(syn::Error::new_spanned(tit, DeriveError::FoundImplTrait)),

        Type::Macro(m) => Err(syn::Error::new_spanned(m, DeriveError::FoundMacroAsType)),

        Type::TraitObject(to) => Err(syn::Error::new_spanned(to, DeriveError::FoundTraitObject)),

        // For the next three arms, the compiler is going to raise an error
        // anyway.
        Type::Infer(_) => Ok(Vec::new()),
        Type::Never(_) => Ok(Vec::new()),
        Type::Verbatim(_) => Ok(Vec::new()),

        _ => panic!("Unknown type met"),
    }
}

fn find_lifetimes_in_type(ty: &Type, lts: &HashSet<Lifetime>) -> Result<Vec<Lifetime>, syn::Error> {
    match ty {
        Type::Array(TypeArray { elem, .. })
        | Type::Group(TypeGroup { elem, .. })
        | Type::Paren(TypeParen { elem, .. })
        | Type::Ptr(TypePtr { elem, .. })
        | Type::Slice(TypeSlice { elem, .. }) => find_lifetimes_in_type(elem.as_ref(), lts),

        Type::Reference(TypeReference { lifetime, elem, .. }) => {
            let mut found_lifetimes = match lifetime {
                Some(lt) if lts.contains(lt) => vec![lt.clone()],
                Some(_) | None => Vec::new(),
            };

            found_lifetimes.extend(find_lifetimes_in_type(elem.as_ref(), lts)?);

            Ok(found_lifetimes)
        }

        Type::Tuple(TypeTuple { elems, .. }) => elems
            .into_iter()
            .map(|ty| find_lifetimes_in_type(ty, lts))
            .try_fold(Vec::new(), extend_discovered),

        Type::BareFn(TypeBareFn { inputs, output, .. }) => {
            let mut found_lifetimes = inputs
                .into_iter()
                .map(|arg| find_lifetimes_in_type(&arg.ty, lts))
                .try_fold(Vec::new(), extend_discovered)?;

            if let ReturnType::Type(_, ty) = output {
                found_lifetimes.extend(find_lifetimes_in_type(ty.as_ref(), lts)?);
            }

            Ok(found_lifetimes)
        }

        Type::Path(TypePath { path, qself }) => {
            let mut found_lifetimes = path
                .segments
                .iter()
                .filter_map(|s| match &s.arguments {
                    PathArguments::None | PathArguments::Parenthesized(_) => None,
                    PathArguments::AngleBracketed(AngleBracketedGenericArguments {
                        args, ..
                    }) => Some(args),
                })
                .flatten()
                .filter_map(|arg| match arg {
                    GenericArgument::Lifetime(lt) if lts.contains(lt) => Some(Ok(vec![lt.clone()])),
                    GenericArgument::Type(ty) => Some(find_lifetimes_in_type(ty, lts)),
                    _ => None,
                })
                .try_fold(Vec::new(), extend_discovered)?;

            if let Some(QSelf { ty, .. }) = qself {
                found_lifetimes.extend(find_lifetimes_in_type(ty.as_ref(), lts)?);
            }

            Ok(found_lifetimes)
        }

        Type::ImplTrait(tit) => Err(syn::Error::new_spanned(tit, DeriveError::FoundImplTrait)),

        Type::Macro(m) => Err(syn::Error::new_spanned(m, DeriveError::FoundMacroAsType)),

        Type::TraitObject(to) => Err(syn::Error::new_spanned(to, DeriveError::FoundTraitObject)),

        // For the next three arms, the compiler is going to raise an error
        // anyway.
        Type::Infer(_) => Ok(Vec::new()),
        Type::Never(_) => Ok(Vec::new()),
        Type::Verbatim(_) => Ok(Vec::new()),

        _ => panic!("Unknown type met"),
    }
}

fn extend_discovered<T>(
    mut discovered: Vec<T>,
    to_add: Result<Vec<T>, syn::Error>,
) -> Result<Vec<T>, syn::Error> {
    let to_add = to_add?;
    discovered.extend(to_add);
    Ok(discovered)
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub(super) enum ButcheringMethod {
    Copy,
    Flatten,
    Regular,
    Unbox,
}

impl ButcheringMethod {
    fn required_traits_for(&self, ty: &Type) -> TokenStream {
        match self {
            ButcheringMethod::Copy => quote! { #ty: Clone },
            ButcheringMethod::Flatten => {
                quote! { #ty: Into<<<#ty as std::ops::Deref>::Target as ToOwned>::Owned> }
            }
            ButcheringMethod::Regular => quote! { #ty: Clone },
            ButcheringMethod::Unbox => quote! { <#ty as std::ops::Deref>::Target: Clone },
        }
    }

    fn output_type_unwrapped(&self, ty: &Type, lt: &TokenStream) -> TokenStream {
        match self {
            ButcheringMethod::Copy => quote! { #ty },
            ButcheringMethod::Flatten | ButcheringMethod::Unbox => {
                let cow = cow();
                quote! { #cow < #lt , <#ty as std::ops::Deref>::Target > }
            }
            ButcheringMethod::Regular => {
                let cow = cow();
                quote! { #cow < #lt , #ty > }
            }
        }
    }

    fn output_type_for(&self, ty: &Type, lt: &TokenStream) -> TokenStream {
        let ty = self.output_type_unwrapped(ty, lt);
        quote! { type Output = #ty; }
    }

    fn expand_borrowed_function(&self, lt: impl ToTokens) -> TokenStream {
        let var_name = quote! { b };

        let borrowed = borrowed();

        let function_body = match self {
            ButcheringMethod::Copy => quote! { #var_name.clone() },
            ButcheringMethod::Flatten => quote! { #borrowed(std::ops::Deref::deref(#var_name)) },
            ButcheringMethod::Regular => quote! { #borrowed(#var_name) },
            ButcheringMethod::Unbox => quote! { #borrowed(std::ops::Deref::deref(#var_name)) },
        };

        quote! {
            fn from_borrowed(#var_name: & #lt Self::Input) -> Self::Output {
                #function_body
            }
        }
    }

    fn expand_owned_function(&self) -> TokenStream {
        let var_name = quote! { o };

        let owned = owned();

        let function_body = match self {
            ButcheringMethod::Copy => quote! { #var_name },
            ButcheringMethod::Flatten => quote! { #owned(#var_name.into()) },
            ButcheringMethod::Regular => quote! { #owned(#var_name) },
            ButcheringMethod::Unbox => quote! { #owned(*#var_name) },
        };

        quote! {
            fn from_owned(#var_name: Self::Input) -> Self::Output {
                #function_body
            }
        }
    }
}

impl Parse for ButcheringMethod {
    fn parse(input: ParseStream) -> SynResult<Self> {
        let i = input.parse::<Ident>()?;

        if i == "copy" {
            Ok(ButcheringMethod::Copy)
        } else if i == "flatten" {
            Ok(ButcheringMethod::Flatten)
        } else if i == "regular" {
            Ok(ButcheringMethod::Regular)
        } else if i == "unbox" {
            Ok(ButcheringMethod::Unbox)
        } else {
            Err(syn::Error::new_spanned(i, DeriveError::UnknownMethod))
        }
    }
}