Skip to content

Commit 795a934

Browse files
committed
Auto merge of rust-lang#82043 - tmiasko:may-have-side-effect, r=kennytm
Turn may_have_side_effect into an associated constant The `may_have_side_effect` is an implementation detail of `TrustedRandomAccess` trait. It describes if obtaining an iterator element may have side effects. It is currently implemented as an associated function. Turn `may_have_side_effect` into an associated constant. This makes the value immediately available to the optimizer.
2 parents 67342b8 + dc3304c commit 795a934

File tree

9 files changed

+30
-78
lines changed

9 files changed

+30
-78
lines changed

library/alloc/src/vec/into_iter.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,7 @@ unsafe impl<T, A: Allocator> TrustedRandomAccess for IntoIter<T, A>
212212
where
213213
T: Copy,
214214
{
215-
fn may_have_side_effect() -> bool {
216-
false
217-
}
215+
const MAY_HAVE_SIDE_EFFECT: bool = false;
218216
}
219217

220218
#[stable(feature = "vec_into_iter_clone", since = "1.8.0")]

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ unsafe impl<I> TrustedRandomAccess for Cloned<I>
124124
where
125125
I: TrustedRandomAccess,
126126
{
127-
#[inline]
128-
fn may_have_side_effect() -> bool {
129-
true
130-
}
127+
const MAY_HAVE_SIDE_EFFECT: bool = true;
131128
}
132129

133130
#[unstable(feature = "trusted_len", issue = "37572")]

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ unsafe impl<I> TrustedRandomAccess for Copied<I>
140140
where
141141
I: TrustedRandomAccess,
142142
{
143-
#[inline]
144-
fn may_have_side_effect() -> bool {
145-
I::may_have_side_effect()
146-
}
143+
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
147144
}
148145

149146
#[stable(feature = "iter_copied", since = "1.36.0")]

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ unsafe impl<I> TrustedRandomAccess for Enumerate<I>
210210
where
211211
I: TrustedRandomAccess,
212212
{
213-
fn may_have_side_effect() -> bool {
214-
I::may_have_side_effect()
215-
}
213+
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
216214
}
217215

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

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ unsafe impl<I> TrustedRandomAccess for Fuse<I>
201201
where
202202
I: TrustedRandomAccess,
203203
{
204-
fn may_have_side_effect() -> bool {
205-
I::may_have_side_effect()
206-
}
204+
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
207205
}
208206

209207
// Fuse specialization trait

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ unsafe impl<I, F> TrustedRandomAccess for Map<I, F>
189189
where
190190
I: TrustedRandomAccess,
191191
{
192-
#[inline]
193-
fn may_have_side_effect() -> bool {
194-
true
195-
}
192+
const MAY_HAVE_SIDE_EFFECT: bool = true;
196193
}
197194

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

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

+10-14
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ where
197197
unsafe {
198198
Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
199199
}
200-
} else if A::may_have_side_effect() && self.index < self.a.size() {
200+
} else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a.size() {
201201
let i = self.index;
202202
self.index += 1;
203203
// match the base implementation's potential side effects
@@ -224,15 +224,15 @@ where
224224
while self.index < end {
225225
let i = self.index;
226226
self.index += 1;
227-
if A::may_have_side_effect() {
227+
if A::MAY_HAVE_SIDE_EFFECT {
228228
// SAFETY: the usage of `cmp::min` to calculate `delta`
229229
// ensures that `end` is smaller than or equal to `self.len`,
230230
// so `i` is also smaller than `self.len`.
231231
unsafe {
232232
self.a.__iterator_get_unchecked(i);
233233
}
234234
}
235-
if B::may_have_side_effect() {
235+
if B::MAY_HAVE_SIDE_EFFECT {
236236
// SAFETY: same as above.
237237
unsafe {
238238
self.b.__iterator_get_unchecked(i);
@@ -249,23 +249,21 @@ where
249249
A: DoubleEndedIterator + ExactSizeIterator,
250250
B: DoubleEndedIterator + ExactSizeIterator,
251251
{
252-
let a_side_effect = A::may_have_side_effect();
253-
let b_side_effect = B::may_have_side_effect();
254-
if a_side_effect || b_side_effect {
252+
if A::MAY_HAVE_SIDE_EFFECT || B::MAY_HAVE_SIDE_EFFECT {
255253
let sz_a = self.a.size();
256254
let sz_b = self.b.size();
257255
// Adjust a, b to equal length, make sure that only the first call
258256
// of `next_back` does this, otherwise we will break the restriction
259257
// on calls to `self.next_back()` after calling `get_unchecked()`.
260258
if sz_a != sz_b {
261259
let sz_a = self.a.size();
262-
if a_side_effect && sz_a > self.len {
260+
if A::MAY_HAVE_SIDE_EFFECT && sz_a > self.len {
263261
for _ in 0..sz_a - cmp::max(self.len, self.index) {
264262
self.a.next_back();
265263
}
266264
}
267265
let sz_b = self.b.size();
268-
if b_side_effect && sz_b > self.len {
266+
if B::MAY_HAVE_SIDE_EFFECT && sz_b > self.len {
269267
for _ in 0..sz_b - self.len {
270268
self.b.next_back();
271269
}
@@ -309,9 +307,7 @@ where
309307
A: TrustedRandomAccess,
310308
B: TrustedRandomAccess,
311309
{
312-
fn may_have_side_effect() -> bool {
313-
A::may_have_side_effect() || B::may_have_side_effect()
314-
}
310+
const MAY_HAVE_SIDE_EFFECT: bool = A::MAY_HAVE_SIDE_EFFECT || B::MAY_HAVE_SIDE_EFFECT;
315311
}
316312

317313
#[stable(feature = "fused", since = "1.26.0")]
@@ -422,9 +418,9 @@ pub unsafe trait TrustedRandomAccess: Sized {
422418
{
423419
self.size_hint().0
424420
}
425-
/// Returns `true` if getting an iterator element may have
426-
/// side effects. Remember to take inner iterators into account.
427-
fn may_have_side_effect() -> bool;
421+
/// `true` if getting an iterator element may have side effects.
422+
/// Remember to take inner iterators into account.
423+
const MAY_HAVE_SIDE_EFFECT: bool;
428424
}
429425

430426
/// Like `Iterator::__iterator_get_unchecked`, but doesn't require the compiler to

library/core/src/slice/iter.rs

+13-39
Original file line numberDiff line numberDiff line change
@@ -1305,9 +1305,7 @@ impl<T> FusedIterator for Windows<'_, T> {}
13051305
#[doc(hidden)]
13061306
#[unstable(feature = "trusted_random_access", issue = "none")]
13071307
unsafe impl<'a, T> TrustedRandomAccess for Windows<'a, T> {
1308-
fn may_have_side_effect() -> bool {
1309-
false
1310-
}
1308+
const MAY_HAVE_SIDE_EFFECT: bool = false;
13111309
}
13121310

13131311
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
@@ -1473,9 +1471,7 @@ impl<T> FusedIterator for Chunks<'_, T> {}
14731471
#[doc(hidden)]
14741472
#[unstable(feature = "trusted_random_access", issue = "none")]
14751473
unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> {
1476-
fn may_have_side_effect() -> bool {
1477-
false
1478-
}
1474+
const MAY_HAVE_SIDE_EFFECT: bool = false;
14791475
}
14801476

14811477
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
@@ -1638,9 +1634,7 @@ impl<T> FusedIterator for ChunksMut<'_, T> {}
16381634
#[doc(hidden)]
16391635
#[unstable(feature = "trusted_random_access", issue = "none")]
16401636
unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> {
1641-
fn may_have_side_effect() -> bool {
1642-
false
1643-
}
1637+
const MAY_HAVE_SIDE_EFFECT: bool = false;
16441638
}
16451639

16461640
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
@@ -1794,9 +1788,7 @@ impl<T> FusedIterator for ChunksExact<'_, T> {}
17941788
#[doc(hidden)]
17951789
#[unstable(feature = "trusted_random_access", issue = "none")]
17961790
unsafe impl<'a, T> TrustedRandomAccess for ChunksExact<'a, T> {
1797-
fn may_have_side_effect() -> bool {
1798-
false
1799-
}
1791+
const MAY_HAVE_SIDE_EFFECT: bool = false;
18001792
}
18011793

18021794
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
@@ -1947,9 +1939,7 @@ impl<T> FusedIterator for ChunksExactMut<'_, T> {}
19471939
#[doc(hidden)]
19481940
#[unstable(feature = "trusted_random_access", issue = "none")]
19491941
unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> {
1950-
fn may_have_side_effect() -> bool {
1951-
false
1952-
}
1942+
const MAY_HAVE_SIDE_EFFECT: bool = false;
19531943
}
19541944

19551945
/// A windowed iterator over a slice in overlapping chunks (`N` elements at a
@@ -2186,9 +2176,7 @@ impl<T, const N: usize> FusedIterator for ArrayChunks<'_, T, N> {}
21862176
#[doc(hidden)]
21872177
#[unstable(feature = "array_chunks", issue = "74985")]
21882178
unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunks<'a, T, N> {
2189-
fn may_have_side_effect() -> bool {
2190-
false
2191-
}
2179+
const MAY_HAVE_SIDE_EFFECT: bool = false;
21922180
}
21932181

21942182
/// An iterator over a slice in (non-overlapping) mutable chunks (`N` elements
@@ -2300,9 +2288,7 @@ impl<T, const N: usize> FusedIterator for ArrayChunksMut<'_, T, N> {}
23002288
#[doc(hidden)]
23012289
#[unstable(feature = "array_chunks", issue = "74985")]
23022290
unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunksMut<'a, T, N> {
2303-
fn may_have_side_effect() -> bool {
2304-
false
2305-
}
2291+
const MAY_HAVE_SIDE_EFFECT: bool = false;
23062292
}
23072293

23082294
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
@@ -2464,9 +2450,7 @@ impl<T> FusedIterator for RChunks<'_, T> {}
24642450
#[doc(hidden)]
24652451
#[unstable(feature = "trusted_random_access", issue = "none")]
24662452
unsafe impl<'a, T> TrustedRandomAccess for RChunks<'a, T> {
2467-
fn may_have_side_effect() -> bool {
2468-
false
2469-
}
2453+
const MAY_HAVE_SIDE_EFFECT: bool = false;
24702454
}
24712455

24722456
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
@@ -2627,9 +2611,7 @@ impl<T> FusedIterator for RChunksMut<'_, T> {}
26272611
#[doc(hidden)]
26282612
#[unstable(feature = "trusted_random_access", issue = "none")]
26292613
unsafe impl<'a, T> TrustedRandomAccess for RChunksMut<'a, T> {
2630-
fn may_have_side_effect() -> bool {
2631-
false
2632-
}
2614+
const MAY_HAVE_SIDE_EFFECT: bool = false;
26332615
}
26342616

26352617
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
@@ -2787,9 +2769,7 @@ impl<T> FusedIterator for RChunksExact<'_, T> {}
27872769
#[doc(hidden)]
27882770
#[unstable(feature = "trusted_random_access", issue = "none")]
27892771
unsafe impl<'a, T> TrustedRandomAccess for RChunksExact<'a, T> {
2790-
fn may_have_side_effect() -> bool {
2791-
false
2792-
}
2772+
const MAY_HAVE_SIDE_EFFECT: bool = false;
27932773
}
27942774

27952775
/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
@@ -2944,25 +2924,19 @@ impl<T> FusedIterator for RChunksExactMut<'_, T> {}
29442924
#[doc(hidden)]
29452925
#[unstable(feature = "trusted_random_access", issue = "none")]
29462926
unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
2947-
fn may_have_side_effect() -> bool {
2948-
false
2949-
}
2927+
const MAY_HAVE_SIDE_EFFECT: bool = false;
29502928
}
29512929

29522930
#[doc(hidden)]
29532931
#[unstable(feature = "trusted_random_access", issue = "none")]
29542932
unsafe impl<'a, T> TrustedRandomAccess for Iter<'a, T> {
2955-
fn may_have_side_effect() -> bool {
2956-
false
2957-
}
2933+
const MAY_HAVE_SIDE_EFFECT: bool = false;
29582934
}
29592935

29602936
#[doc(hidden)]
29612937
#[unstable(feature = "trusted_random_access", issue = "none")]
29622938
unsafe impl<'a, T> TrustedRandomAccess for IterMut<'a, T> {
2963-
fn may_have_side_effect() -> bool {
2964-
false
2965-
}
2939+
const MAY_HAVE_SIDE_EFFECT: bool = false;
29662940
}
29672941

29682942
/// An iterator over slice in (non-overlapping) chunks separated by a predicate.

library/core/src/str/iter.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,7 @@ unsafe impl TrustedLen for Bytes<'_> {}
321321
#[doc(hidden)]
322322
#[unstable(feature = "trusted_random_access", issue = "none")]
323323
unsafe impl TrustedRandomAccess for Bytes<'_> {
324-
#[inline]
325-
fn may_have_side_effect() -> bool {
326-
false
327-
}
324+
const MAY_HAVE_SIDE_EFFECT: bool = false;
328325
}
329326

330327
/// This macro generates a Clone impl for string pattern API

0 commit comments

Comments
 (0)