|
2 | 2 |
|
3 | 3 | use crate::{
|
4 | 4 | fmt,
|
5 |
| - iter::{ExactSizeIterator, FusedIterator, TrustedLen}, |
| 5 | + iter::{ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess}, |
6 | 6 | mem::{self, MaybeUninit},
|
7 | 7 | ops::Range,
|
8 | 8 | ptr,
|
@@ -130,6 +130,18 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
|
130 | 130 | fn last(mut self) -> Option<Self::Item> {
|
131 | 131 | self.next_back()
|
132 | 132 | }
|
| 133 | + |
| 134 | + #[inline] |
| 135 | + unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item |
| 136 | + where |
| 137 | + Self: TrustedRandomAccess, |
| 138 | + { |
| 139 | + // SAFETY: Callers are only allowed to pass an index that is in bounds |
| 140 | + // Additionally Self: TrustedRandomAccess is only implemented for T: Copy which means even |
| 141 | + // multiple repeated reads of the same index would be safe and the |
| 142 | + // values aree !Drop, thus won't suffer from double drops. |
| 143 | + unsafe { self.data.get_unchecked(self.alive.start + idx).assume_init_read() } |
| 144 | + } |
133 | 145 | }
|
134 | 146 |
|
135 | 147 | #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
@@ -184,6 +196,17 @@ impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}
|
184 | 196 | #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
185 | 197 | unsafe impl<T, const N: usize> TrustedLen for IntoIter<T, N> {}
|
186 | 198 |
|
| 199 | +#[doc(hidden)] |
| 200 | +#[unstable(feature = "trusted_random_access", issue = "none")] |
| 201 | +// T: Copy as approximation for !Drop since get_unchecked does not update the pointers |
| 202 | +// and thus we can't implement drop-handling |
| 203 | +unsafe impl<T, const N: usize> TrustedRandomAccess for IntoIter<T, N> |
| 204 | +where |
| 205 | + T: Copy, |
| 206 | +{ |
| 207 | + const MAY_HAVE_SIDE_EFFECT: bool = false; |
| 208 | +} |
| 209 | + |
187 | 210 | #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
188 | 211 | impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
|
189 | 212 | fn clone(&self) -> Self {
|
|
0 commit comments