Skip to content

Commit 88ed2d1

Browse files
author
Jorge Aparicio
committed
Use operator sugar in the expansion of #[deriving(PartialEq)]
1 parent 63c4f22 commit 88ed2d1

File tree

1 file changed

+31
-5
lines changed
  • src/libsyntax/ext/deriving/cmp

1 file changed

+31
-5
lines changed

src/libsyntax/ext/deriving/cmp/eq.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Item, Expr};
11+
use ast::{MetaItem, Item, Expr, mod};
1212
use codemap::Span;
1313
use ext::base::ExtCtxt;
1414
use ext::build::AstBuilder;
@@ -25,12 +25,38 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
2525
// structures are equal if all fields are equal, and non equal, if
2626
// any fields are not equal or if the enum variants are different
2727
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
28-
cs_and(|cx, span, _, _| cx.expr_bool(span, false),
29-
cx, span, substr)
28+
cs_fold(
29+
true, // use foldl
30+
|cx, span, subexpr, self_f, other_fs| {
31+
let other_f = match other_fs {
32+
[ref o_f] => o_f,
33+
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Eq)`")
34+
};
35+
36+
let eq = cx.expr_binary(span, ast::BiEq, self_f, other_f.clone());
37+
38+
cx.expr_binary(span, ast::BiAnd, subexpr, eq)
39+
},
40+
cx.expr_bool(span, true),
41+
|cx, span, _, _| cx.expr_bool(span, false),
42+
cx, span, substr)
3043
}
3144
fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
32-
cs_or(|cx, span, _, _| cx.expr_bool(span, true),
33-
cx, span, substr)
45+
cs_fold(
46+
true, // use foldl
47+
|cx, span, subexpr, self_f, other_fs| {
48+
let other_f = match other_fs {
49+
[ref o_f] => o_f,
50+
_ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Eq)`")
51+
};
52+
53+
let eq = cx.expr_binary(span, ast::BiNe, self_f, other_f.clone());
54+
55+
cx.expr_binary(span, ast::BiOr, subexpr, eq)
56+
},
57+
cx.expr_bool(span, false),
58+
|cx, span, _, _| cx.expr_bool(span, true),
59+
cx, span, substr)
3460
}
3561

3662
macro_rules! md (

0 commit comments

Comments
 (0)