implement TrustedRandomAccess for array::IntoIter

This commit is contained in:
The8472 2021-01-31 21:16:08 +01:00
parent 895d7a9a09
commit 08a1dd287d

View File

@ -2,7 +2,7 @@
use crate::{ use crate::{
fmt, fmt,
iter::{ExactSizeIterator, FusedIterator, TrustedLen}, iter::{ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess},
mem::{self, MaybeUninit}, mem::{self, MaybeUninit},
ops::Range, ops::Range,
ptr, ptr,
@ -130,6 +130,18 @@ fn count(self) -> usize {
fn last(mut self) -> Option<Self::Item> { fn last(mut self) -> Option<Self::Item> {
self.next_back() self.next_back()
} }
#[inline]
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 aree !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")]
@ -184,6 +196,17 @@ impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}
#[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> {} 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")] #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
impl<T: Clone, const N: usize> Clone for IntoIter<T, N> { impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
fn clone(&self) -> Self { fn clone(&self) -> Self {