@@ -1654,46 +1654,6 @@ impl<'a> TraitDef<'a> {
1654
1654
}
1655
1655
}
1656
1656
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
-
1697
1657
/// Function to fold over fields, with three cases, to generate more efficient and concise code.
1698
1658
/// When the `substructure` has grouped fields, there are two cases:
1699
1659
/// 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> {
1702
1662
/// fields.
1703
1663
/// When the `substructure` is an `EnumNonMatchingCollapsed`, the result of `enum_nonmatch_f`
1704
1664
/// is returned. Statics may not be folded over.
1705
- pub fn cs_fold1 < F , B > (
1665
+ pub fn cs_fold < F , B > (
1706
1666
use_foldl : bool ,
1707
- f : F ,
1667
+ mut f : F ,
1708
1668
mut b : B ,
1709
- enum_nonmatch_f : EnumNonMatchCollapsedFunc < ' _ > ,
1669
+ mut enum_nonmatch_f : EnumNonMatchCollapsedFunc < ' _ > ,
1710
1670
cx : & mut ExtCtxt < ' _ > ,
1711
1671
trait_span : Span ,
1712
1672
substructure : & Substructure < ' _ > ,
@@ -1731,12 +1691,18 @@ where
1731
1691
( true , _) => ( b ( cx, None ) , & all_fields[ ..] ) ,
1732
1692
} ;
1733
1693
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
+ }
1738
1703
}
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`" ) ,
1740
1706
}
1741
1707
}
1742
1708
0 commit comments