@@ -45,7 +45,14 @@ macro_rules! register_builtin {
45
45
46
46
register_builtin ! {
47
47
( COPY_TRAIT , Copy ) => copy_expand,
48
- ( CLONE_TRAIT , Clone ) => clone_expand
48
+ ( CLONE_TRAIT , Clone ) => clone_expand,
49
+ ( DEFAULT_TRAIT , Default ) => default_expand,
50
+ ( DEBUG_TRAIT , Debug ) => debug_expand,
51
+ ( HASH_TRAIT , Hash ) => hash_expand,
52
+ ( ORD_TRAIT , Ord ) => ord_expand,
53
+ ( PARTIAL_ORD_TRAIT , PartialOrd ) => partial_ord_expand,
54
+ ( EQ_TRAIT , Eq ) => eq_expand,
55
+ ( PARTIAL_EQ_TRAIT , PartialEq ) => partial_eq_expand
49
56
}
50
57
51
58
struct BasicAdtInfo {
@@ -109,36 +116,93 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
109
116
result
110
117
}
111
118
112
- fn copy_expand (
113
- _db : & dyn AstDatabase ,
114
- _id : MacroCallId ,
119
+ fn expand_simple_derive (
115
120
tt : & tt:: Subtree ,
121
+ trait_path : tt:: Subtree ,
116
122
) -> Result < tt:: Subtree , mbe:: ExpandError > {
117
123
let info = parse_adt ( tt) ?;
118
124
let name = info. name ;
119
- let bound = ( quote ! { : std:: marker:: Copy } ) . token_trees ;
125
+ let trait_path_clone = trait_path. token_trees . clone ( ) ;
126
+ let bound = ( quote ! { : ##trait_path_clone } ) . token_trees ;
120
127
let type_params = make_type_args ( info. type_params , bound) ;
121
128
let type_args = make_type_args ( info. type_params , Vec :: new ( ) ) ;
129
+ let trait_path = trait_path. token_trees ;
122
130
let expanded = quote ! {
123
- impl ##type_params std :: marker :: Copy for #name ##type_args { }
131
+ impl ##type_params ##trait_path for #name ##type_args { }
124
132
} ;
125
133
Ok ( expanded)
126
134
}
127
135
136
+ fn copy_expand (
137
+ _db : & dyn AstDatabase ,
138
+ _id : MacroCallId ,
139
+ tt : & tt:: Subtree ,
140
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
141
+ expand_simple_derive ( tt, quote ! { std:: marker:: Copy } )
142
+ }
143
+
128
144
fn clone_expand (
129
145
_db : & dyn AstDatabase ,
130
146
_id : MacroCallId ,
131
147
tt : & tt:: Subtree ,
132
148
) -> Result < tt:: Subtree , mbe:: ExpandError > {
133
- let info = parse_adt ( tt) ?;
134
- let name = info. name ;
135
- let bound = ( quote ! { : std:: clone:: Clone } ) . token_trees ;
136
- let type_params = make_type_args ( info. type_params , bound) ;
137
- let type_args = make_type_args ( info. type_params , Vec :: new ( ) ) ;
138
- let expanded = quote ! {
139
- impl ##type_params std:: clone:: Clone for #name ##type_args { }
140
- } ;
141
- Ok ( expanded)
149
+ expand_simple_derive ( tt, quote ! { std:: clone:: Clone } )
150
+ }
151
+
152
+ fn default_expand (
153
+ _db : & dyn AstDatabase ,
154
+ _id : MacroCallId ,
155
+ tt : & tt:: Subtree ,
156
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
157
+ expand_simple_derive ( tt, quote ! { std:: default :: Default } )
158
+ }
159
+
160
+ fn debug_expand (
161
+ _db : & dyn AstDatabase ,
162
+ _id : MacroCallId ,
163
+ tt : & tt:: Subtree ,
164
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
165
+ expand_simple_derive ( tt, quote ! { std:: fmt:: Debug } )
166
+ }
167
+
168
+ fn hash_expand (
169
+ _db : & dyn AstDatabase ,
170
+ _id : MacroCallId ,
171
+ tt : & tt:: Subtree ,
172
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
173
+ expand_simple_derive ( tt, quote ! { std:: hash:: Hash } )
174
+ }
175
+
176
+ fn eq_expand (
177
+ _db : & dyn AstDatabase ,
178
+ _id : MacroCallId ,
179
+ tt : & tt:: Subtree ,
180
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
181
+ expand_simple_derive ( tt, quote ! { std:: cmp:: Eq } )
182
+ }
183
+
184
+ fn partial_eq_expand (
185
+ _db : & dyn AstDatabase ,
186
+ _id : MacroCallId ,
187
+ tt : & tt:: Subtree ,
188
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
189
+ expand_simple_derive ( tt, quote ! { std:: cmp:: PartialEq } )
190
+ }
191
+
192
+ fn ord_expand (
193
+ _db : & dyn AstDatabase ,
194
+ _id : MacroCallId ,
195
+ tt : & tt:: Subtree ,
196
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
197
+ expand_simple_derive ( tt, quote ! { std:: cmp:: Ord } )
198
+ }
199
+
200
+ fn partial_ord_expand (
201
+ _db : & dyn AstDatabase ,
202
+ _id : MacroCallId ,
203
+ tt : & tt:: Subtree ,
204
+ ) -> Result < tt:: Subtree , mbe:: ExpandError > {
205
+ expand_simple_derive ( tt, quote ! { std:: cmp:: PartialOrd } )
142
206
}
143
207
144
208
#[ cfg( test) ]
0 commit comments