Skip to content

Commit 977e1e4

Browse files
committed
Reduce genericity in Copied and Cloned
1 parent 92f8e6f commit 977e1e4

File tree

1 file changed

+34
-16
lines changed
  • src/libcore/iter/adapters

1 file changed

+34
-16
lines changed

src/libcore/iter/adapters/mod.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ impl<I> Copied<I> {
143143
}
144144
}
145145

146+
fn copy_fold<T: Copy, Acc>(
147+
mut f: impl FnMut(Acc, T) -> Acc,
148+
) -> impl FnMut(Acc, &T) -> Acc {
149+
move |acc, &elt| f(acc, elt)
150+
}
151+
152+
fn copy_try_fold<T: Copy, Acc, R>(
153+
mut f: impl FnMut(Acc, T) -> R,
154+
) -> impl FnMut(Acc, &T) -> R {
155+
move |acc, &elt| f(acc, elt)
156+
}
157+
146158
#[stable(feature = "iter_copied", since = "1.36.0")]
147159
impl<'a, I, T: 'a> Iterator for Copied<I>
148160
where I: Iterator<Item=&'a T>, T: Copy
@@ -157,16 +169,16 @@ impl<'a, I, T: 'a> Iterator for Copied<I>
157169
self.it.size_hint()
158170
}
159171

160-
fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
172+
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where
161173
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
162174
{
163-
self.it.try_fold(init, move |acc, &elt| f(acc, elt))
175+
self.it.try_fold(init, copy_try_fold(f))
164176
}
165177

166-
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
178+
fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
167179
where F: FnMut(Acc, Self::Item) -> Acc,
168180
{
169-
self.it.fold(init, move |acc, &elt| f(acc, elt))
181+
self.it.fold(init, copy_fold(f))
170182
}
171183
}
172184

@@ -178,16 +190,16 @@ impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
178190
self.it.next_back().copied()
179191
}
180192

181-
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
193+
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where
182194
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
183195
{
184-
self.it.try_rfold(init, move |acc, &elt| f(acc, elt))
196+
self.it.try_rfold(init, copy_try_fold(f))
185197
}
186198

187-
fn rfold<Acc, F>(self, init: Acc, mut f: F) -> Acc
199+
fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
188200
where F: FnMut(Acc, Self::Item) -> Acc,
189201
{
190-
self.it.rfold(init, move |acc, &elt| f(acc, elt))
202+
self.it.rfold(init, copy_fold(f))
191203
}
192204
}
193205

@@ -248,6 +260,12 @@ impl<I> Cloned<I> {
248260
}
249261
}
250262

263+
fn clone_try_fold<T: Clone, Acc, R>(
264+
mut f: impl FnMut(Acc, T) -> R,
265+
) -> impl FnMut(Acc, &T) -> R {
266+
move |acc, elt| f(acc, elt.clone())
267+
}
268+
251269
#[stable(feature = "iter_cloned", since = "1.1.0")]
252270
impl<'a, I, T: 'a> Iterator for Cloned<I>
253271
where I: Iterator<Item=&'a T>, T: Clone
@@ -262,16 +280,16 @@ impl<'a, I, T: 'a> Iterator for Cloned<I>
262280
self.it.size_hint()
263281
}
264282

265-
fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
283+
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where
266284
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
267285
{
268-
self.it.try_fold(init, move |acc, elt| f(acc, elt.clone()))
286+
self.it.try_fold(init, clone_try_fold(f))
269287
}
270288

271-
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
289+
fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
272290
where F: FnMut(Acc, Self::Item) -> Acc,
273291
{
274-
self.it.fold(init, move |acc, elt| f(acc, elt.clone()))
292+
self.it.map(T::clone).fold(init, f)
275293
}
276294
}
277295

@@ -283,16 +301,16 @@ impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
283301
self.it.next_back().cloned()
284302
}
285303

286-
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
304+
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where
287305
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
288306
{
289-
self.it.try_rfold(init, move |acc, elt| f(acc, elt.clone()))
307+
self.it.try_rfold(init, clone_try_fold(f))
290308
}
291309

292-
fn rfold<Acc, F>(self, init: Acc, mut f: F) -> Acc
310+
fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
293311
where F: FnMut(Acc, Self::Item) -> Acc,
294312
{
295-
self.it.rfold(init, move |acc, elt| f(acc, elt.clone()))
313+
self.it.map(T::clone).rfold(init, f)
296314
}
297315
}
298316

0 commit comments

Comments
 (0)