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