partfun_derive/
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
use proc_macro::TokenStream;
use syn::{Data, DataStruct, DeriveInput, Fields, GenericParam, Generics, TypeParamBound, parse_macro_input, parse_quote};
use syn::spanned::Spanned;
use quote::{quote, quote_spanned};

#[proc_macro_derive(Semigroup)]
pub fn semigroup(input: TokenStream) -> TokenStream {
    let DeriveInput { ident, data, generics, .. } = parse_macro_input!(input);

    let generics = add_trait_bounds(generics, parse_quote!(Semigroup));
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    let combine_fn = match data {
        Data::Struct(DataStruct{ fields: Fields::Named(ref fields), .. }) => {
            let recurse = fields.named.iter().map(|f| {
                let name = &f.ident;
                quote_spanned! {f.span()=>
                    #name: self.#name.combine(rhs.#name)
                }
            });

            quote! {
                fn combine(self, rhs: Self) -> Self {
                    Self {
                        #( #recurse ),*
                    }
                }
            }
        },
        Data::Struct(DataStruct{ fields: Fields::Unnamed(ref fields), .. }) => {
            let i = (0..fields.unnamed.len()).map(syn::Index::from);
            quote! {
                fn combine(self, rhs: Self) -> Self {
                    Self( #( self.#i.combine(rhs.#i) ),* )
                }
            }
        },
        _ => unimplemented!(),
    };

    let output = quote! {
        impl #impl_generics Semigroup for #ident #ty_generics #where_clause {
            #combine_fn
        }
    };

    output.into()
}

#[proc_macro_derive(Monoid)]
pub fn monoid(input: TokenStream) -> TokenStream {
    let DeriveInput { ident, data, generics, .. } = parse_macro_input!(input);

    let generics = add_trait_bounds(generics, parse_quote!(Monoid));
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let monoid_fn = match data {
        Data::Struct(DataStruct{ fields: Fields::Named(ref fields), .. }) => {
            let recurse = fields.named.iter().map(|f| {
                let name = &f.ident;
                quote_spanned! {f.span()=>
                    #name: Monoid::empty()
                }
            });

            quote! {
                fn empty() -> Self {
                    Self {
                        #( #recurse ),*
                    }
                }
            }
        },
        Data::Struct(DataStruct{ fields: Fields::Unnamed(ref fields), .. }) => {
            let recurse = fields.unnamed.iter().map(|f| {
                quote_spanned! {f.span()=>
                    Monoid::empty()
                }
            });

            quote! {
                fn empty() -> Self {
                    Self( #( #recurse ),* )
                }
            }
        },
        _ => unimplemented!(),
    };

    let output = quote! {
        impl #impl_generics Monoid for #ident #ty_generics #where_clause {
            #monoid_fn
        }
    };

    output.into()
}

fn add_trait_bounds(mut generics: Generics, ty: TypeParamBound) -> Generics {
    for param in &mut generics.params {
        if let GenericParam::Type(ref mut type_param) = *param {
            type_param.bounds.push(ty.clone());
        }
    }

    generics
}