use crate::{
fmt,
- iter::{self, ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess},
+ iter::{self, ExactSizeIterator, FusedIterator, TrustedLen},
mem::{self, MaybeUninit},
ops::Range,
ptr,
(len, Some(len))
}
+ #[inline]
+ fn fold<Acc, Fold>(mut self, init: Acc, mut fold: Fold) -> Acc
+ where
+ Fold: FnMut(Acc, Self::Item) -> Acc,
+ {
+ let data = &mut self.data;
+ // FIXME: This uses try_fold(&mut iter) instead of fold(iter) because the latter
+ // would go through the blanket `impl Iterator for &mut I` implementation
+ // which lacks inline annotations on its methods and adding those would be a larger
+ // perturbation than using try_fold here.
+ // Whether it would be beneficial to add those annotations should be investigated separately.
+ (&mut self.alive)
+ .try_fold::<_, _, Result<_, !>>(init, |acc, idx| {
+ // SAFETY: idx is obtained by folding over the `alive` range, which implies the
+ // value is currently considered alive but as the range is being consumed each value
+ // we read here will only be read once and then considered dead.
+ Ok(fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() }))
+ })
+ .unwrap()
+ }
+
fn count(self) -> usize {
self.len()
}
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
-
- #[inline]
- #[doc(hidden)]
- unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
- where
- Self: TrustedRandomAccess,
- {
- // SAFETY: Callers are only allowed to pass an index that is in bounds
- // Additionally Self: TrustedRandomAccess is only implemented for T: Copy which means even
- // multiple repeated reads of the same index would be safe and the
- // values are !Drop, thus won't suffer from double drops.
- unsafe { self.data.get_unchecked(self.alive.start + idx).assume_init_read() }
- }
}
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
unsafe impl<T, const N: usize> TrustedLen for IntoIter<T, N> {}
-#[doc(hidden)]
-#[unstable(feature = "trusted_random_access", issue = "none")]
-// T: Copy as approximation for !Drop since get_unchecked does not update the pointers
-// and thus we can't implement drop-handling
-unsafe impl<T, const N: usize> TrustedRandomAccess for IntoIter<T, N>
-where
- T: Copy,
-{
- const MAY_HAVE_SIDE_EFFECT: bool = false;
-}
-
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
fn clone(&self) -> Self {