summary refs log tree commit diff
path: root/src/libsyntax_ext
diff options
context:
space:
mode:
authorvarkor <github@varkor.com>2018-04-12 01:35:14 +0100
committervarkor <github@varkor.com>2018-04-12 01:53:21 +0100
commit105c5180941f4034fd0d576a1d4c1bb71dd8e077 (patch)
treeb7bb8c093d9b9d42ad2482e4bf44f31f1d7f5bae /src/libsyntax_ext
parent60dc4f8ec8729bd851b8ee8e3dac9abae1f62bef (diff)
downloadrust-105c5180941f4034fd0d576a1d4c1bb71dd8e077.tar.gz
rust-105c5180941f4034fd0d576a1d4c1bb71dd8e077.zip
Abstract cs_eq for partial_eq
Diffstat (limited to 'src/libsyntax_ext')
-rw-r--r--src/libsyntax_ext/deriving/cmp/partial_eq.rs77
1 files 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 f62140aa65f..81ca7e73228 100644
--- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs
+++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs
@@ -26,73 +26,50 @@ 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> {
-        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)`"),
-                };
+    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)`"),
+            };
 
-                let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone());
+            cx.expr_binary(span, op, self_f, other_f.clone())
+        };
 
-                cx.expr_binary(span, BinOpKind::And, 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())
-                    }
-                    None => cx.expr_bool(span, true),
-                }
-            },
-            Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
-            cx,
-            span,
-            substr)
-    }
-    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)
+                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::Ne, self_f, other_f.clone())
+                        op(cx, span, self_f, other_fs)
                     }
-                    None => cx.expr_bool(span, false),
+                    None => cx.expr_bool(span, base),
                 }
             },
-            Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
+            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_op(cx, span, substr, BinOpKind::Ne, BinOpKind::Or, false)
+    }
+
     macro_rules! md {
         ($name:expr, $f:ident) => { {
             let inline = cx.meta_word(span, Symbol::intern("inline"));