@@ -26,73 +26,50 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
26
26
push : & mut FnMut ( Annotatable ) ) {
27
27
// structures are equal if all fields are equal, and non equal, if
28
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_fold1 ( true , // use foldl
31
- |cx, span, subexpr, self_f, other_fs| {
32
- let other_f = match ( other_fs. len ( ) , other_fs. get ( 0 ) ) {
33
- ( 1 , Some ( o_f) ) => o_f,
34
- _ => cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialEq)`" ) ,
35
- } ;
29
+ fn cs_op ( cx : & mut ExtCtxt ,
30
+ span : Span ,
31
+ substr : & Substructure ,
32
+ op : BinOpKind ,
33
+ combiner : BinOpKind ,
34
+ base : bool )
35
+ -> P < Expr >
36
+ {
37
+ let op = |cx : & mut ExtCtxt , span : Span , self_f : P < Expr > , other_fs : & [ P < Expr > ] | {
38
+ let other_f = match ( other_fs. len ( ) , other_fs. get ( 0 ) ) {
39
+ ( 1 , Some ( o_f) ) => o_f,
40
+ _ => cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialEq)`" ) ,
41
+ } ;
36
42
37
- let eq = cx. expr_binary ( span, BinOpKind :: Eq , self_f, other_f. clone ( ) ) ;
43
+ cx. expr_binary ( span, op, self_f, other_f. clone ( ) )
44
+ } ;
38
45
39
- cx. expr_binary ( span, BinOpKind :: And , subexpr, eq)
40
- } ,
41
- |cx, args| {
42
- match args {
43
- Some ( ( span, self_f, other_fs) ) => {
44
- // Special-case the base case to generate cleaner code.
45
- let other_f = match ( other_fs. len ( ) , other_fs. get ( 0 ) ) {
46
- ( 1 , Some ( o_f) ) => o_f,
47
- _ => {
48
- cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialEq)`" )
49
- }
50
- } ;
51
-
52
- cx. expr_binary ( span, BinOpKind :: Eq , self_f, other_f. clone ( ) )
53
- }
54
- None => cx. expr_bool ( span, true ) ,
55
- }
56
- } ,
57
- Box :: new ( |cx, span, _, _| cx. expr_bool ( span, false ) ) ,
58
- cx,
59
- span,
60
- substr)
61
- }
62
- fn cs_ne ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
63
46
cs_fold1 ( true , // use foldl
64
47
|cx, span, subexpr, self_f, other_fs| {
65
- let other_f = match ( other_fs. len ( ) , other_fs. get ( 0 ) ) {
66
- ( 1 , Some ( o_f) ) => o_f,
67
- _ => cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialEq)`" ) ,
68
- } ;
69
-
70
- let eq = cx. expr_binary ( span, BinOpKind :: Ne , self_f, other_f. clone ( ) ) ;
71
-
72
- cx. expr_binary ( span, BinOpKind :: Or , subexpr, eq)
48
+ let eq = op ( cx, span, self_f, other_fs) ;
49
+ cx. expr_binary ( span, combiner, subexpr, eq)
73
50
} ,
74
51
|cx, args| {
75
52
match args {
76
53
Some ( ( span, self_f, other_fs) ) => {
77
54
// Special-case the base case to generate cleaner code.
78
- let other_f = match ( other_fs. len ( ) , other_fs. get ( 0 ) ) {
79
- ( 1 , Some ( o_f) ) => o_f,
80
- _ => {
81
- cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialEq)`" )
82
- }
83
- } ;
84
-
85
- cx. expr_binary ( span, BinOpKind :: Ne , self_f, other_f. clone ( ) )
55
+ op ( cx, span, self_f, other_fs)
86
56
}
87
- None => cx. expr_bool ( span, false ) ,
57
+ None => cx. expr_bool ( span, base ) ,
88
58
}
89
59
} ,
90
- Box :: new ( |cx, span, _, _| cx. expr_bool ( span, true ) ) ,
60
+ Box :: new ( |cx, span, _, _| cx. expr_bool ( span, !base ) ) ,
91
61
cx,
92
62
span,
93
63
substr)
94
64
}
95
65
66
+ fn cs_eq ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
67
+ cs_op ( cx, span, substr, BinOpKind :: Eq , BinOpKind :: And , true )
68
+ }
69
+ fn cs_ne ( cx : & mut ExtCtxt , span : Span , substr : & Substructure ) -> P < Expr > {
70
+ cs_op ( cx, span, substr, BinOpKind :: Ne , BinOpKind :: Or , false )
71
+ }
72
+
96
73
macro_rules! md {
97
74
( $name: expr, $f: ident) => { {
98
75
let inline = cx. meta_word( span, Symbol :: intern( "inline" ) ) ;
0 commit comments