core/slice/iter/macros.rs
1//! Macros used by iterators of slice.
2
3/// Convenience & performance macro for consuming the `end_or_len` field, by
4/// giving a `(&mut) usize` or `(&mut) NonNull<T>` depending whether `T` is
5/// or is not a ZST respectively.
6///
7/// Internally, this reads the `end` through a pointer-to-`NonNull` so that
8/// it'll get the appropriate non-null metadata in the backend without needing
9/// to call `assume` manually.
10macro_rules! if_zst {
11 (mut $this:ident, $len:ident => $zst_body:expr, $end:ident => $other_body:expr,) => {{
12 #![allow(unused_unsafe)] // we're sometimes used within an unsafe block
13
14 if T::IS_ZST {
15 // SAFETY: for ZSTs, the pointer is storing a provenance-free length,
16 // so consuming and updating it as a `usize` is fine.
17 let $len = unsafe { &mut *(&raw mut $this.end_or_len).cast::<usize>() };
18 $zst_body
19 } else {
20 // SAFETY: for non-ZSTs, the type invariant ensures it cannot be null
21 let $end = unsafe { &mut *(&raw mut $this.end_or_len).cast::<NonNull<T>>() };
22 $other_body
23 }
24 }};
25 ($this:ident, $len:ident => $zst_body:expr, $end:ident => $other_body:expr,) => {{
26 #![allow(unused_unsafe)] // we're sometimes used within an unsafe block
27
28 if T::IS_ZST {
29 let $len = $this.end_or_len.addr();
30 $zst_body
31 } else {
32 // SAFETY: for non-ZSTs, the type invariant ensures it cannot be null
33 let $end = unsafe { mem::transmute::<*const T, NonNull<T>>($this.end_or_len) };
34 $other_body
35 }
36 }};
37}
38
39// Inlining is_empty and len makes a huge performance difference
40macro_rules! is_empty {
41 ($self: ident) => {
42 if_zst!($self,
43 len => len == 0,
44 end => $self.ptr == end,
45 )
46 };
47}
48
49macro_rules! len {
50 ($self: ident) => {{
51 if_zst!($self,
52 len => len,
53 end => {
54 // To get rid of some bounds checks (see `position`), we use ptr_sub instead of
55 // offset_from (Tested by `codegen/slice-position-bounds-check`.)
56 // SAFETY: by the type invariant pointers are aligned and `start <= end`
57 unsafe { end.offset_from_unsigned($self.ptr) }
58 },
59 )
60 }};
61}
62
63// The shared definition of the `Iter` and `IterMut` iterators
64macro_rules! iterator {
65 (
66 struct $name:ident -> $ptr:ty,
67 $elem:ty,
68 $raw_mut:tt,
69 {$( $mut_:tt )?},
70 $into_ref:ident,
71 $array_ref:ident,
72 {$($extra:tt)*}
73 ) => {
74 impl<'a, T> $name<'a, T> {
75 /// Returns the last element and moves the end of the iterator backwards by 1.
76 ///
77 /// # Safety
78 ///
79 /// The iterator must not be empty
80 #[inline]
81 unsafe fn next_back_unchecked(&mut self) -> $elem {
82 // SAFETY: the caller promised it's not empty, so
83 // the offsetting is in-bounds and there's an element to return.
84 unsafe { self.pre_dec_end(1).$into_ref() }
85 }
86
87 // Helper function for creating a slice from the iterator.
88 #[inline(always)]
89 // NOTE(dead_code): Method is used by Iter, but not IterMut
90 #[cfg_attr(feature = "ferrocene_subset", allow(dead_code))]
91 fn make_slice(&self) -> &'a [T] {
92 // SAFETY: the iterator was created from a slice with pointer
93 // `self.ptr` and length `len!(self)`. This guarantees that all
94 // the prerequisites for `from_raw_parts` are fulfilled.
95 unsafe { from_raw_parts(self.ptr.as_ptr(), len!(self)) }
96 }
97
98 // Helper function for moving the start of the iterator forwards by `offset` elements,
99 // returning the old start.
100 // Unsafe because the offset must not exceed `self.len()`.
101 #[inline(always)]
102 unsafe fn post_inc_start(&mut self, offset: usize) -> NonNull<T> {
103 let old = self.ptr;
104
105 // SAFETY: the caller guarantees that `offset` doesn't exceed `self.len()`,
106 // so this new pointer is inside `self` and thus guaranteed to be non-null.
107 unsafe {
108 if_zst!(mut self,
109 // Using the intrinsic directly avoids emitting a UbCheck
110 len => *len = crate::intrinsics::unchecked_sub(*len, offset),
111 _end => self.ptr = self.ptr.add(offset),
112 );
113 }
114 old
115 }
116
117 // Helper function for moving the end of the iterator backwards by `offset` elements,
118 // returning the new end.
119 // Unsafe because the offset must not exceed `self.len()`.
120 #[inline(always)]
121 unsafe fn pre_dec_end(&mut self, offset: usize) -> NonNull<T> {
122 if_zst!(mut self,
123 // SAFETY: By our precondition, `offset` can be at most the
124 // current length, so the subtraction can never overflow.
125 len => unsafe {
126 // Using the intrinsic directly avoids emitting a UbCheck
127 *len = crate::intrinsics::unchecked_sub(*len, offset);
128 self.ptr
129 },
130 // SAFETY: the caller guarantees that `offset` doesn't exceed `self.len()`,
131 // which is guaranteed to not overflow an `isize`. Also, the resulting pointer
132 // is in bounds of `slice`, which fulfills the other requirements for `offset`.
133 end => unsafe {
134 *end = end.sub(offset);
135 *end
136 },
137 )
138 }
139 }
140
141 #[stable(feature = "rust1", since = "1.0.0")]
142 impl<T> ExactSizeIterator for $name<'_, T> {
143 #[inline(always)]
144 fn len(&self) -> usize {
145 len!(self)
146 }
147
148 #[inline(always)]
149 #[cfg(not(feature = "ferrocene_subset"))]
150 fn is_empty(&self) -> bool {
151 is_empty!(self)
152 }
153 }
154
155 #[stable(feature = "rust1", since = "1.0.0")]
156 impl<'a, T> Iterator for $name<'a, T> {
157 type Item = $elem;
158
159 #[inline]
160 fn next(&mut self) -> Option<$elem> {
161 // intentionally not using the helpers because this is
162 // one of the most mono'd things in the library.
163
164 let ptr = self.ptr;
165 let end_or_len = self.end_or_len;
166 // SAFETY: See inner comments. (For some reason having multiple
167 // block breaks inlining this -- if you can fix that please do!)
168 unsafe {
169 if T::IS_ZST {
170 let len = end_or_len.addr();
171 if len == 0 {
172 return None;
173 }
174 // SAFETY: just checked that it's not zero, so subtracting one
175 // cannot wrap. (Ideally this would be `checked_sub`, which
176 // does the same thing internally, but as of 2025-02 that
177 // doesn't optimize quite as small in MIR.)
178 self.end_or_len = without_provenance_mut(len.unchecked_sub(1));
179 } else {
180 // SAFETY: by type invariant, the `end_or_len` field is always
181 // non-null for a non-ZST pointee. (This transmute ensures we
182 // get `!nonnull` metadata on the load of the field.)
183 if ptr == crate::intrinsics::transmute::<$ptr, NonNull<T>>(end_or_len) {
184 return None;
185 }
186 // SAFETY: since it's not empty, per the check above, moving
187 // forward one keeps us inside the slice, and this is valid.
188 self.ptr = ptr.add(1);
189 }
190 // SAFETY: Now that we know it wasn't empty and we've moved past
191 // the first one (to avoid giving a duplicate `&mut` next time),
192 // we can give out a reference to it.
193 Some({ptr}.$into_ref())
194 }
195 }
196
197 #[cfg(not(feature = "ferrocene_subset"))]
198 fn next_chunk<const N:usize>(&mut self) -> Result<[$elem; N], crate::array::IntoIter<$elem, N>> {
199 if T::IS_ZST {
200 return crate::array::iter_next_chunk(self);
201 }
202 let len = len!(self);
203 if len >= N {
204 // SAFETY: we are just getting an array of [T; N] and moving the pointer over a little
205 let r = unsafe { self.post_inc_start(N).cast_array().$into_ref() }
206 .$array_ref(); // must convert &[T; N] to [&T; N]
207 Ok(r)
208 } else {
209 // cant use $array_ref because theres no builtin for &mut [MU<T>; N] -> [&mut MU<T>; N]
210 // cant use copy_nonoverlapping as the $elem is of type &{mut} T instead of T
211 let mut a = [const { crate::mem::MaybeUninit::<$elem>::uninit() }; N];
212 for into in (&mut a).into_iter().take(len) {
213 // SAFETY: take(n) limits to remainder (slice produces worse codegen)
214 into.write(unsafe { self.post_inc_start(1).$into_ref() });
215 }
216 // SAFETY: we just initialized elements 0..len
217 unsafe { Err(crate::array::IntoIter::new_unchecked(a, 0..len)) }
218 }
219 }
220
221 #[inline]
222 #[cfg(not(feature = "ferrocene_subset"))]
223 fn size_hint(&self) -> (usize, Option<usize>) {
224 let exact = len!(self);
225 (exact, Some(exact))
226 }
227
228 #[inline]
229 #[cfg(not(feature = "ferrocene_subset"))]
230 fn count(self) -> usize {
231 len!(self)
232 }
233
234 #[inline]
235 fn nth(&mut self, n: usize) -> Option<$elem> {
236 if n >= len!(self) {
237 // This iterator is now empty.
238 if_zst!(mut self,
239 len => *len = 0,
240 end => self.ptr = *end,
241 );
242 return None;
243 }
244 // SAFETY: We are in bounds. `post_inc_start` does the right thing even for ZSTs.
245 unsafe {
246 self.post_inc_start(n);
247 Some(self.next_unchecked())
248 }
249 }
250
251 #[inline]
252 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
253 let advance = cmp::min(len!(self), n);
254 // SAFETY: By construction, `advance` does not exceed `self.len()`.
255 unsafe { self.post_inc_start(advance) };
256 NonZero::new(n - advance).map_or(Ok(()), Err)
257 }
258
259 #[inline]
260 fn last(mut self) -> Option<$elem> {
261 self.next_back()
262 }
263
264 #[inline]
265 fn fold<B, F>(self, init: B, mut f: F) -> B
266 where
267 F: FnMut(B, Self::Item) -> B,
268 {
269 // this implementation consists of the following optimizations compared to the
270 // default implementation:
271 // - do-while loop, as is llvm's preferred loop shape,
272 // see https://releases.llvm.org/16.0.0/docs/LoopTerminology.html#more-canonical-loops
273 // - bumps an index instead of a pointer since the latter case inhibits
274 // some optimizations, see #111603
275 // - avoids Option wrapping/matching
276 if is_empty!(self) {
277 return init;
278 }
279 let mut acc = init;
280 let mut i = 0;
281 let len = len!(self);
282 loop {
283 // SAFETY: the loop iterates `i in 0..len`, which always is in bounds of
284 // the slice allocation
285 acc = f(acc, unsafe { & $( $mut_ )? *self.ptr.add(i).as_ptr() });
286 // SAFETY: `i` can't overflow since it'll only reach usize::MAX if the
287 // slice had that length, in which case we'll break out of the loop
288 // after the increment
289 i = unsafe { i.unchecked_add(1) };
290 if i == len {
291 break;
292 }
293 }
294 acc
295 }
296
297 // We override the default implementation, which uses `try_fold`,
298 // because this simple implementation generates less LLVM IR and is
299 // faster to compile.
300 #[inline]
301 fn for_each<F>(mut self, mut f: F)
302 where
303 Self: Sized,
304 F: FnMut(Self::Item),
305 {
306 while let Some(x) = self.next() {
307 f(x);
308 }
309 }
310
311 // We override the default implementation, which uses `try_fold`,
312 // because this simple implementation generates less LLVM IR and is
313 // faster to compile.
314 #[inline]
315 fn all<F>(&mut self, mut f: F) -> bool
316 where
317 Self: Sized,
318 F: FnMut(Self::Item) -> bool,
319 {
320 while let Some(x) = self.next() {
321 if !f(x) {
322 return false;
323 }
324 }
325 true
326 }
327
328 // We override the default implementation, which uses `try_fold`,
329 // because this simple implementation generates less LLVM IR and is
330 // faster to compile.
331 #[inline]
332 fn any<F>(&mut self, mut f: F) -> bool
333 where
334 Self: Sized,
335 F: FnMut(Self::Item) -> bool,
336 {
337 while let Some(x) = self.next() {
338 if f(x) {
339 return true;
340 }
341 }
342 false
343 }
344
345 // We override the default implementation, which uses `try_fold`,
346 // because this simple implementation generates less LLVM IR and is
347 // faster to compile.
348 #[inline]
349 fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item>
350 where
351 Self: Sized,
352 P: FnMut(&Self::Item) -> bool,
353 {
354 while let Some(x) = self.next() {
355 if predicate(&x) {
356 return Some(x);
357 }
358 }
359 None
360 }
361
362 // We override the default implementation, which uses `try_fold`,
363 // because this simple implementation generates less LLVM IR and is
364 // faster to compile.
365 #[inline]
366 #[cfg(not(feature = "ferrocene_subset"))]
367 fn find_map<B, F>(&mut self, mut f: F) -> Option<B>
368 where
369 Self: Sized,
370 F: FnMut(Self::Item) -> Option<B>,
371 {
372 while let Some(x) = self.next() {
373 if let Some(y) = f(x) {
374 return Some(y);
375 }
376 }
377 None
378 }
379
380 // We override the default implementation, which uses `try_fold`,
381 // because this simple implementation generates less LLVM IR and is
382 // faster to compile. Also, the `assume` avoids a bounds check.
383 #[inline]
384 fn position<P>(&mut self, mut predicate: P) -> Option<usize> where
385 Self: Sized,
386 P: FnMut(Self::Item) -> bool,
387 {
388 let n = len!(self);
389 let mut i = 0;
390 while let Some(x) = self.next() {
391 if predicate(x) {
392 // SAFETY: we are guaranteed to be in bounds by the loop invariant:
393 // when `i >= n`, `self.next()` returns `None` and the loop breaks.
394 unsafe { assert_unchecked(i < n) };
395 return Some(i);
396 }
397 i += 1;
398 }
399 None
400 }
401
402 // We override the default implementation, which uses `try_fold`,
403 // because this simple implementation generates less LLVM IR and is
404 // faster to compile. Also, the `assume` avoids a bounds check.
405 #[inline]
406 #[cfg(not(feature = "ferrocene_subset"))]
407 fn rposition<P>(&mut self, mut predicate: P) -> Option<usize> where
408 P: FnMut(Self::Item) -> bool,
409 Self: Sized + ExactSizeIterator + DoubleEndedIterator
410 {
411 let n = len!(self);
412 let mut i = n;
413 while let Some(x) = self.next_back() {
414 i -= 1;
415 if predicate(x) {
416 // SAFETY: `i` must be lower than `n` since it starts at `n`
417 // and is only decreasing.
418 unsafe { assert_unchecked(i < n) };
419 return Some(i);
420 }
421 }
422 None
423 }
424
425 #[inline]
426 #[cfg(not(feature = "ferrocene_subset"))]
427 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
428 // SAFETY: the caller must guarantee that `i` is in bounds of
429 // the underlying slice, so `i` cannot overflow an `isize`, and
430 // the returned references is guaranteed to refer to an element
431 // of the slice and thus guaranteed to be valid.
432 //
433 // Also note that the caller also guarantees that we're never
434 // called with the same index again, and that no other methods
435 // that will access this subslice are called, so it is valid
436 // for the returned reference to be mutable in the case of
437 // `IterMut`
438 unsafe { & $( $mut_ )? * self.ptr.as_ptr().add(idx) }
439 }
440
441 $($extra)*
442 }
443
444 #[stable(feature = "rust1", since = "1.0.0")]
445 impl<'a, T> DoubleEndedIterator for $name<'a, T> {
446 #[inline]
447 fn next_back(&mut self) -> Option<$elem> {
448 // could be implemented with slices, but this avoids bounds checks
449
450 // SAFETY: The call to `next_back_unchecked`
451 // is safe since we check if the iterator is empty first.
452 unsafe {
453 if is_empty!(self) {
454 None
455 } else {
456 Some(self.next_back_unchecked())
457 }
458 }
459 }
460
461 #[inline]
462 fn nth_back(&mut self, n: usize) -> Option<$elem> {
463 if n >= len!(self) {
464 // This iterator is now empty.
465 if_zst!(mut self,
466 len => *len = 0,
467 end => *end = self.ptr,
468 );
469 return None;
470 }
471 // SAFETY: We are in bounds. `pre_dec_end` does the right thing even for ZSTs.
472 unsafe {
473 self.pre_dec_end(n);
474 Some(self.next_back_unchecked())
475 }
476 }
477
478 #[inline]
479 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
480 let advance = cmp::min(len!(self), n);
481 // SAFETY: By construction, `advance` does not exceed `self.len()`.
482 unsafe { self.pre_dec_end(advance) };
483 NonZero::new(n - advance).map_or(Ok(()), Err)
484 }
485 }
486
487 #[stable(feature = "fused", since = "1.26.0")]
488 #[cfg(not(feature = "ferrocene_subset"))]
489 impl<T> FusedIterator for $name<'_, T> {}
490
491 #[unstable(feature = "trusted_len", issue = "37572")]
492 unsafe impl<T> TrustedLen for $name<'_, T> {}
493
494 impl<'a, T> UncheckedIterator for $name<'a, T> {
495 #[inline]
496 unsafe fn next_unchecked(&mut self) -> $elem {
497 // SAFETY: The caller promised there's at least one more item.
498 unsafe {
499 self.post_inc_start(1).$into_ref()
500 }
501 }
502 }
503
504 #[stable(feature = "default_iters", since = "1.70.0")]
505 #[cfg(not(feature = "ferrocene_subset"))]
506 impl<T> Default for $name<'_, T> {
507 /// Creates an empty slice iterator.
508 ///
509 /// ```
510 #[doc = concat!("# use core::slice::", stringify!($name), ";")]
511 #[doc = concat!("let iter: ", stringify!($name<'_, u8>), " = Default::default();")]
512 /// assert_eq!(iter.len(), 0);
513 /// ```
514 fn default() -> Self {
515 (& $( $mut_ )? []).into_iter()
516 }
517 }
518 }
519}
520
521#[cfg(not(feature = "ferrocene_subset"))]
522macro_rules! forward_iterator {
523 ($name:ident: $elem:ident, $iter_of:ty) => {
524 #[stable(feature = "rust1", since = "1.0.0")]
525 impl<'a, $elem, P> Iterator for $name<'a, $elem, P>
526 where
527 P: FnMut(&T) -> bool,
528 {
529 type Item = $iter_of;
530
531 #[inline]
532 fn next(&mut self) -> Option<$iter_of> {
533 self.inner.next()
534 }
535
536 #[inline]
537 fn size_hint(&self) -> (usize, Option<usize>) {
538 self.inner.size_hint()
539 }
540 }
541
542 #[stable(feature = "fused", since = "1.26.0")]
543 impl<'a, $elem, P> FusedIterator for $name<'a, $elem, P> where P: FnMut(&T) -> bool {}
544 };
545}