@@ -45,7 +45,14 @@ macro_rules! register_builtin {
4545
4646register_builtin ! {
4747 ( 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
4956}
5057
5158struct BasicAdtInfo {
@@ -109,36 +116,93 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
109116 result
110117}
111118
112- fn copy_expand (
113- _db : & dyn AstDatabase ,
114- _id : MacroCallId ,
119+ fn expand_simple_derive (
115120 tt : & tt:: Subtree ,
121+ trait_path : tt:: Subtree ,
116122) -> Result < tt:: Subtree , mbe:: ExpandError > {
117123 let info = parse_adt ( tt) ?;
118124 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 ;
120127 let type_params = make_type_args ( info. type_params , bound) ;
121128 let type_args = make_type_args ( info. type_params , Vec :: new ( ) ) ;
129+ let trait_path = trait_path. token_trees ;
122130 let expanded = quote ! {
123- impl ##type_params std :: marker :: Copy for #name ##type_args { }
131+ impl ##type_params ##trait_path for #name ##type_args { }
124132 } ;
125133 Ok ( expanded)
126134}
127135
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+
128144fn clone_expand (
129145 _db : & dyn AstDatabase ,
130146 _id : MacroCallId ,
131147 tt : & tt:: Subtree ,
132148) -> 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 } )
142206}
143207
144208#[ cfg( test) ]
0 commit comments