提交 0da063c9 编写于 作者: N Nicholas Nethercote

Inline and remove the `cs_fold_*` functions.

Because they now have a single call site each.

Also rename `cs_fold1` as `cs_fold`, now that it's the only folding
function left.
上级 0ee79f2c
......@@ -70,7 +70,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
// cmp => cmp
// }
//
let expr = cs_fold1(
let expr = cs_fold(
// foldr nests the if-elses correctly, leaving the first field
// as the outermost one, and the last as the innermost.
false,
......
......@@ -31,7 +31,7 @@ fn cs_op(
cx.expr_binary(span, op, self_f, other_f.clone())
};
let expr = cs_fold1(
let expr = cs_fold(
true, // use foldl
|cx, span, subexpr, self_f, other_fs| {
let eq = op(cx, span, self_f, other_fs);
......
......@@ -68,7 +68,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
// cmp => cmp
// }
//
let expr = cs_fold1(
let expr = cs_fold(
// foldr nests the if-elses correctly, leaving the first field
// as the outermost one, and the last as the innermost.
false,
......
......@@ -1654,46 +1654,6 @@ fn create_enum_variant_pattern(
}
}
// helpful premade recipes
fn cs_fold_fields<'a, F>(
use_foldl: bool,
mut f: F,
base: P<Expr>,
cx: &mut ExtCtxt<'_>,
all_fields: &[FieldInfo<'a>],
) -> P<Expr>
where
F: FnMut(&mut ExtCtxt<'_>, Span, P<Expr>, P<Expr>, &[P<Expr>]) -> P<Expr>,
{
if use_foldl {
all_fields
.iter()
.fold(base, |old, field| f(cx, field.span, old, field.self_.clone(), &field.other))
} else {
all_fields
.iter()
.rev()
.fold(base, |old, field| f(cx, field.span, old, field.self_.clone(), &field.other))
}
}
fn cs_fold_enumnonmatch(
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
cx: &mut ExtCtxt<'_>,
trait_span: Span,
substructure: &Substructure<'_>,
) -> P<Expr> {
match *substructure.fields {
EnumNonMatchingCollapsed(tuple) => enum_nonmatch_f(cx, trait_span, tuple),
_ => cx.span_bug(trait_span, "cs_fold_enumnonmatch expected an EnumNonMatchingCollapsed"),
}
}
fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
cx.span_bug(trait_span, "static function in `derive`")
}
/// Function to fold over fields, with three cases, to generate more efficient and concise code.
/// When the `substructure` has grouped fields, there are two cases:
/// Zero fields: call the base case function with `None` (like the usual base case of `cs_fold`).
......@@ -1702,11 +1662,11 @@ fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
/// fields.
/// When the `substructure` is an `EnumNonMatchingCollapsed`, the result of `enum_nonmatch_f`
/// is returned. Statics may not be folded over.
pub fn cs_fold1<F, B>(
pub fn cs_fold<F, B>(
use_foldl: bool,
f: F,
mut f: F,
mut b: B,
enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
cx: &mut ExtCtxt<'_>,
trait_span: Span,
substructure: &Substructure<'_>,
......@@ -1731,12 +1691,18 @@ pub fn cs_fold1<F, B>(
(true, _) => (b(cx, None), &all_fields[..]),
};
cs_fold_fields(use_foldl, f, base, cx, rest)
}
EnumNonMatchingCollapsed(..) => {
cs_fold_enumnonmatch(enum_nonmatch_f, cx, trait_span, substructure)
if use_foldl {
rest.iter().fold(base, |old, field| {
f(cx, field.span, old, field.self_.clone(), &field.other)
})
} else {
rest.iter().rev().fold(base, |old, field| {
f(cx, field.span, old, field.self_.clone(), &field.other)
})
}
}
StaticEnum(..) | StaticStruct(..) => cs_fold_static(cx, trait_span),
EnumNonMatchingCollapsed(tuple) => enum_nonmatch_f(cx, trait_span, tuple),
StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"),
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册