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
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Field, Fields};
#[proc_macro_derive(HListSupport)]
pub fn hlist_support_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let data_struct: DataStruct;
if let Data::Struct(s) = input.data {
data_struct = s
} else {
panic!("`HListSupport` may only be applied to structs")
}
let fields: Fields;
if let Fields::Named(_) = data_struct.fields {
fields = data_struct.fields
} else {
panic!("`HListSupport` may only be applied to structs with named fields")
}
let struct_name = &input.ident;
let hlist_type = hlist_type(fields.iter());
let hlist_pat = hlist_pattern(fields.iter());
let struct_field_init = struct_field_init(fields.iter());
let hlist_cloned_init = hlist_cloned_init(fields.iter());
let hlist_init = hlist_init(fields.iter());
let expanded = quote! {
#[allow(dead_code)]
impl FromHList<#hlist_type> for #struct_name {
fn from_hlist(hlist: #hlist_type) -> Self {
match hlist {
#hlist_pat => #struct_name { #struct_field_init }
}
}
}
#[allow(dead_code)]
impl ToHList<#hlist_type> for #struct_name {
fn to_hlist(&self) -> #hlist_type {
#hlist_cloned_init
}
}
#[allow(dead_code)]
impl IntoHList<#hlist_type> for #struct_name {
fn into_hlist(self) -> #hlist_type {
#hlist_init
}
}
};
TokenStream::from(expanded)
}
fn hlist_type(mut fields: syn::punctuated::Iter<Field>) -> proc_macro2::TokenStream {
match fields.next() {
Some(field) => {
let lhs = field.ty.to_token_stream();
let rhs = hlist_type(fields);
quote!(HCons<#lhs, #rhs>)
}
None => quote!(HNil),
}
}
fn hlist_pattern(mut fields: syn::punctuated::Iter<Field>) -> proc_macro2::TokenStream {
match fields.next() {
Some(field) => {
let lhs = field.ident.as_ref();
let rhs = hlist_pattern(fields);
quote!(HCons(#lhs, #rhs))
}
None => quote!(HNil),
}
}
fn hlist_init(mut fields: syn::punctuated::Iter<Field>) -> proc_macro2::TokenStream {
match fields.next() {
Some(field) => {
let lhs = field.ident.as_ref();
let rhs = hlist_init(fields);
quote!(HCons(self.#lhs, #rhs))
}
None => quote!(HNil),
}
}
fn hlist_cloned_init(mut fields: syn::punctuated::Iter<Field>) -> proc_macro2::TokenStream {
match fields.next() {
Some(field) => {
let lhs = field.ident.as_ref();
let rhs = hlist_cloned_init(fields);
quote!(HCons(self.#lhs.clone(), #rhs))
}
None => quote!(HNil),
}
}
fn struct_field_init(fields: syn::punctuated::Iter<Field>) -> proc_macro2::TokenStream {
let field_names = fields.map(|f| f.ident.as_ref());
quote!(#(#field_names),*)
}