-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathderive_labelled_generic.rs
174 lines (144 loc) · 6.91 KB
/
derive_labelled_generic.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
use std::iter::repeat;
use frunk_proc_macro_helpers::*;
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::Data;
/// Given an AST, returns an implementation of Generic using HList with
/// Field (see frunk_core::labelled) elements
///
/// Only works with Structs and Tuple Structs
pub fn impl_labelled_generic(input: TokenStream) -> impl ToTokens {
let ast = to_ast(input);
let name = &ast.ident;
let generics = &ast.generics;
let generics_ref = ref_generics(generics);
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let (impl_generics_ref, _, where_clause_ref) = generics_ref.split_for_impl();
#[allow(clippy::let_and_return)]
let tree = match ast.data {
Data::Struct(ref data) => {
let field_bindings = FieldBindings::new(&data.fields);
let repr_type = field_bindings.build_hlist_type(FieldBinding::build_field_type);
let repr_type_ref = field_bindings.build_hlist_type(FieldBinding::build_field_type_ref);
let repr_type_mut = field_bindings.build_hlist_type(FieldBinding::build_field_type_mut);
let hcons_expr = field_bindings.build_hlist_constr(FieldBinding::build_field_expr);
let hcons_pat = field_bindings.build_hlist_constr(FieldBinding::build_field_pat);
let type_constr = field_bindings.build_type_constr(FieldBinding::build);
let type_pat_ref = field_bindings.build_type_constr(FieldBinding::build_pat_ref);
let type_pat_mut = field_bindings.build_type_constr(FieldBinding::build_pat_mut);
quote! {
#[allow(non_snake_case, non_camel_case_types)]
impl #impl_generics ::frunk_core::labelled::LabelledGeneric for #name #ty_generics #where_clause {
type Repr = #repr_type;
#[inline(always)]
fn into(self) -> Self::Repr {
let #name #type_constr = self;
#hcons_expr
}
#[inline(always)]
fn from(r: Self::Repr) -> Self {
let #hcons_pat = r;
#name #type_constr
}
}
#[allow(non_snake_case, non_camel_case_types)]
impl #impl_generics_ref ::frunk_core::labelled::IntoLabelledGeneric for & '_frunk_ref_ #name #ty_generics #where_clause_ref {
type Repr = #repr_type_ref;
#[inline(always)]
fn into(self) -> Self::Repr {
let #name #type_pat_ref = *self;
#hcons_expr
}
}
#[allow(non_snake_case, non_camel_case_types)]
impl #impl_generics_ref ::frunk_core::labelled::IntoLabelledGeneric for & '_frunk_ref_ mut #name #ty_generics #where_clause_ref {
type Repr = #repr_type_mut;
#[inline(always)]
fn into(self) -> Self::Repr {
let #name #type_pat_mut = *self;
#hcons_expr
}
}
}
}
Data::Enum(ref data) => {
let variant_bindings = VariantBindings::new(&data.variants);
let repr_type =
&variant_bindings.build_coprod_type(VariantBinding::build_hlist_field_type);
let repr_type_ref =
&variant_bindings.build_coprod_type(VariantBinding::build_hlist_field_type_ref);
let repr_type_mut =
&variant_bindings.build_coprod_type(VariantBinding::build_hlist_field_type_mut);
let coprod_exprs =
&variant_bindings.build_coprod_constrs(VariantBinding::build_hlist_field_expr);
let coprod_pats =
&variant_bindings.build_coprod_constrs(VariantBinding::build_hlist_field_pat);
let coprod_unreachable = &variant_bindings.build_coprod_unreachable_arm(false);
let type_constrs1 =
&variant_bindings.build_variant_constrs(VariantBinding::build_type_constr);
let type_constrs2 = type_constrs1;
let type_pat_ref =
&variant_bindings.build_variant_constrs(VariantBinding::build_type_pat_ref);
let type_pat_mut =
&variant_bindings.build_variant_constrs(VariantBinding::build_type_pat_mut);
let name_it1 = repeat(name);
let name_it2 = repeat(name);
let name_it3 = repeat(name);
let name_it4 = repeat(name);
let base_impl = quote! {
#[allow(non_snake_case, non_camel_case_types)]
impl #impl_generics ::frunk_core::labelled::LabelledGeneric for #name #ty_generics #where_clause {
type Repr = #repr_type;
#[inline(always)]
fn into(self) -> Self::Repr {
match self {
#(
#name_it1 :: #type_constrs1 => #coprod_exprs,
)*
}
}
#[inline(always)]
fn from(r: Self::Repr) -> Self {
match r {
#(
#coprod_pats => #name_it2 :: #type_constrs2,
)*
#coprod_unreachable
}
}
}
};
let ref_impl = quote! {
#[allow(non_snake_case, non_camel_case_types)]
impl #impl_generics_ref ::frunk_core::labelled::IntoLabelledGeneric for & '_frunk_ref_ #name #ty_generics #where_clause_ref {
type Repr = #repr_type_ref;
#[inline(always)]
fn into(self) -> Self::Repr {
match self {
#(
#name_it3 :: #type_pat_ref => #coprod_exprs,
)*
}
}
}
};
let mut_impl = quote! {
#[allow(non_snake_case, non_camel_case_types)]
impl #impl_generics_ref ::frunk_core::labelled::IntoLabelledGeneric for & '_frunk_ref_ mut #name #ty_generics #where_clause_ref {
type Repr = #repr_type_mut;
#[inline(always)]
fn into(self) -> Self::Repr {
match self {
#(
#name_it4 :: #type_pat_mut => #coprod_exprs,
)*
}
}
}
};
quote! { #base_impl #ref_impl #mut_impl }
}
_ => panic!("Only Structs and Enums can be turned into Labelled Generics."),
};
tree
}