@@ -1654,46 +1654,6 @@ impl<'a> TraitDef<'a> {
16541654 }
16551655}
16561656
1657- // helpful premade recipes
1658-
1659- fn cs_fold_fields < ' a , F > (
1660- use_foldl : bool ,
1661- mut f : F ,
1662- base : P < Expr > ,
1663- cx : & mut ExtCtxt < ' _ > ,
1664- all_fields : & [ FieldInfo < ' a > ] ,
1665- ) -> P < Expr >
1666- where
1667- F : FnMut ( & mut ExtCtxt < ' _ > , Span , P < Expr > , P < Expr > , & [ P < Expr > ] ) -> P < Expr > ,
1668- {
1669- if use_foldl {
1670- all_fields
1671- . iter ( )
1672- . fold ( base, |old, field| f ( cx, field. span , old, field. self_ . clone ( ) , & field. other ) )
1673- } else {
1674- all_fields
1675- . iter ( )
1676- . rev ( )
1677- . fold ( base, |old, field| f ( cx, field. span , old, field. self_ . clone ( ) , & field. other ) )
1678- }
1679- }
1680-
1681- fn cs_fold_enumnonmatch (
1682- mut enum_nonmatch_f : EnumNonMatchCollapsedFunc < ' _ > ,
1683- cx : & mut ExtCtxt < ' _ > ,
1684- trait_span : Span ,
1685- substructure : & Substructure < ' _ > ,
1686- ) -> P < Expr > {
1687- match * substructure. fields {
1688- EnumNonMatchingCollapsed ( tuple) => enum_nonmatch_f ( cx, trait_span, tuple) ,
1689- _ => cx. span_bug ( trait_span, "cs_fold_enumnonmatch expected an EnumNonMatchingCollapsed" ) ,
1690- }
1691- }
1692-
1693- fn cs_fold_static ( cx : & mut ExtCtxt < ' _ > , trait_span : Span ) -> P < Expr > {
1694- cx. span_bug ( trait_span, "static function in `derive`" )
1695- }
1696-
16971657/// Function to fold over fields, with three cases, to generate more efficient and concise code.
16981658/// When the `substructure` has grouped fields, there are two cases:
16991659/// 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> {
17021662/// fields.
17031663/// When the `substructure` is an `EnumNonMatchingCollapsed`, the result of `enum_nonmatch_f`
17041664/// is returned. Statics may not be folded over.
1705- pub fn cs_fold1 < F , B > (
1665+ pub fn cs_fold < F , B > (
17061666 use_foldl : bool ,
1707- f : F ,
1667+ mut f : F ,
17081668 mut b : B ,
1709- enum_nonmatch_f : EnumNonMatchCollapsedFunc < ' _ > ,
1669+ mut enum_nonmatch_f : EnumNonMatchCollapsedFunc < ' _ > ,
17101670 cx : & mut ExtCtxt < ' _ > ,
17111671 trait_span : Span ,
17121672 substructure : & Substructure < ' _ > ,
@@ -1731,12 +1691,18 @@ where
17311691 ( true , _) => ( b ( cx, None ) , & all_fields[ ..] ) ,
17321692 } ;
17331693
1734- cs_fold_fields ( use_foldl, f, base, cx, rest)
1735- }
1736- EnumNonMatchingCollapsed ( ..) => {
1737- cs_fold_enumnonmatch ( enum_nonmatch_f, cx, trait_span, substructure)
1694+ if use_foldl {
1695+ rest. iter ( ) . fold ( base, |old, field| {
1696+ f ( cx, field. span , old, field. self_ . clone ( ) , & field. other )
1697+ } )
1698+ } else {
1699+ rest. iter ( ) . rev ( ) . fold ( base, |old, field| {
1700+ f ( cx, field. span , old, field. self_ . clone ( ) , & field. other )
1701+ } )
1702+ }
17381703 }
1739- StaticEnum ( ..) | StaticStruct ( ..) => cs_fold_static ( cx, trait_span) ,
1704+ EnumNonMatchingCollapsed ( tuple) => enum_nonmatch_f ( cx, trait_span, tuple) ,
1705+ StaticEnum ( ..) | StaticStruct ( ..) => cx. span_bug ( trait_span, "static function in `derive`" ) ,
17401706 }
17411707}
17421708
0 commit comments