Remove redundant operation in derive[PartialEq]
This commit is contained in:
parent
3238e0d098
commit
1ce06d0932
@ -27,40 +27,70 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
|
||||
// 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_fold(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)`"),
|
||||
};
|
||||
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());
|
||||
let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone());
|
||||
|
||||
cx.expr_binary(span, BinOpKind::And, subexpr, eq)
|
||||
},
|
||||
cx.expr_bool(span, true),
|
||||
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
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_fold(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)`"),
|
||||
};
|
||||
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());
|
||||
let eq = cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone());
|
||||
|
||||
cx.expr_binary(span, BinOpKind::Or, subexpr, eq)
|
||||
},
|
||||
cx.expr_bool(span, false),
|
||||
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
|
||||
cx,
|
||||
span,
|
||||
substr)
|
||||
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)
|
||||
}
|
||||
|
||||
macro_rules! md {
|
||||
|
Loading…
x
Reference in New Issue
Block a user