Skip to content

Commit 8a6d677

Browse files
committed
Avoid unused clones in Cloned<I> and Copied<I>
Avoid cloning in `Cloned<I>` or copying in `Copied<I>` when elements are only needed by reference or not at all. There is already some precedent for this, given that `__iterator_get_unchecked` is implemented, which can skip elements. The reduced clones are technically observable by a user impl of `Clone`.
1 parent 65fa0ab commit 8a6d677

File tree

2 files changed

+167
-20
lines changed

2 files changed

+167
-20
lines changed

Diff for: library/core/src/iter/adapters/cloned.rs

+88-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::num::NonZero;
22

3+
use crate::cmp::Ordering;
34
use crate::iter::adapters::zip::try_get_unchecked;
45
use crate::iter::adapters::{SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
56
use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen, UncheckedIterator};
@@ -41,13 +42,31 @@ where
4142
self.it.next().cloned()
4243
}
4344

45+
#[inline]
4446
fn size_hint(&self) -> (usize, Option<usize>) {
4547
self.it.size_hint()
4648
}
4749

50+
#[inline]
51+
fn count(self) -> usize {
52+
self.it.count()
53+
}
54+
55+
fn last(self) -> Option<T> {
56+
self.it.last().cloned()
57+
}
58+
59+
#[inline]
60+
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
61+
self.it.advance_by(n)
62+
}
63+
64+
fn nth(&mut self, n: usize) -> Option<T> {
65+
self.it.nth(n).cloned()
66+
}
67+
4868
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
4969
where
50-
Self: Sized,
5170
F: FnMut(B, Self::Item) -> R,
5271
R: Try<Output = B>,
5372
{
@@ -61,6 +80,60 @@ where
6180
self.it.map(T::clone).fold(init, f)
6281
}
6382

83+
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item>
84+
where
85+
P: FnMut(&Self::Item) -> bool,
86+
{
87+
self.it.find(move |x| predicate(&x)).cloned()
88+
}
89+
90+
// FIXME: Implement try_find
91+
92+
fn max_by<F>(self, mut compare: F) -> Option<Self::Item>
93+
where
94+
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
95+
{
96+
self.it.max_by(move |&x, &y| compare(x, y)).cloned()
97+
}
98+
99+
fn min_by<F>(self, mut compare: F) -> Option<Self::Item>
100+
where
101+
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
102+
{
103+
self.it.min_by(move |&x, &y| compare(x, y)).cloned()
104+
}
105+
106+
fn cmp<O>(self, other: O) -> Ordering
107+
where
108+
O: IntoIterator<Item = Self::Item>,
109+
Self::Item: Ord,
110+
{
111+
self.it.cmp_by(other, |x, y| x.cmp(&y))
112+
}
113+
114+
fn partial_cmp<O>(self, other: O) -> Option<Ordering>
115+
where
116+
O: IntoIterator,
117+
Self::Item: PartialOrd<O::Item>,
118+
{
119+
self.it.partial_cmp_by(other, |x, y| x.partial_cmp(&y))
120+
}
121+
122+
fn eq<O>(self, other: O) -> bool
123+
where
124+
O: IntoIterator,
125+
Self::Item: PartialEq<O::Item>,
126+
{
127+
self.it.eq_by(other, |x, y| x == &y)
128+
}
129+
130+
fn is_sorted_by<F>(self, mut compare: F) -> bool
131+
where
132+
F: FnMut(&Self::Item, &Self::Item) -> bool,
133+
{
134+
self.it.is_sorted_by(move |&x, &y| compare(x, y))
135+
}
136+
64137
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
65138
where
66139
Self: TrustedRandomAccessNoCoerce,
@@ -81,9 +154,13 @@ where
81154
self.it.next_back().cloned()
82155
}
83156

157+
#[inline]
158+
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
159+
self.it.advance_back_by(n)
160+
}
161+
84162
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
85163
where
86-
Self: Sized,
87164
F: FnMut(B, Self::Item) -> R,
88165
R: Try<Output = B>,
89166
{
@@ -96,6 +173,13 @@ where
96173
{
97174
self.it.map(T::clone).rfold(init, f)
98175
}
176+
177+
fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
178+
where
179+
P: FnMut(&Self::Item) -> bool,
180+
{
181+
self.it.rfind(move |x| predicate(&x)).cloned()
182+
}
99183
}
100184

101185
#[stable(feature = "iter_cloned", since = "1.1.0")]
@@ -104,10 +188,12 @@ where
104188
I: ExactSizeIterator<Item = &'a T>,
105189
T: Clone,
106190
{
191+
#[inline]
107192
fn len(&self) -> usize {
108193
self.it.len()
109194
}
110195

196+
#[inline]
111197
fn is_empty(&self) -> bool {
112198
self.it.is_empty()
113199
}

Diff for: library/core/src/iter/adapters/copied.rs

+79-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::cmp::Ordering;
12
use crate::iter::adapters::zip::try_get_unchecked;
23
use crate::iter::adapters::{SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
34
use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen};
@@ -48,20 +49,35 @@ where
4849

4950
fn next_chunk<const N: usize>(
5051
&mut self,
51-
) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>>
52-
where
53-
Self: Sized,
54-
{
52+
) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>> {
5553
<I as SpecNextChunk<'_, N, T>>::spec_next_chunk(&mut self.it)
5654
}
5755

56+
#[inline]
5857
fn size_hint(&self) -> (usize, Option<usize>) {
5958
self.it.size_hint()
6059
}
6160

61+
#[inline]
62+
fn count(self) -> usize {
63+
self.it.count()
64+
}
65+
66+
fn last(self) -> Option<T> {
67+
self.it.last().copied()
68+
}
69+
70+
#[inline]
71+
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
72+
self.it.advance_by(n)
73+
}
74+
75+
fn nth(&mut self, n: usize) -> Option<T> {
76+
self.it.nth(n).copied()
77+
}
78+
6279
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
6380
where
64-
Self: Sized,
6581
F: FnMut(B, Self::Item) -> R,
6682
R: Try<Output = B>,
6783
{
@@ -75,21 +91,58 @@ where
7591
self.it.fold(init, copy_fold(f))
7692
}
7793

78-
fn nth(&mut self, n: usize) -> Option<T> {
79-
self.it.nth(n).copied()
94+
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item>
95+
where
96+
P: FnMut(&Self::Item) -> bool,
97+
{
98+
self.it.find(move |x| predicate(&x)).copied()
8099
}
81100

82-
fn last(self) -> Option<T> {
83-
self.it.last().copied()
101+
// FIXME: Implement try_find
102+
103+
fn max_by<F>(self, mut compare: F) -> Option<Self::Item>
104+
where
105+
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
106+
{
107+
self.it.max_by(move |&x, &y| compare(x, y)).copied()
84108
}
85109

86-
fn count(self) -> usize {
87-
self.it.count()
110+
fn min_by<F>(self, mut compare: F) -> Option<Self::Item>
111+
where
112+
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
113+
{
114+
self.it.min_by(move |&x, &y| compare(x, y)).copied()
88115
}
89116

90-
#[inline]
91-
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
92-
self.it.advance_by(n)
117+
fn cmp<O>(self, other: O) -> Ordering
118+
where
119+
O: IntoIterator<Item = Self::Item>,
120+
Self::Item: Ord,
121+
{
122+
self.it.cmp_by(other, |x, y| x.cmp(&y))
123+
}
124+
125+
fn partial_cmp<O>(self, other: O) -> Option<Ordering>
126+
where
127+
O: IntoIterator,
128+
Self::Item: PartialOrd<O::Item>,
129+
{
130+
self.it.partial_cmp_by(other, |x, y| x.partial_cmp(&y))
131+
}
132+
133+
fn eq<O>(self, other: O) -> bool
134+
where
135+
O: IntoIterator,
136+
Self::Item: PartialEq<O::Item>,
137+
{
138+
self.it.eq_by(other, |x, y| x == &y)
139+
}
140+
141+
fn is_sorted_by<F>(self, mut compare: F) -> bool
142+
where
143+
F: FnMut(&Self::Item, &Self::Item) -> bool,
144+
{
145+
self.it.is_sorted_by(move |&x, &y| compare(x, y))
93146
}
94147

95148
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
@@ -112,9 +165,13 @@ where
112165
self.it.next_back().copied()
113166
}
114167

168+
#[inline]
169+
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
170+
self.it.advance_back_by(n)
171+
}
172+
115173
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
116174
where
117-
Self: Sized,
118175
F: FnMut(B, Self::Item) -> R,
119176
R: Try<Output = B>,
120177
{
@@ -128,9 +185,11 @@ where
128185
self.it.rfold(init, copy_fold(f))
129186
}
130187

131-
#[inline]
132-
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
133-
self.it.advance_back_by(n)
188+
fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
189+
where
190+
P: FnMut(&Self::Item) -> bool,
191+
{
192+
self.it.rfind(move |x| predicate(&x)).copied()
134193
}
135194
}
136195

@@ -140,10 +199,12 @@ where
140199
I: ExactSizeIterator<Item = &'a T>,
141200
T: Copy,
142201
{
202+
#[inline]
143203
fn len(&self) -> usize {
144204
self.it.len()
145205
}
146206

207+
#[inline]
147208
fn is_empty(&self) -> bool {
148209
self.it.is_empty()
149210
}

0 commit comments

Comments
 (0)