8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use ast:: { MetaItem , Item , Expr , self } ;
11
+ use ast:: { MetaItem , Item , Expr } ;
12
12
use codemap:: Span ;
13
13
use ext:: base:: ExtCtxt ;
14
14
use ext:: build:: AstBuilder ;
@@ -17,77 +17,52 @@ use ext::deriving::generic::ty::*;
17
17
use parse:: token:: InternedString ;
18
18
use ptr:: P ;
19
19
20
- pub fn expand_deriving_eq < F > ( cx : & mut ExtCtxt ,
21
- span : Span ,
22
- mitem : & MetaItem ,
23
- item : & Item ,
24
- push : F ) where
25
- F : FnOnce ( P < Item > ) ,
20
+ pub fn expand_deriving_eq ( cx : & mut ExtCtxt ,
21
+ span : Span ,
22
+ mitem : & MetaItem ,
23
+ item : & Item ,
24
+ push : & mut FnMut ( P < Item > ) )
26
25
{
27
- // structures are equal if all fields are equal, and non equal, if
28
- // any fields are not equal or if the enum variants are different
29
- fn cs_eq ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
30
- cs_fold (
31
- true , // use foldl
32
- |cx, span, subexpr, self_f, other_fs| {
33
- let other_f = match other_fs {
34
- [ ref o_f] => o_f,
35
- _ => cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialEq)`" )
36
- } ;
37
-
38
- let eq = cx. expr_binary ( span, ast:: BiEq , self_f, other_f. clone ( ) ) ;
39
-
40
- cx. expr_binary ( span, ast:: BiAnd , subexpr, eq)
41
- } ,
42
- cx. expr_bool ( span, true ) ,
43
- Box :: new ( |cx, span, _, _| cx. expr_bool ( span, false ) ) ,
44
- cx, span, substr)
45
- }
46
- fn cs_ne ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
47
- cs_fold (
48
- true , // use foldl
49
- |cx, span, subexpr, self_f, other_fs| {
50
- let other_f = match other_fs {
51
- [ ref o_f] => o_f,
52
- _ => cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialEq)`" )
53
- } ;
54
-
55
- let eq = cx. expr_binary ( span, ast:: BiNe , self_f, other_f. clone ( ) ) ;
56
-
57
- cx. expr_binary ( span, ast:: BiOr , subexpr, eq)
26
+ fn cs_total_eq_assert ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
27
+ cs_same_method (
28
+ |cx, span, exprs| {
29
+ // create `a.<method>(); b.<method>(); c.<method>(); ...`
30
+ // (where method is `assert_receiver_is_total_eq`)
31
+ let stmts = exprs. into_iter ( ) . map ( |e| cx. stmt_expr ( e) ) . collect ( ) ;
32
+ let block = cx. block ( span, stmts, None ) ;
33
+ cx. expr_block ( block)
58
34
} ,
59
- cx. expr_bool ( span, false ) ,
60
- Box :: new ( |cx, span, _, _| cx. expr_bool ( span, true ) ) ,
61
- cx, span, substr)
35
+ Box :: new ( |cx, sp, _, _| {
36
+ cx. span_bug ( sp, "non matching enums in derive(Eq)?" ) } ) ,
37
+ cx,
38
+ span,
39
+ substr
40
+ )
62
41
}
63
42
64
- macro_rules! md {
65
- ( $name: expr, $f: ident) => { {
66
- let inline = cx. meta_word( span, InternedString :: new( "inline" ) ) ;
67
- let attrs = vec!( cx. attribute( span, inline) ) ;
43
+ let inline = cx. meta_word ( span, InternedString :: new ( "inline" ) ) ;
44
+ let hidden = cx. meta_word ( span, InternedString :: new ( "hidden" ) ) ;
45
+ let doc = cx. meta_list ( span, InternedString :: new ( "doc" ) , vec ! ( hidden) ) ;
46
+ let attrs = vec ! ( cx. attribute( span, inline) ,
47
+ cx. attribute( span, doc) ) ;
48
+ let trait_def = TraitDef {
49
+ span : span,
50
+ attributes : Vec :: new ( ) ,
51
+ path : path_std ! ( cx, core:: cmp:: Eq ) ,
52
+ additional_bounds : Vec :: new ( ) ,
53
+ generics : LifetimeBounds :: empty ( ) ,
54
+ methods : vec ! (
68
55
MethodDef {
69
- name: $name ,
56
+ name: "assert_receiver_is_total_eq" ,
70
57
generics: LifetimeBounds :: empty( ) ,
71
58
explicit_self: borrowed_explicit_self( ) ,
72
- args: vec!( borrowed_self ( ) ) ,
73
- ret_ty: Literal ( path_local! ( bool ) ) ,
59
+ args: vec!( ) ,
60
+ ret_ty: nil_ty ( ) ,
74
61
attributes: attrs,
75
62
combine_substructure: combine_substructure( Box :: new( |a, b, c| {
76
- $f ( a, b, c)
63
+ cs_total_eq_assert ( a, b, c)
77
64
} ) )
78
65
}
79
- } }
80
- }
81
-
82
- let trait_def = TraitDef {
83
- span : span,
84
- attributes : Vec :: new ( ) ,
85
- path : path_std ! ( cx, core:: cmp:: PartialEq ) ,
86
- additional_bounds : Vec :: new ( ) ,
87
- generics : LifetimeBounds :: empty ( ) ,
88
- methods : vec ! (
89
- md!( "eq" , cs_eq) ,
90
- md!( "ne" , cs_ne)
91
66
) ,
92
67
associated_types : Vec :: new ( ) ,
93
68
} ;
0 commit comments