From 105c5180941f4034fd0d576a1d4c1bb71dd8e077 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 12 Apr 2018 01:35:14 +0100 Subject: [PATCH] Abstract cs_eq for partial_eq --- src/libsyntax_ext/deriving/cmp/partial_eq.rs | 77 +++++++------------- 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/src/libsyntax_ext/deriving/cmp/partial_eq.rs b/src/libsyntax_ext/deriving/cmp/partial_eq.rs index f62140aa65fa..81ca7e732283 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs @@ -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 { + fn cs_op(cx: &mut ExtCtxt, + span: Span, + substr: &Substructure, + op: BinOpKind, + combiner: BinOpKind, + base: bool) + -> P + { + let op = |cx: &mut ExtCtxt, span: Span, self_f: P, other_fs: &[P]| { + 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 { + cs_op(cx, span, substr, BinOpKind::Eq, BinOpKind::And, true) + } fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P { - 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 {