Skip to content

Commit 2592609

Browse files
authored
Rollup merge of #102300 - scottmcm:simpler-fold-closures, r=Mark-Simulacrum
Use a macro to not have to copy-paste `ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0` everywhere Also use that macro to replace a bunch of places that had custom closure-wrappers. +35 -114 sounds good to me.
2 parents e42c4d7 + 55492de commit 2592609

File tree

9 files changed

+35
-114
lines changed

9 files changed

+35
-114
lines changed

library/core/src/iter/adapters/array_chunks.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::array;
2-
use crate::const_closure::ConstFnMutClosure;
32
use crate::iter::{ByRefSized, FusedIterator, Iterator};
4-
use crate::ops::{ControlFlow, NeverShortCircuit, Try};
3+
use crate::ops::{ControlFlow, Try};
54

65
/// An iterator over `N` elements of the iterator at a time.
76
///
@@ -83,13 +82,7 @@ where
8382
}
8483
}
8584

86-
fn fold<B, F>(mut self, init: B, mut f: F) -> B
87-
where
88-
Self: Sized,
89-
F: FnMut(B, Self::Item) -> B,
90-
{
91-
self.try_fold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
92-
}
85+
impl_fold_via_try_fold! { fold -> try_fold }
9386
}
9487

9588
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
@@ -127,13 +120,7 @@ where
127120
try { acc }
128121
}
129122

130-
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
131-
where
132-
Self: Sized,
133-
F: FnMut(B, Self::Item) -> B,
134-
{
135-
self.try_rfold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
136-
}
123+
impl_fold_via_try_fold! { rfold -> try_rfold }
137124
}
138125

139126
impl<I, const N: usize> ArrayChunks<I, N>

library/core/src/iter/adapters/map_while.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,7 @@ where
6464
.into_try()
6565
}
6666

67-
#[inline]
68-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
69-
where
70-
Self: Sized,
71-
Fold: FnMut(Acc, Self::Item) -> Acc,
72-
{
73-
#[inline]
74-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
75-
move |acc, x| Ok(f(acc, x))
76-
}
77-
78-
self.try_fold(init, ok(fold)).unwrap()
79-
}
67+
impl_fold_via_try_fold! { fold -> try_fold }
8068
}
8169

8270
#[unstable(issue = "none", feature = "inplace_iteration")]

library/core/src/iter/adapters/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::const_closure::ConstFnMutClosure;
21
use crate::iter::{InPlaceIterable, Iterator};
3-
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, NeverShortCircuit, Residual, Try};
2+
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
43

54
mod array_chunks;
65
mod by_ref_sized;
@@ -204,13 +203,7 @@ where
204203
.into_try()
205204
}
206205

207-
fn fold<B, F>(mut self, init: B, mut fold: F) -> B
208-
where
209-
Self: Sized,
210-
F: FnMut(B, Self::Item) -> B,
211-
{
212-
self.try_fold(init, ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0
213-
}
206+
impl_fold_via_try_fold! { fold -> try_fold }
214207
}
215208

216209
#[unstable(issue = "none", feature = "inplace_iteration")]

library/core/src/iter/adapters/scan.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,7 @@ where
7474
self.iter.try_fold(init, scan(state, f, fold)).into_try()
7575
}
7676

77-
#[inline]
78-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
79-
where
80-
Self: Sized,
81-
Fold: FnMut(Acc, Self::Item) -> Acc,
82-
{
83-
#[inline]
84-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
85-
move |acc, x| Ok(f(acc, x))
86-
}
87-
88-
self.try_fold(init, ok(fold)).unwrap()
89-
}
77+
impl_fold_via_try_fold! { fold -> try_fold }
9078
}
9179

9280
#[unstable(issue = "none", feature = "inplace_iteration")]

library/core/src/iter/adapters/skip.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,7 @@ where
206206
if n == 0 { try { init } } else { self.iter.try_rfold(init, check(n, fold)).into_try() }
207207
}
208208

209-
fn rfold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
210-
where
211-
Fold: FnMut(Acc, Self::Item) -> Acc,
212-
{
213-
#[inline]
214-
fn ok<Acc, T>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, T) -> Result<Acc, !> {
215-
move |acc, x| Ok(f(acc, x))
216-
}
217-
218-
self.try_rfold(init, ok(fold)).unwrap()
219-
}
209+
impl_fold_via_try_fold! { rfold -> try_rfold }
220210

221211
#[inline]
222212
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {

library/core/src/iter/adapters/take.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,7 @@ where
9898
}
9999
}
100100

101-
#[inline]
102-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
103-
where
104-
Self: Sized,
105-
Fold: FnMut(Acc, Self::Item) -> Acc,
106-
{
107-
#[inline]
108-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
109-
move |acc, x| Ok(f(acc, x))
110-
}
111-
112-
self.try_fold(init, ok(fold)).unwrap()
113-
}
101+
impl_fold_via_try_fold! { fold -> try_fold }
114102

115103
#[inline]
116104
#[rustc_inherit_overflow_checks]

library/core/src/iter/adapters/take_while.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,7 @@ where
9494
}
9595
}
9696

97-
#[inline]
98-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
99-
where
100-
Self: Sized,
101-
Fold: FnMut(Acc, Self::Item) -> Acc,
102-
{
103-
#[inline]
104-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
105-
move |acc, x| Ok(f(acc, x))
106-
}
107-
108-
self.try_fold(init, ok(fold)).unwrap()
109-
}
97+
impl_fold_via_try_fold! { fold -> try_fold }
11098
}
11199

112100
#[stable(feature = "fused", since = "1.26.0")]

library/core/src/iter/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,29 @@
352352
353353
#![stable(feature = "rust1", since = "1.0.0")]
354354

355+
// This needs to be up here in order to be usable in the child modules
356+
macro_rules! impl_fold_via_try_fold {
357+
(fold -> try_fold) => {
358+
impl_fold_via_try_fold! { @internal fold -> try_fold }
359+
};
360+
(rfold -> try_rfold) => {
361+
impl_fold_via_try_fold! { @internal rfold -> try_rfold }
362+
};
363+
(@internal $fold:ident -> $try_fold:ident) => {
364+
#[inline]
365+
fn $fold<AAA, FFF>(mut self, init: AAA, mut fold: FFF) -> AAA
366+
where
367+
FFF: FnMut(AAA, Self::Item) -> AAA,
368+
{
369+
use crate::const_closure::ConstFnMutClosure;
370+
use crate::ops::NeverShortCircuit;
371+
372+
let fold = ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp);
373+
self.$try_fold(init, fold).0
374+
}
375+
};
376+
}
377+
355378
#[stable(feature = "rust1", since = "1.0.0")]
356379
pub use self::traits::Iterator;
357380

library/core/src/iter/range.rs

+2-26
Original file line numberDiff line numberDiff line change
@@ -1150,19 +1150,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
11501150
self.spec_try_fold(init, f)
11511151
}
11521152

1153-
#[inline]
1154-
fn fold<B, F>(mut self, init: B, f: F) -> B
1155-
where
1156-
Self: Sized,
1157-
F: FnMut(B, Self::Item) -> B,
1158-
{
1159-
#[inline]
1160-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
1161-
move |acc, x| Ok(f(acc, x))
1162-
}
1163-
1164-
self.try_fold(init, ok(f)).unwrap()
1165-
}
1153+
impl_fold_via_try_fold! { fold -> try_fold }
11661154

11671155
#[inline]
11681156
fn last(mut self) -> Option<A> {
@@ -1230,19 +1218,7 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
12301218
self.spec_try_rfold(init, f)
12311219
}
12321220

1233-
#[inline]
1234-
fn rfold<B, F>(mut self, init: B, f: F) -> B
1235-
where
1236-
Self: Sized,
1237-
F: FnMut(B, Self::Item) -> B,
1238-
{
1239-
#[inline]
1240-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
1241-
move |acc, x| Ok(f(acc, x))
1242-
}
1243-
1244-
self.try_rfold(init, ok(f)).unwrap()
1245-
}
1221+
impl_fold_via_try_fold! { rfold -> try_rfold }
12461222
}
12471223

12481224
// Safety: See above implementation for `ops::Range<A>`

0 commit comments

Comments
 (0)