Abstract cs_eq for partial_eq

This commit is contained in:
varkor 2018-04-12 01:35:14 +01:00
parent 60dc4f8ec8
commit 105c518094

View file

@ -26,71 +26,48 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
push: &mut FnMut(Annotatable)) {
// structures are equal if all fields are equal, and non equal, if
// any fields are not equal or if the enum variants are different
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
fn cs_op(cx: &mut ExtCtxt,
span: Span,
substr: &Substructure,
op: BinOpKind,
combiner: BinOpKind,
base: bool)
-> P<Expr>
{
let op = |cx: &mut ExtCtxt, span: Span, self_f: P<Expr>, other_fs: &[P<Expr>]| {
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
};
cx.expr_binary(span, op, self_f, other_f.clone())
};
cs_fold1(true, // use foldl
|cx, span, subexpr, self_f, other_fs| {
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
};
let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone());
cx.expr_binary(span, BinOpKind::And, subexpr, eq)
let eq = op(cx, span, self_f, other_fs);
cx.expr_binary(span, combiner, subexpr, eq)
},
|cx, args| {
match args {
Some((span, self_f, other_fs)) => {
// Special-case the base case to generate cleaner code.
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
_ => {
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
}
};
cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone())
op(cx, span, self_f, other_fs)
}
None => cx.expr_bool(span, true),
None => cx.expr_bool(span, base),
}
},
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
Box::new(|cx, span, _, _| cx.expr_bool(span, !base)),
cx,
span,
substr)
}
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
cs_op(cx, span, substr, BinOpKind::Eq, BinOpKind::And, true)
}
fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
cs_fold1(true, // use foldl
|cx, span, subexpr, self_f, other_fs| {
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
};
let eq = cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone());
cx.expr_binary(span, BinOpKind::Or, subexpr, eq)
},
|cx, args| {
match args {
Some((span, self_f, other_fs)) => {
// Special-case the base case to generate cleaner code.
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
_ => {
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
}
};
cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone())
}
None => cx.expr_bool(span, false),
}
},
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
cx,
span,
substr)
cs_op(cx, span, substr, BinOpKind::Ne, BinOpKind::Or, false)
}
macro_rules! md {