Skip to content

Commit 105c518

Browse files
committed
Abstract cs_eq for partial_eq
1 parent 60dc4f8 commit 105c518

File tree

1 file changed

+27
-50
lines changed

1 file changed

+27
-50
lines changed

src/libsyntax_ext/deriving/cmp/partial_eq.rs

+27-50
Original file line numberDiff line numberDiff line change
@@ -26,73 +26,50 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
2626
push: &mut FnMut(Annotatable)) {
2727
// structures are equal if all fields are equal, and non equal, if
2828
// 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+
};
3642

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+
};
3845

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> {
6346
cs_fold1(true, // use foldl
6447
|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)
7350
},
7451
|cx, args| {
7552
match args {
7653
Some((span, self_f, other_fs)) => {
7754
// 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)
8656
}
87-
None => cx.expr_bool(span, false),
57+
None => cx.expr_bool(span, base),
8858
}
8959
},
90-
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
60+
Box::new(|cx, span, _, _| cx.expr_bool(span, !base)),
9161
cx,
9262
span,
9363
substr)
9464
}
9565

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+
9673
macro_rules! md {
9774
($name:expr, $f:ident) => { {
9875
let inline = cx.meta_word(span, Symbol::intern("inline"));

0 commit comments

Comments
 (0)