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
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::{parse_macro_input, parse_str, Ident, ImplItem, ImplItemMethod, ItemImpl, Type};
use quote::quote;
use std::collections::{HashSet, HashMap};
use std::sync::Mutex;
use proc_macro2::Span;
#[macro_use]
extern crate lazy_static;
lazy_static!{
static ref DEFAULT_TRAIT_IMPLS: Mutex<HashMap<String, DefaultTraitImpl>> = Mutex::new(HashMap::new());
}
struct DefaultTraitImpl {
pub trait_name: String,
pub methods: Vec<String>,
}
#[proc_macro_attribute]
pub fn default_trait_impl(_: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemImpl);
let pseudotrait = match *input.self_ty {
Type::Path(type_path) => {
match type_path.path.get_ident() {
Some(ident) => ident.to_string(),
None => return syntax_invalid_error(),
}
},
_ => return syntax_invalid_error(),
};
let trait_name = match input.trait_ {
Some(trait_tuple) => {
match trait_tuple.1.get_ident() {
Some(ident) => ident.to_string(),
None => return syntax_invalid_error(),
}
},
_ => return syntax_invalid_error(),
};
let methods: Vec<String> = input.items.iter().map(|method| {
return quote! {
#method
}.to_string()
}).collect();
DEFAULT_TRAIT_IMPLS.lock().unwrap().insert(pseudotrait, DefaultTraitImpl { trait_name, methods });
TokenStream::new()
}
fn syntax_invalid_error() -> TokenStream {
return quote! {
compile_error!("`default_trait_impl` expects to be given a syntactially valid trait implementation");
}.into()
}
#[proc_macro_attribute]
pub fn trait_impl(_: TokenStream, input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as ItemImpl);
let trait_name = match &input.trait_ {
Some(trait_tuple) => {
match trait_tuple.1.get_ident() {
Some(ident) => ident.to_string(),
None => return syntax_invalid_error(),
}
},
_ => return syntax_invalid_error(),
};
let mut methods = HashSet::new();
for item in &input.items {
if let ImplItem::Method(method) = item {
methods.insert(method.sig.ident.to_string());
}
}
match DEFAULT_TRAIT_IMPLS.lock().unwrap().get(&trait_name) {
Some(default_impl) => {
if let Some(trait_tuple) = &mut input.trait_ {
trait_tuple.1.segments[0].ident = Ident::new(&default_impl.trait_name, Span::call_site());
}
for default_impl_method in &default_impl.methods {
let parsed_default_method: ImplItemMethod = parse_str(default_impl_method).unwrap();
if !methods.contains(&parsed_default_method.sig.ident.to_string()) {
input.items.push(ImplItem::Method(parsed_default_method));
}
}
},
_ => return quote! {
compile_error!("`trait_impl` expects there to be a `default_trait_impl` for the trait it implements");
}.into()
}
let res = quote! {
#input
};
res.into()
}