Skip to content

Commit 6e20bb8

Browse files
authored
Merge pull request #1667 from dtolnay/punctuatedfold
Optimize punctuated::fold
2 parents 3dfacc1 + 9d95cab commit 6e20bb8

File tree

4 files changed

+63
-53
lines changed

4 files changed

+63
-53
lines changed

codegen/src/fold.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn visit(
4242
let Type::Syn(t) = t else { unimplemented!() };
4343
let method = method_name(t);
4444
Some(quote! {
45-
FoldHelper::lift(#name, f, F::#method)
45+
crate::punctuated::fold(#name, f, F::#method)
4646
})
4747
}
4848
Type::Option(t) => {

src/gen/fold.rs

Lines changed: 39 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen_helper.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[cfg(feature = "fold")]
22
pub(crate) mod fold {
33
use crate::fold::Fold;
4-
use crate::punctuated::{Pair, Punctuated};
54

65
pub(crate) trait FoldHelper {
76
type Item;
@@ -21,20 +20,4 @@ pub(crate) mod fold {
2120
self.into_iter().map(|it| f(fold, it)).collect()
2221
}
2322
}
24-
25-
impl<T, P> FoldHelper for Punctuated<T, P> {
26-
type Item = T;
27-
fn lift<V, F>(self, fold: &mut V, mut f: F) -> Self
28-
where
29-
V: Fold + ?Sized,
30-
F: FnMut(&mut V, Self::Item) -> Self::Item,
31-
{
32-
self.into_pairs()
33-
.map(|pair| match pair {
34-
Pair::Punctuated(t, p) => Pair::Punctuated(f(fold, t), p),
35-
Pair::End(t) => Pair::End(f(fold, t)),
36-
})
37-
.collect()
38-
}
39-
}
4023
}

src/punctuated.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,29 @@ impl<T, P> IndexMut<usize> for Punctuated<T, P> {
10731073
}
10741074
}
10751075

1076+
#[cfg(all(feature = "fold", any(feature = "full", feature = "derive")))]
1077+
pub(crate) fn fold<T, P, V, F>(
1078+
punctuated: Punctuated<T, P>,
1079+
fold: &mut V,
1080+
mut f: F,
1081+
) -> Punctuated<T, P>
1082+
where
1083+
V: ?Sized,
1084+
F: FnMut(&mut V, T) -> T,
1085+
{
1086+
Punctuated {
1087+
inner: punctuated
1088+
.inner
1089+
.into_iter()
1090+
.map(|(t, p)| (f(fold, t), p))
1091+
.collect(),
1092+
last: match punctuated.last {
1093+
Some(t) => Some(Box::new(f(fold, *t))),
1094+
None => None,
1095+
},
1096+
}
1097+
}
1098+
10761099
#[cfg(feature = "printing")]
10771100
mod printing {
10781101
use crate::punctuated::{Pair, Punctuated};

0 commit comments

Comments
 (0)